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   package javax.swing.beaninfo;
   40   
   41   import java.awt;
   42   import java.awt.event;
   43   
   44   import javax.swing;
   45   import javax.swing.border;
   46   
   47   /**
   48    * Swing version of a Border property editor.
   49    *
   50    * @version 1.3 02/27/02
   51    * @author  Tom Santos
   52    * @author  Mark Davidson
   53    */
   54   public class SwingBorderEditor extends SwingEditorSupport {
   55     
   56       private JComboBox borderCombo;
   57       private JButton  borderButton;
   58       private BorderDialog borderDialog;
   59   
   60       private Border etched = BorderFactory.createEtchedBorder();
   61       private Border bevelLowered = BorderFactory.createLoweredBevelBorder();
   62       private Border bevelRaised =BorderFactory.createRaisedBevelBorder();
   63       private Border line = BorderFactory.createLineBorder(Color.black);
   64       private Border borders[] = { etched, bevelLowered, bevelRaised, line };
   65   
   66       private Border border;
   67   
   68       public SwingBorderEditor(){
   69   	createComponents();
   70   	addComponentListeners();
   71       }
   72   
   73       private void createComponents() {
   74   	panel = new JPanel();
   75   	panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
   76   	panel.setAlignmentX(Component.LEFT_ALIGNMENT);
   77   	UIDefaults table = UIManager.getDefaults();
   78   	table.put("beaninfo.BorderIcon", LookAndFeel.makeIcon(getClass(), "icons/BorderIcon.gif"));
   79           table.put("beaninfo.BorderBevelLowered", LookAndFeel.makeIcon(getClass(), "icons/BorderBevelLowered.gif"));
   80           table.put("beaninfo.BorderBevelRaised", LookAndFeel.makeIcon(getClass(), "icons/BorderBevelRaised.gif"));
   81           table.put("beaninfo.BorderEtched", LookAndFeel.makeIcon(getClass(), "icons/BorderEtched.gif"));
   82           table.put("beaninfo.BorderLine", LookAndFeel.makeIcon(getClass(), "icons/BorderLine.gif"));
   83   	Icon buttonIcon = UIManager.getIcon("beaninfo.BorderIcon");
   84   
   85           borderCombo = createComboBox();
   86   
   87   	// need rigid area match up
   88   	borderButton = new JButton(buttonIcon);
   89   	Dimension d = new Dimension(buttonIcon.getIconWidth(), buttonIcon.getIconHeight());
   90   	borderButton.setMargin(SwingEditorSupport.BUTTON_MARGIN);
   91   
   92   	setAlignment(borderCombo);
   93   	setAlignment(borderButton);
   94   	panel.add(borderCombo);
   95   	panel.add(Box.createRigidArea(new Dimension(5,0)));
   96   	panel.add(borderButton);
   97   	panel.add(Box.createHorizontalGlue());
   98       }
   99   
  100       private void addComponentListeners() {
  101   	borderButton.addActionListener(new ActionListener(){
  102   		public void actionPerformed(ActionEvent e){
  103   		    if (borderDialog == null)
  104   			borderDialog = new BorderDialog(panel.getParent(), "Border Chooser");
  105   		    border = borderDialog.showDialog();
  106   		    if (!(borderDialog.isCancelled()))
  107   			setValue(border);
  108   		}
  109   	    });
  110       }
  111   
  112       public void setValue(Object value){
  113   	super.setValue(value);
  114   	// update our GUI state
  115   	// set ComboBox to any equal border value
  116   	//	borderCombo.setSelectedItem(value);
  117   	// set BorderChooser - setSelectedBorder to any equal value as well
  118       }
  119     
  120       private JComboBox createComboBox(){
  121   	DefaultComboBoxModel model = new DefaultComboBoxModel();
  122   	for (int i = 0; i < 4; i++){
  123   	    model.addElement(new Integer(i));
  124   	}
  125             
  126   	JComboBox c = new JComboBox(model); // borders);
  127   	c.setRenderer(new TestCellRenderer(c));
  128   	c.setPreferredSize(SwingEditorSupport.MEDIUM_DIMENSION); // new Dimension(120,20));
  129   	c.setMinimumSize(SwingEditorSupport.MEDIUM_DIMENSION);
  130   	c.setMaximumSize(SwingEditorSupport.MEDIUM_DIMENSION);
  131   	c.setSelectedIndex(-1);
  132   	c.addActionListener(new ActionListener(){
  133   		public void actionPerformed(ActionEvent e){
  134   		    JComboBox cb = (JComboBox)e.getSource();
  135   		    border = borders[cb.getSelectedIndex()];
  136   		    setValue(border);
  137   		}
  138   	    }); 
  139   	return c;
  140       }
  141   
  142   
  143       class TestCellRenderer extends JLabel implements ListCellRenderer   {
  144   	JComboBox combobox;
  145   	Icon images[] = {
  146   	    UIManager.getIcon("beaninfo.BorderEtched"),                  
  147   	    UIManager.getIcon("beaninfo.BorderBevelLowered"),
  148   	    UIManager.getIcon("beaninfo.BorderBevelRaised"),
  149   	    UIManager.getIcon("beaninfo.BorderLine") };
  150   
  151   	String desc[] = {
  152   	    "Etched",                  
  153   	    "BevelLowered",
  154   	    "BevelRaised",
  155   	    "Line" };
  156             
  157   	public TestCellRenderer(JComboBox x) {
  158   	    this.combobox = x;
  159   	    setOpaque(true);
  160   	}
  161   
  162   	public Component getListCellRendererComponent(JList list, Object value,
  163   						      int modelIndex,
  164   						      boolean isSelected,
  165   						      boolean cellHasFocus) {
  166   	    if (value == null) {
  167   		setText("");
  168   		setIcon(null);
  169   	    } else {
  170   		int index = ((Integer)value).intValue();
  171   		if (index < 0){
  172   		    setText("");
  173   		    setIcon(null);
  174   		} else {
  175   		    String text = " " + desc[index];
  176   		    setIcon(images[index]);                          
  177   		    setText(text);
  178   		    if (isSelected){
  179   			setBackground(UIManager.getColor("ComboBox.selectionBackground"));
  180   			setForeground(UIManager.getColor("ComboBox.selectionForeground"));
  181   		    } else {
  182   			setBackground(UIManager.getColor("ComboBox.background"));
  183   			setForeground(UIManager.getColor("ComboBox.foreground"));
  184   		    }
  185   		}
  186   	    }
  187   	    return this;
  188   	}
  189       }
  190   
  191       class BorderDialog extends JDialog {
  192   	JPanel pane;
  193   	JButton okButton;
  194   	BorderChooser borderChooser;
  195   	Border border = null;
  196   	boolean cancel = false;
  197   
  198   	public BorderDialog(Component c, String title){
  199   	    super(JOptionPane.getFrameForComponent(c), title, true);
  200   	    Container contentPane = getContentPane();
  201   	    pane = new JPanel();
  202   
  203   	    contentPane.setLayout(new BorderLayout());
  204   	    okButton = new JButton("OK"); // new BorderTracker(pane);
  205   	    ActionListener okListener = new ActionListener(){
  206   		    public void actionPerformed(ActionEvent e){
  207   			// get the Border from the pane
  208   			border = getBorder();
  209   		    }
  210   		};
  211   
  212   	    getRootPane().setDefaultButton(okButton);
  213   	    okButton.setActionCommand("OK");
  214   	    if (okListener != null) {
  215   		okButton.addActionListener(okListener);
  216   	    }
  217   	    okButton.addActionListener(new ActionListener() {
  218   		    public void actionPerformed(ActionEvent e) {
  219   			hide();
  220   		    }
  221   		});
  222   	    JButton cancelButton = new JButton("Cancel");
  223   	    cancelButton.setActionCommand("cancel");
  224   	    cancelButton.addActionListener(new ActionListener() {
  225   		    public void actionPerformed(ActionEvent e) {
  226   			cancel = true;
  227   			hide();
  228   		    }
  229   		});
  230   	    // borderlayout
  231   	    addBorderChooser(pane);      
  232   	    pane.add(okButton);
  233   	    pane.add(cancelButton);
  234   	    contentPane.add(pane, BorderLayout.CENTER);
  235   	    pack();
  236   	    this.addWindowListener(new Closer());
  237   	    this.addComponentListener(new DisposeOnClose());
  238   	}
  239   
  240   	public void addBorderChooser(JPanel panel){
  241   	    borderChooser = new BorderChooser();
  242   	    panel.add(borderChooser);
  243   	}
  244   
  245   	public void setBorder(){ // called from pane
  246   	}
  247   
  248   	public Border getBorder(){
  249   	    return borderChooser.getSelectedBorder();
  250   	    //      return this.border;
  251   	}
  252   
  253   	public Border showDialog(){
  254   	    this.cancel = false;
  255   	    this.show();
  256   	    return getBorder(); // border should be ok
  257   	}
  258   
  259   	public boolean isCancelled(){
  260   	    return this.cancel;
  261   	}
  262   
  263   	class Closer extends WindowAdapter {
  264   	    public void windowClosing(WindowEvent e) {
  265   		Window w = e.getWindow();
  266   		w.hide();
  267   	    }
  268   	}
  269   
  270   	class DisposeOnClose extends ComponentAdapter {
  271   	    public void componentHidden(ComponentEvent e) {
  272   		Window w = (Window)e.getComponent();
  273   		w.dispose();
  274   	    }
  275   	}
  276       }
  277   
  278       public static void main(String args[]){
  279   	JFrame f = new JFrame();
  280   	f.addWindowListener(new WindowAdapter(){
  281   		public void windowClosing(WindowEvent e){
  282   		    System.exit(0);
  283   		}
  284   	    });
  285   	SwingBorderEditor editor = new SwingBorderEditor();
  286   	f.getContentPane().add(editor.getCustomEditor());
  287   	f.pack();
  288   	f.show();
  289       }
  290   }

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