Save This Page
Home » openjdk-7 » net.jbeans.bean.property » editor » [javadoc | source]
    1   /* -------------------------------------------------------------------
    2    * Java source file for the class FontEditor
    3    * 
    4    * Copyright (c), 2002, Masahiro Takatsuka.
    5    * All Rights Researved.
    6    * 
    7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
    8    * $Author: takatsukam $
    9    * 
   10    * $Date: 2003/07/25 04:51:45 $
   11    * 
   12    * $Id: FontEditor.java,v 1.1.1.1 2003/07/25 04:51:45 takatsukam Exp $
   13    * 
   14    * Reference:		Document no:
   15    * ___				___
   16    * 
   17    * To Do:
   18    * ___
   19    * 
   20   ------------------------------------------------------------------- */
   21   
   22   /* --------------------------- Package ---------------------------- */
   23   package net.jbeans.bean.property.editor;
   24   
   25   /* ------------------ Import classes (packages) ------------------- */
   26   import java.awt;
   27   import java.awt.event;
   28   import javax.swing;
   29   
   30   /*====================================================================
   31                     Implementation of class FontEditor                  
   32   ====================================================================*/
   33   /**
   34    * A Font property editor.
   35    * 
   36    * @version $Revision: 1.1.1.1 $
   37    * @author Masahiro Takatsuka (masa@jbeans.net)
   38    * @see EditorSupport
   39    * @see ActionListener
   40    */
   41   
   42   public final class FontEditor extends EditorSupport implements ActionListener {
   43       private static final int BUTTON_WIDTH = 20;
   44       private static final int BUTTON_HEIGHT = 30;
   45       
   46       private static Dimension sButtonSize = new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT);
   47       
   48       private String[] fonts;
   49       private static int[] sPointSizes = { 3, 5, 8, 10, 12, 14, 18, 24, 36, 48 };
   50   	private static String[] sStyleStrings = {"plain", "italic", "bold", "bold/italic"};
   51       private static int[] sStyleValues = {Font.PLAIN, Font.ITALIC, Font.BOLD, Font.BOLD | Font.ITALIC, };
   52   	private static final int DEMO_SIZE = 12;
   53   	private static Font[] sStyleFonts = {new Font("Dialog", sStyleValues[0], DEMO_SIZE),
   54   										new Font("Dialog", sStyleValues[1], DEMO_SIZE),
   55   										new Font("Dialog", sStyleValues[2], DEMO_SIZE),
   56   										new Font("Dialog", sStyleValues[3], DEMO_SIZE),
   57   	};
   58   	
   59       private int selectedStyle = Font.PLAIN;
   60       private final static String SAMPLE_TEXT = "Abcde...";
   61   
   62       // Controls 
   63       private JComboBox familyNameCombo;
   64       private JComboBox fontSizeCombo;
   65   	private JComboBox fontStyleCombo;
   66   	
   67       private JToggleButton pButton, iButton, bButton, biButton;
   68       private FontDisplay iDisplay, pDisplay, bDisplay, biDisplay;
   69       
   70       private JLabel labelDisplay;
   71   	
   72       public FontEditor() {
   73           this.fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
   74       
   75           this.pDisplay = new FontDisplay(Font.PLAIN);
   76           this.pButton = new JToggleButton(this.pDisplay);
   77           this.pButton.setToolTipText("plain style");
   78   
   79           this.iDisplay = new FontDisplay(Font.ITALIC);
   80           this.iButton = new JToggleButton(this.iDisplay);
   81           this.iButton.setToolTipText("italic style");
   82   
   83           this.bDisplay = new FontDisplay(Font.BOLD);
   84           this.bButton = new JToggleButton(this.bDisplay);
   85           this.bButton.setToolTipText("bold style");
   86   
   87           this.biDisplay = new FontDisplay(Font.BOLD | Font.ITALIC);
   88           this.biButton = new JToggleButton(this.biDisplay);
   89           this.biButton.setToolTipText("bold/italic style");
   90   		
   91           initializeButton(this.pButton);
   92           initializeButton(this.iButton);
   93           initializeButton(this.bButton);
   94   		initializeButton(this.biButton);
   95   
   96           ButtonGroup group = new ButtonGroup();
   97           group.add(this.pButton);
   98           group.add(this.iButton);
   99           group.add(this.bButton);
  100   		group.add(this.biButton);
  101       
  102           // ComboBoxes
  103           this.familyNameCombo = new JComboBox();
  104           this.fontSizeCombo = new JComboBox();
  105   		this.fontStyleCombo = new JComboBox();
  106   		
  107           this.labelDisplay = new JLabel(this.fonts[0]);
  108           this.labelDisplay.setAlignmentX(Component.LEFT_ALIGNMENT);
  109           this.labelDisplay.setPreferredSize(new Dimension(250,30));
  110           this.labelDisplay.setMinimumSize(new Dimension(250,30));
  111   
  112           initializeComboBoxes();
  113   
  114   		// Assemble the panel.
  115           JPanel p = new JPanel();
  116           p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
  117           p.add(this.familyNameCombo);
  118           p.add(Box.createRigidArea(new Dimension(5,0)));    
  119           p.add(this.fontSizeCombo);
  120           p.add(Box.createRigidArea(new Dimension(5,0)));    
  121           p.add(this.fontStyleCombo);
  122           p.add(Box.createRigidArea(new Dimension(5,0)));    
  123   
  124   //          p.add(_pButton);
  125   //          p.add(_iButton);
  126   //          p.add(_bButton);
  127   //  		p.add(_biButton);		
  128           p.setAlignmentX(Component.LEFT_ALIGNMENT);
  129       
  130           JPanel panel = new JPanel();
  131           panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  132           panel.add(p);
  133           panel.add(this.labelDisplay);
  134   		setPanel(panel);
  135       }
  136   
  137       private void initializeButton(JToggleButton b) {
  138           b.setBorderPainted(false);
  139           b.setFocusPainted(false);
  140           b.setContentAreaFilled(false);
  141           
  142           b.setPreferredSize(sButtonSize);
  143           b.setMaximumSize(sButtonSize);
  144           b.setMinimumSize(sButtonSize);
  145           b.addActionListener(this);
  146           setAlignment(b);
  147       }
  148   
  149       private void initializeComboBoxes() {
  150           for (int i = 0; i < this.fonts.length; i++)
  151               this.familyNameCombo.addItem(this.fonts[i]);
  152   
  153           this.familyNameCombo.setPreferredSize(EditorSupport.MEDIUM_DIMENSION);
  154           this.familyNameCombo.setMinimumSize(EditorSupport.MEDIUM_DIMENSION);
  155           this.familyNameCombo.setMaximumSize(EditorSupport.MEDIUM_DIMENSION);
  156           this.familyNameCombo.addActionListener(this);
  157           setAlignment(this.familyNameCombo);
  158       
  159           for (int i = 0; i < sPointSizes.length; i++)
  160               this.fontSizeCombo.addItem("" + sPointSizes[i]);
  161           
  162           this.fontSizeCombo.setPreferredSize(EditorSupport.SMALL_DIMENSION);
  163           this.fontSizeCombo.setMaximumSize(EditorSupport.SMALL_DIMENSION);
  164           this.fontSizeCombo.setMinimumSize(EditorSupport.SMALL_DIMENSION);
  165           this.fontSizeCombo.addActionListener(this);
  166           setAlignment(this.fontSizeCombo);
  167   
  168           for (int i = 0; i < sStyleStrings.length; i++) {
  169               this.fontStyleCombo.addItem(sStyleStrings[i]);
  170   		}
  171           
  172           this.fontStyleCombo.setPreferredSize(EditorSupport.MEDIUM_DIMENSION);
  173           this.fontStyleCombo.setMaximumSize(EditorSupport.MEDIUM_DIMENSION);
  174           this.fontStyleCombo.setMinimumSize(EditorSupport.MEDIUM_DIMENSION);
  175           this.fontStyleCombo.addActionListener(this);
  176   		this.fontStyleCombo.setRenderer(new FontStyleRenderer());
  177           setAlignment(this.fontStyleCombo);
  178       }
  179   
  180   	final class FontStyleRenderer extends JLabel implements ListCellRenderer {
  181   		public FontStyleRenderer() {
  182   			setOpaque(false);
  183   			setVerticalAlignment(CENTER);
  184   		}
  185   		
  186   		public final Component getListCellRendererComponent(JList list,
  187   													  Object value,
  188   													  int index,
  189   													  boolean isSelected,
  190   													  boolean cellHasFocus) {
  191   			if (index >= 0) {
  192   				setFont(sStyleFonts[index]);
  193   			}
  194   			setText(value.toString());
  195   			return this;
  196   		}
  197   	}   
  198   
  199   
  200       /** 
  201        * ActionListener handler for all component events.
  202        */
  203       public final void actionPerformed(ActionEvent evt)  {
  204           Object obj = evt.getSource();
  205   
  206           if (obj instanceof AbstractButton) {
  207               AbstractButton button = (AbstractButton)obj;
  208   	        if (obj == this.pButton)  {
  209                   this.selectedStyle = Font.PLAIN;
  210   	        } else if (obj == this.iButton) {
  211   	            this.selectedStyle = Font.ITALIC;
  212   	        } else if (obj == this.bButton) {
  213   	            this.selectedStyle = Font.BOLD;
  214   	        } else if (obj == this.biButton) {
  215   	            this.selectedStyle = (Font.BOLD | Font.ITALIC);
  216   	        }
  217   
  218   	        String family = (String)this.familyNameCombo.getSelectedItem();
  219   	        int size = sPointSizes[this.fontSizeCombo.getSelectedIndex()];
  220   			
  221   	        setValue(new Font(family, this.selectedStyle, size));
  222           }
  223   
  224           if (obj instanceof JComboBox)  {
  225               String family = (String)this.familyNameCombo.getSelectedItem();
  226               int size = sPointSizes[this.fontSizeCombo.getSelectedIndex()];
  227               this.selectedStyle = sStyleValues[this.fontStyleCombo.getSelectedIndex()];
  228   				
  229   			setValue(new Font(family, this.selectedStyle, size)); 
  230           }
  231       }
  232   
  233       /** 
  234        * Reconfigure the controls to reflect the current font.
  235        */
  236       private void editorChangeValue(Font font){
  237   		if (font == null) {
  238   			return;
  239   		}
  240           for (int i = 0; i < this.fonts.length; i++) {
  241               if (this.fonts[i].equals(font.getName())) {
  242   	            this.familyNameCombo.setSelectedIndex(i);
  243           	    break;
  244               }
  245           }
  246   
  247           for (int i = 0; i < sPointSizes.length; i++) {
  248               if (font.getSize() <= sPointSizes[i]){
  249   	            this.fontSizeCombo.setSelectedIndex(i);
  250   	            break;
  251               }
  252           }
  253   
  254   		this.selectedStyle = font.getStyle();
  255           for (int i = 0; i < sStyleValues.length; i++) {
  256               if (sStyleValues[i] == this.selectedStyle) {
  257   	            this.fontStyleCombo.setSelectedIndex(i);
  258           	    break;
  259               }
  260           }
  261   
  262           String style = "";
  263           switch (this.selectedStyle) {
  264               case Font.PLAIN:
  265                   this.pButton.setSelected(true);
  266                   style = "Plain";
  267                   break;
  268               case Font.ITALIC:
  269                   this.iButton.setSelected(true);
  270                   style = "Italic";
  271                   break;
  272               case Font.BOLD:
  273                   this.bButton.setSelected(true);
  274                   style = "Bold";
  275                   break;
  276           }
  277           String family = font.getFamily();
  278           
  279           this.iDisplay.setFamily(family);
  280           this.pDisplay.setFamily(family);
  281           this.bDisplay.setFamily(family);
  282           this.biDisplay.setFamily(family);		
  283           
  284           this.labelDisplay.setFont(font);
  285           this.labelDisplay.setText(family + ", " + style + ", " + font.getSize());
  286   
  287           //  getPanel().revalidate();
  288           getPanel().repaint();
  289       }
  290   
  291       //
  292       // PropertyEditor interface definitions
  293       //
  294   
  295       public final void setValue(Object value) {
  296       	super.setValue(value);
  297   	    editorChangeValue((Font)value);
  298       }
  299   
  300       public final boolean isPaintable() {
  301       	return true;
  302       }
  303   
  304       public final void paintValue(Graphics g, Rectangle rect) {
  305   	    // Silent noop.
  306   	    Font oldFont = g.getFont();
  307   	    g.setFont((Font)getValue());
  308   	    FontMetrics fm = g.getFontMetrics();
  309   	    int vpad = (rect.height - fm.getAscent())/2;
  310   	    g.drawString(SAMPLE_TEXT, 0, rect.height-vpad);
  311   	    g.setFont(oldFont);
  312       }
  313   
  314       public final String getJavaInitializationString() {
  315       	Font font = (Font)getValue();
  316       
  317           return "new java.awt.Font(\"" + font.getFamily() + "\", " +
  318           font.getStyle() + ", " + font.getSize() + ")";
  319       }
  320   
  321       /** 
  322        * Implementation of a Icon button.
  323        */
  324       final class FontDisplay implements Icon {
  325           private Font font;
  326           
  327           private int style;
  328           private int size = 24;
  329           
  330           private String label;
  331   
  332           private int iconWidth = 20;
  333           private int iconHeight = 30;
  334   
  335           public FontDisplay(int style, String label) {
  336               this.style = style;
  337   			this.label = label;
  338               this.font = new Font("Dialog", this.style, this.size);
  339           }
  340   
  341   		public FontDisplay(int style) {
  342   			this(style, "A");
  343   		}
  344   					
  345           public FontDisplay() {
  346   			this(Font.PLAIN);
  347           }
  348   
  349           public final void setFamily(String family) {
  350               this.font = new Font(family, this.style, this.size);
  351           }
  352   		
  353           public final void paintIcon(Component c, Graphics g, int x, int y) {
  354               JComponent component = (JComponent)c;
  355               
  356               Font oldFont = g.getFont();
  357               g.setFont(this.font);
  358               if (component instanceof JToggleButton) {
  359   	            AbstractButton b= (AbstractButton)component;
  360   	            ButtonModel model = b.getModel();
  361   	            if (model.isPressed() || model.isSelected()) {
  362       	            g.setColor(Color.black); // xxx: foreground
  363   				} else {
  364   	                g.setColor(Color.gray); // xxx: foreground light
  365   				}
  366               }
  367               g.drawString(this.label, x, (y + this.iconHeight) - 7);
  368               g.setFont(oldFont);
  369           }
  370   
  371           public final int getIconWidth() {
  372               return this.iconWidth;
  373           }
  374           
  375           public final int getIconHeight() {
  376               return this.iconHeight;
  377           }
  378       } // end class FontDisplay
  379   }

Save This Page
Home » openjdk-7 » net.jbeans.bean.property » editor » [javadoc | source]