View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.property.editors;
21  
22  import java.awt.Component;
23  import java.awt.Font;
24  import java.awt.GraphicsEnvironment;
25  import java.awt.event.ItemEvent;
26  import java.awt.event.ItemListener;
27  
28  import javax.swing.DefaultComboBoxModel;
29  import javax.swing.DefaultListCellRenderer;
30  import javax.swing.JComboBox;
31  import javax.swing.JList;
32  
33  
34  /**
35   * 
36   * To change the template for this generated type comment go to
37   * Window>Preferences>Java>Code Generation>Code and Comments
38   * 
39   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
40   * @version $id$
41   */
42  public class SimpleFontEditor extends JComboBox implements PropertyEditor {
43  	String[] fonts;
44  
45  	private class FontRenderer extends DefaultListCellRenderer {
46  
47  		public Component getListCellRendererComponent(JList list, Object value,
48  				int index, boolean isSelected, boolean cellHasFocus) {
49  			String name = (String) value;
50  			Component c = super.getListCellRendererComponent(list, value,
51  					index, isSelected, cellHasFocus);
52  			if (isSelected || !SimpleFontEditor.this.hasFocus())
53  				SimpleFontEditor.this.setFont(Font.decode(name));
54  			c.setFont(Font.decode(name));
55  			return c;
56  
57  		}
58  
59  	}
60  
61  	public SimpleFontEditor() {
62  		super();
63  		fonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
64  				.getAvailableFontFamilyNames();
65  		setModel(new DefaultComboBoxModel(fonts));
66  		setRenderer(new FontRenderer());
67  		setEditable(false);
68  
69  		addItemListener(new ItemListener() {
70  			Object old = null;
71  
72  			public void itemStateChanged(ItemEvent e) {
73  				if (e.getStateChange() == ItemEvent.DESELECTED) {
74  					old = e.getItem();
75  				} else {
76  					firePropertyChange(PROPERTY_VALUE_NAME, old, e.getItem());
77  				}
78  			}
79  		});
80  	}
81  
82  	public Object getPropertyValue() {
83  		String name = (String) getSelectedItem();
84  		return Font.decode(name);
85  	}
86  
87  	/*
88  	 * (non-Javadoc)
89  	 * 
90  	 * @see com.cosylab.gui.property.PropertyEditor#setPropertyValue(java.lang.Object)
91  	 */
92  	public boolean setPropertyValue(Object value) {
93  		setSelectedItem(((Font) value).getFamily());
94  		return true;
95  	}
96  
97  	/*
98  	 * (non-Javadoc)
99  	 * 
100 	 * @see com.cosylab.gui.property.PropertyEditor#getDescription()
101 	 */
102 	public String getDescription() {
103 		return "Font";
104 	}
105 
106 	/*
107 	 * (non-Javadoc)
108 	 * 
109 	 * @see com.cosylab.gui.property.PropertyEditor#setDescription(java.lang.String)
110 	 */
111 	public void setDescription(String description) {
112 	}
113 
114 }