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   package javax.swing.beaninfo;
   39   
   40   import java.awt.Color;
   41   import java.awt.Component;
   42   import java.awt.Dimension;
   43   import java.awt.Font;
   44   import java.awt.FontMetrics;
   45   import java.awt.Graphics;
   46   import java.awt.GraphicsEnvironment;
   47   import java.awt.Rectangle;
   48   
   49   import java.awt.event.ActionListener;
   50   import java.awt.event.ActionEvent;
   51   
   52   import javax.swing.AbstractButton;
   53   import javax.swing.Box;
   54   import javax.swing.BoxLayout;
   55   import javax.swing.ButtonGroup;
   56   import javax.swing.ButtonModel;
   57   import javax.swing.Icon;
   58   import javax.swing.JButton;
   59   import javax.swing.JComboBox;
   60   import javax.swing.JComponent;
   61   import javax.swing.JLabel;
   62   import javax.swing.JPanel;
   63   import javax.swing.JToggleButton;
   64   
   65   /**
   66    * A Font property editor. Mostly designed by Chris Ryan.
   67    *
   68    * @version 1.6 02/27/02
   69    * @author Tom Santos
   70    * @author Mark Davidson
   71    */
   72   public class SwingFontEditor extends SwingEditorSupport implements ActionListener {
   73   
   74       private static int BUTTON_WIDTH = 20;
   75       private static int BUTTON_HEIGHT = 30;
   76       
   77       private static Dimension buttonSize = new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT);
   78       
   79       private String fonts[];
   80       private static int[] pointSizes = { 3, 5, 8, 10, 12, 14, 18, 24, 36, 48 };
   81       
   82       private int selectedStyle = Font.PLAIN;
   83   
   84       private final static String sampleText = "Abcde...";
   85   
   86       // Controls 
   87       private JComboBox familyNameCombo;
   88       private JComboBox fontSizeCombo;
   89   
   90       private JToggleButton pButton, iButton, bButton;
   91       private FontDisplay iDisplay, pDisplay, bDisplay;
   92       
   93       private JLabel labelDisplay;
   94   	
   95       public SwingFontEditor() {
   96           fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
   97       
   98           pDisplay = new FontDisplay(Font.PLAIN);
   99           pButton = new JToggleButton(pDisplay);
  100           pButton.setToolTipText("plain style");
  101   
  102           iDisplay = new FontDisplay(Font.ITALIC);
  103           iButton = new JToggleButton(iDisplay);
  104           iButton.setToolTipText("italic style");
  105   
  106           bDisplay = new FontDisplay(Font.BOLD);
  107           bButton = new JToggleButton(bDisplay);
  108           bButton.setToolTipText("bold style");
  109   
  110           initializeButton(pButton);
  111           initializeButton(iButton);
  112           initializeButton(bButton);
  113   
  114           ButtonGroup group = new ButtonGroup();
  115           group.add(pButton);
  116           group.add(iButton);
  117           group.add(bButton);
  118       
  119           labelDisplay = new JLabel(fonts[0]);
  120           labelDisplay.setAlignmentX(Component.LEFT_ALIGNMENT);
  121           labelDisplay.setPreferredSize(new Dimension(250,30));
  122           labelDisplay.setMinimumSize(new Dimension(250,30));
  123   
  124           initializeComboBoxes();
  125   
  126   	// Assemble the panel.
  127           JPanel p = new JPanel();
  128           p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
  129           p.add(familyNameCombo);
  130           p.add(Box.createRigidArea(new Dimension(5,0)));    
  131           p.add(fontSizeCombo);
  132           p.add(Box.createRigidArea(new Dimension(5,0)));    
  133   
  134           p.add(pButton);
  135           p.add(iButton);
  136           p.add(bButton);
  137           p.setAlignmentX(Component.LEFT_ALIGNMENT);
  138       
  139           panel = new JPanel();
  140           panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  141           panel.add(p);
  142           panel.add(labelDisplay);
  143       }
  144   
  145       private void initializeButton(JToggleButton b) {
  146           b.setBorderPainted(false);
  147           b.setFocusPainted(false);
  148           b.setContentAreaFilled(false);
  149           
  150           b.setPreferredSize(buttonSize);
  151           b.setMaximumSize(buttonSize);
  152           b.setMinimumSize(buttonSize);
  153           b.addActionListener(this);
  154           setAlignment(b);
  155       }
  156   
  157       /**
  158        * Creates the ComboBoxes. The fons and point sizes must be initialized.
  159        */
  160       private void initializeComboBoxes() {
  161           // ComboBoxes
  162           familyNameCombo = new JComboBox(fonts);
  163           fontSizeCombo = new JComboBox();
  164       
  165           familyNameCombo.setPreferredSize(SwingEditorSupport.MEDIUM_DIMENSION);
  166           familyNameCombo.setMinimumSize(SwingEditorSupport.MEDIUM_DIMENSION);
  167           familyNameCombo.setMaximumSize(SwingEditorSupport.MEDIUM_DIMENSION);
  168           familyNameCombo.addActionListener(this);
  169           setAlignment(familyNameCombo);
  170       
  171   	for (int i = 0; i < pointSizes.length; i++)
  172   	    fontSizeCombo.addItem("" + pointSizes[i]);
  173           
  174           fontSizeCombo.setPreferredSize(SwingEditorSupport.SMALL_DIMENSION);
  175           fontSizeCombo.setMaximumSize(SwingEditorSupport.SMALL_DIMENSION);
  176           fontSizeCombo.setMinimumSize(SwingEditorSupport.SMALL_DIMENSION);
  177           fontSizeCombo.addActionListener(this);
  178           setAlignment(fontSizeCombo);
  179   
  180       }
  181       
  182       /** 
  183        * ActionListener handler for all component events to set the
  184        * value of the PropertyEditor as a result of a change in the
  185        * value by the user.
  186        */
  187       public void actionPerformed(ActionEvent evt)  {
  188   	if (valueSet == true) {
  189   	    // XXX yucky hack! The value is already up to date.
  190   	    // take care of side effects.
  191   	    return;
  192   	}
  193           Object obj = evt.getSource();
  194   	String family = "Dialog";
  195   	int size = 12;
  196   
  197           if (obj instanceof AbstractButton) {
  198               AbstractButton button = (AbstractButton)obj;
  199   	    if (obj == pButton)  {
  200                   selectedStyle = Font.PLAIN;
  201   	    } else if (obj == iButton) {
  202   		selectedStyle = Font.ITALIC;
  203   	    } else if (obj == bButton) {
  204   		selectedStyle = Font.BOLD;
  205   	    }
  206   	    
  207   	    family = (String)familyNameCombo.getSelectedItem();
  208   	    if (fontSizeCombo.getSelectedIndex() != -1) {
  209   		size = pointSizes[fontSizeCombo.getSelectedIndex()];
  210   	    }
  211           }
  212   
  213           if (obj instanceof JComboBox)  {
  214               family = (String)familyNameCombo.getSelectedItem();
  215   	    if (fontSizeCombo.getSelectedIndex() != -1) {
  216   		size = pointSizes[fontSizeCombo.getSelectedIndex()];
  217   	    }
  218           }
  219   	super.setValue(new Font(family, selectedStyle, size));
  220       }
  221   
  222       /** 
  223        * Reconfigure the controls to reflect the current font.
  224        */
  225       private void editorChangeValue(Font font){
  226   	if (font == null) {
  227   	    familyNameCombo.setSelectedIndex(-1);
  228   	    fontSizeCombo.setSelectedIndex(-1);
  229   	    labelDisplay.setText("null");
  230   	    return;
  231   	}
  232   
  233           for (int i = 0; i < fonts.length; i++) {
  234               if (fonts[i].equals(font.getName())) {
  235   		familyNameCombo.setSelectedIndex(i);
  236   		break;
  237               }
  238           }
  239   
  240           for (int i = 0; i < pointSizes.length; i++) {
  241               if (font.getSize() <= pointSizes[i]){
  242   		fontSizeCombo.setSelectedIndex(i);
  243   		break;
  244               }
  245           }
  246   
  247           selectedStyle = font.getStyle();
  248           String style = "";
  249           switch (selectedStyle) {
  250               case Font.PLAIN:
  251                   pButton.setSelected(true);
  252                   style = "Plain";
  253                   break;
  254               case Font.ITALIC:
  255                   iButton.setSelected(true);
  256                   style = "Italic";
  257                   break;
  258               case Font.BOLD:
  259                   bButton.setSelected(true);
  260                   style = "Bold";
  261                   break;
  262           }
  263           String family = font.getFamily();
  264           
  265           iDisplay.setFamily(family);
  266           pDisplay.setFamily(family);
  267           bDisplay.setFamily(family);
  268           
  269           labelDisplay.setFont(font);
  270           labelDisplay.setText(family + ", " + style + ", " + font.getSize());
  271   
  272           panel.revalidate();
  273           panel.repaint();
  274       }
  275   
  276       //
  277       // PropertyEditor interface definitions
  278       //
  279   
  280       // XXX - hack to keep the feedback loop from setting the combo
  281       // boxes from calling the actionPerformed methods.
  282       private boolean valueSet;
  283   
  284       public void setValue(Object value) {
  285       	super.setValue(value);
  286   	valueSet = true;
  287   	editorChangeValue((Font)value);
  288   	valueSet = false;
  289       }
  290   
  291       public boolean isPaintable() {
  292       	return true;
  293       }
  294   
  295       public void paintValue(Graphics g, Rectangle rect) {
  296   	// Silent noop.
  297   	Font oldFont = g.getFont();
  298   	g.setFont((Font)getValue());
  299   	FontMetrics fm = g.getFontMetrics();
  300   	int vpad = (rect.height - fm.getAscent())/2;
  301   	g.drawString(sampleText, 0, rect.height-vpad);
  302   	g.setFont(oldFont);
  303       }
  304   
  305       public String getJavaInitializationString() {
  306       	Font font = (Font)getValue();
  307       
  308           return "new java.awt.Font(\"" + font.getFamily() + "\", " +
  309           font.getStyle() + ", " + font.getSize() + ")";
  310       }
  311   
  312       /** 
  313        * Implementation of a Icon button.
  314        */
  315       private class FontDisplay implements Icon {
  316           private Font font;
  317           
  318           private int style = Font.PLAIN;
  319           private int size = 24;
  320           
  321           private String label = "A";
  322   
  323           private int iconWidth = 20;
  324           private int iconHeight = 30;
  325   
  326           public FontDisplay(int style) {
  327               this.style = style;
  328               this.font = new Font("Dialog", this.style, this.size);
  329           }
  330   
  331           public FontDisplay() {
  332               this.font = new Font("Dialog", this.style, this.size);
  333           }
  334   
  335           public void setFamily(String family) {
  336               this.font = new Font(family, this.style, this.size);
  337           }
  338   		
  339           public void paintIcon(Component c, Graphics g, int x, int y) {
  340               JComponent component = (JComponent)c;
  341               
  342               Font oldFont = g.getFont();
  343               g.setFont(this.font);
  344               if (component instanceof JToggleButton) {
  345   		AbstractButton b= (AbstractButton)component;
  346   		ButtonModel model = b.getModel();
  347   		if (model.isPressed() || model.isSelected())
  348   		    g.setColor(Color.black); // xxx: foreground
  349   		else
  350   		    g.setColor(Color.gray); // xxx: foreground light
  351               }
  352               g.drawString(label, x, (y + iconHeight) - 7);
  353               g.setFont(oldFont);
  354           }
  355   
  356           public int getIconWidth() {
  357               return iconWidth;
  358           }
  359           
  360           public int getIconHeight() {
  361               return iconHeight;
  362           }
  363       } // end class FontDisplay
  364   }

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