Save This Page
Home » openjdk-7 » javax » swing » beaninfo » [javadoc | source]
    1   /*
    2    * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
    3    * 
    4    * Redistribution and use in source and binary forms, with or
    5    * without modification, are permitted provided that the following
    6    * conditions are met:
    7    * 
    8    * - Redistributions of source code must retain the above copyright
    9    *   notice, this list of conditions and the following disclaimer.
   10    * 
   11    * - Redistribution in binary form must reproduce the above
   12    *   copyright notice, this list of conditions and the following
   13    *   disclaimer in the documentation and/or other materials
   14    *   provided with the distribution.
   15    * 
   16    * Neither the name of Sun Microsystems, Inc. or the names of
   17    * contributors may be used to endorse or promote products derived
   18    * from this software without specific prior written permission.
   19    * 
   20    * This software is provided "AS IS," without a warranty of any
   21    * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
   22    * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
   23    * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
   24    * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
   25    * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
   26    * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
   27    * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
   28    * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
   29    * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
   30    * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
   31    * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
   32    * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
   33    * 
   34    * You acknowledge that this software is not designed, licensed or
   35    * intended for use in the design, construction, operation or
   36    * maintenance of any nuclear facility.
   37    */
   38   
   39   
   40   package javax.swing.beaninfo;
   41   
   42   import java.awt;
   43   import java.awt.event;
   44   import javax.swing;
   45   import javax.swing.border.Border;
   46   import javax.swing.border.TitledBorder;
   47   
   48   
   49   // XXX: how to set one of these dynamically from the bean target?
   50   public class BorderChooser extends JTabbedPane {
   51     private Border border = null;
   52     private static Color color = null;
   53     private static JPanel previous = null;
   54     
   55     
   56       public BorderChooser() {
   57           Border blackline, etched, raisedbevel, loweredbevel, empty;
   58   
   59           //A border that puts 10 extra pixels at the sides and
   60           //bottom of each pane.
   61           Border paneEdge = BorderFactory.createEmptyBorder(0,10,10,10);
   62   
   63   	blackline = BorderFactory.createLineBorder(Color.black);
   64   	etched = BorderFactory.createEtchedBorder();
   65           raisedbevel = BorderFactory.createRaisedBevelBorder();
   66           loweredbevel = BorderFactory.createLoweredBevelBorder();
   67           empty = BorderFactory.createEmptyBorder();
   68   
   69           //First pane: simple borders
   70           JPanel simpleBorders = new JPanel();
   71           simpleBorders.setBorder(paneEdge);
   72           simpleBorders.setLayout(new BoxLayout(simpleBorders,
   73                                                 BoxLayout.Y_AXIS));
   74   
   75           addCompForBorder(blackline, "line border",
   76                            simpleBorders);
   77           addCompForBorder(etched, "etched border",
   78                            simpleBorders);
   79           addCompForBorder(raisedbevel, "raised bevel border",
   80                            simpleBorders);
   81           addCompForBorder(loweredbevel, "lowered bevel border",
   82                            simpleBorders);
   83           addCompForBorder(empty, "empty border",
   84                            simpleBorders);
   85   
   86           //Second pane: matte borders
   87           JPanel matteBorders = new JPanel();
   88           matteBorders.setBorder(paneEdge);
   89           matteBorders.setLayout(new BoxLayout(matteBorders,
   90                                                 BoxLayout.Y_AXIS));
   91   
   92           //XXX: We *should* size the component so that the icons tile OK.
   93           //XXX: Without that, the icons are likely to be cut off and look bad.
   94           ImageIcon icon = new ImageIcon("images/left.gif"); //20x22
   95           Border border = BorderFactory.createMatteBorder(-1, -1, -1, -1, icon);
   96           addCompForBorder(border,
   97                            "matte border (-1,-1,-1,-1,icon)",
   98                            matteBorders);
   99           border = BorderFactory.createMatteBorder(1, 5, 1, 1, Color.red);
  100           addCompForBorder(border,
  101                            "matte border (1,5,1,1,Color.red)",
  102                            matteBorders);
  103           border = BorderFactory.createMatteBorder(0, 20, 0, 0, icon);
  104           addCompForBorder(border,
  105                            "matte border (0,20,0,0,icon)",
  106                            matteBorders);
  107   
  108           //Third pane: titled borders
  109           JPanel titledBorders = new JPanel();
  110           titledBorders.setBorder(paneEdge);
  111           titledBorders.setLayout(new BoxLayout(titledBorders,
  112                                                 BoxLayout.Y_AXIS));
  113           TitledBorder titled;
  114   
  115           titled = BorderFactory.createTitledBorder("title");
  116           addCompForBorder(titled,
  117                            "default titled border"
  118                            + " (default just., default pos.)",
  119                            titledBorders);
  120   
  121           titled = BorderFactory.createTitledBorder(
  122                                 blackline, "title");
  123           addCompForTitledBorder(titled,
  124                                  "titled line border"
  125                                      + " (centered, default pos.)",
  126                                  TitledBorder.CENTER,
  127                                  TitledBorder.DEFAULT_POSITION,
  128                                  titledBorders);
  129   
  130           titled = BorderFactory.createTitledBorder(etched, "title");
  131           addCompForTitledBorder(titled,
  132                                  "titled etched border"
  133                                      + " (right just., default pos.)",
  134                                  TitledBorder.RIGHT,
  135                                  TitledBorder.DEFAULT_POSITION,
  136                                  titledBorders);
  137   
  138           titled = BorderFactory.createTitledBorder(
  139                           loweredbevel, "title");
  140           addCompForTitledBorder(titled,
  141                                  "titled lowered bevel border"
  142                                      + " (default just., above top)",
  143                                  TitledBorder.DEFAULT_JUSTIFICATION,
  144                                  TitledBorder.ABOVE_TOP,
  145                                  titledBorders);
  146   
  147           titled = BorderFactory.createTitledBorder(
  148                           empty, "title");
  149           addCompForTitledBorder(titled, "titled empty border"
  150                                  + " (default just., bottom)",
  151                                  TitledBorder.DEFAULT_JUSTIFICATION,
  152                                  TitledBorder.BOTTOM,
  153                                  titledBorders);
  154   
  155           //Fourth pane: compound borders
  156           JPanel compoundBorders = new JPanel();
  157           compoundBorders.setBorder(paneEdge);
  158           compoundBorders.setLayout(new BoxLayout(compoundBorders,
  159                                                 BoxLayout.Y_AXIS));
  160           Border redline = BorderFactory.createLineBorder(Color.red);
  161   
  162           Border compound;
  163           compound = BorderFactory.createCompoundBorder(
  164                                     raisedbevel, loweredbevel);
  165           addCompForBorder(compound, "compound border (two bevels)",
  166                            compoundBorders);
  167   
  168           compound = BorderFactory.createCompoundBorder(
  169                                     redline, compound);
  170           addCompForBorder(compound, "compound border (add a red outline)",
  171                            compoundBorders);
  172   
  173           titled = BorderFactory.createTitledBorder(
  174                                     compound, "title",
  175                                     TitledBorder.CENTER,
  176                                     TitledBorder.BELOW_BOTTOM);
  177           addCompForBorder(titled, 
  178                            "titled compound border"
  179                            + " (centered, below bottom)",
  180                            compoundBorders);
  181   
  182           addTab("Simple", null, simpleBorders, null);
  183           addTab("Matte", null, matteBorders, null);
  184           addTab("Titled", null, titledBorders, null);
  185           addTab("Compound", null, compoundBorders, null);
  186           setSelectedIndex(0);
  187       }
  188   
  189       void addCompForTitledBorder(TitledBorder border,
  190                                   String description,
  191                                   int justification,
  192                                   int position,
  193                                   Container container) {
  194           border.setTitleJustification(justification);
  195           border.setTitlePosition(position);
  196           addCompForBorder(border, description,
  197                            container);
  198       }
  199   
  200       void addCompForBorder(Border border,
  201                             String description,
  202                             Container container) {
  203           JPanel comp = new JPanel(false);
  204   	JLabel label = new JLabel(description, JLabel.CENTER);
  205   	label.setPreferredSize(new Dimension(180,20));
  206   	label.setMaximumSize(new Dimension(180,20));
  207   	label.setMinimumSize(new Dimension(180,20));
  208   	//	label.setToolTipText("mouse click selects Border - double click for more...");
  209   	comp.setLayout(new GridLayout(1, 1));
  210   	comp.add(label);
  211           comp.setBorder(border);
  212   	comp.addMouseListener(new ActiveBorderListener(border,comp));
  213           container.add(Box.createRigidArea(new Dimension(0, 10))); 
  214           container.add(comp);
  215       }
  216   
  217     public Border getSelectedBorder(){
  218       return this.border;
  219     }
  220   
  221     public void setSelectedBorder(Border b, JPanel p){
  222       if (border != b){
  223         if (previous != null)
  224   	previous.setBackground(color);
  225         previous = p;
  226         color = p.getBackground();
  227         p.setBackground(UIManager.getColor("Button.select"));
  228         p.revalidate();
  229         p.repaint();
  230       }
  231       this.border = b;
  232     }
  233     
  234   
  235     class ActiveBorderListener extends MouseAdapter {
  236       Border b;
  237       JPanel p;
  238       
  239       public ActiveBorderListener(Border b, JPanel p){
  240         super();
  241         this.b = b;
  242         this.p = p;
  243       }
  244   
  245       public void mousePressed(MouseEvent e){
  246         setSelectedBorder(b,p);
  247         if (e.getClickCount() ==  2){
  248   	// bring up title/color editor
  249   	if (b instanceof TitledBorder){
  250   	  TitledBorder t = (TitledBorder)b;
  251   	  if (t.getTitle() != null){
  252   	    String title = JOptionPane.showInputDialog(p, "Please Enter Title String: ");
  253   	    if (title != null){
  254   	      t.setTitle(title);
  255   	    }
  256   	  }
  257   	}
  258         }
  259       }
  260     };    
  261   
  262   
  263   
  264       public static void main(String[] args) {
  265           JFrame frame = new JFrame("BorderChooser");
  266   
  267           frame.addWindowListener(new WindowAdapter() {
  268                   public void windowClosing(WindowEvent e) {
  269                       System.exit(0);
  270                   }
  271               });
  272   
  273           frame.getContentPane().add("Center", new BorderChooser());
  274           frame.pack();
  275           frame.show();
  276       }
  277   }

Save This Page
Home » openjdk-7 » javax » swing » beaninfo » [javadoc | source]