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.ActionEvent;
26  import java.awt.event.ActionListener;
27  
28  import javax.swing.DefaultComboBoxModel;
29  import javax.swing.DefaultListCellRenderer;
30  import javax.swing.JList;
31  
32  import com.cosylab.gui.components.SimpleButton;
33  import com.cosylab.gui.components.SimpleComboBox;
34  import com.cosylab.gui.components.util.CosyUIElements;
35  import com.cosylab.gui.components.util.FontHelper;
36  
37  /**
38   * A property editor for selecting Fonts using a combo box.
39   * 
40   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
41   * @version $id$
42   */
43  public class FontEditor extends SimpleComboBox implements PropertyEditor {
44  	private class FontEditorUI extends SimpleComboBox.DropComboBoxUI {
45  		/**
46  		 * @see com.cosylab.gui.components.SimpleComboBox.DropComboBoxUI#getDropButton()
47  		 */
48  		protected SimpleButton getDropButton() {
49  			return extendButton;
50  		}
51  	}
52  
53  	private class FontRenderer extends DefaultListCellRenderer {
54  		/**
55  		 * Returns a component displaying the font's name by using font itself.
56  		 * 
57  		 * @param list
58  		 *            containing values to be rendered
59  		 * @param value
60  		 *            to be rednered (Font)
61  		 * @param index
62  		 *            of value
63  		 * @param isSelected
64  		 *            true if renderer is selected
65  		 * @param cellHasFocus
66  		 *            tue if renderer has focus
67  		 * 
68  		 * @return a label displaying the Font
69  		 */
70  		public Component getListCellRendererComponent(JList list, Object value,
71  				int index, boolean isSelected, boolean cellHasFocus) {
72  			String name = (String) value;
73  			super.getListCellRendererComponent(list, value, index, isSelected,
74  					cellHasFocus);
75  
76  			// if (isSelected || !FontEditor.this.hasFocus())
77  			// FontEditor.this.setFont(Font.decode(name));
78  			setFont(Font.decode(name));
79  
80  			if (!isPopupVisible()) {
81  				setBackground(FontEditor.this.getBackground());
82  			}
83  
84  			return this;
85  		}
86  	}
87  
88  	/** Both drop-down selection and button for the dialog. */
89  	public static final int MODE_BOTH = 0;
90  
91  	/** Only the drop-down selection */
92  	public static final int MODE_COMBO = 1;
93  
94  	private Font value = null;
95  
96  	private SimpleButton extendButton = null;
97  
98  	private String[] fonts;
99  
100 	private int mode = -1;
101 
102 	/**
103 	 * Creates a new FontEditor object.
104 	 */
105 	public FontEditor() {
106 		super();
107 		fonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
108 				.getAvailableFontFamilyNames();
109 		setModel(new DefaultComboBoxModel(fonts));
110 		setRenderer(new FontRenderer());
111 		setEditable(false);
112 		initialize();
113 		setPropertyValue(FontHelper.getDefaultFont().getFamily());
114 	}
115 
116 	/**
117 	 * @see com.cosylab.gui.property.editors.PropertyEditor#setDescription(java.lang.String)
118 	 */
119 	public void setDescription(String description) {
120 	}
121 
122 	/**
123 	 * @see com.cosylab.gui.property.editors.PropertyEditor#getDescription()
124 	 */
125 	public String getDescription() {
126 		return null;
127 	}
128 
129 	/**
130 	 * Sets the ComboBox mode. Overriden to implement FontEditorUI UI.
131 	 * 
132 	 * @param mode
133 	 *            to be set.
134 	 */
135 	public void setMode(int mode) {
136 		if (mode != this.mode) {
137 			int old = this.mode;
138 			this.mode = mode;
139 
140 			if (mode == MODE_BOTH) {
141 				setUI(new FontEditorUI());
142 			} else if (mode == MODE_COMBO) {
143 				setStyle(SimpleComboBox.DROP_DOWN_STYLE);
144 			}
145 
146 			firePropertyChange("mode", old, mode);
147 		}
148 	}
149 
150 	/**
151 	 * @see com.cosylab.gui.property.editors.PropertyEditor#setPropertyValue(java.lang.Object)
152 	 */
153 	public boolean setPropertyValue(Object value) {
154 		if (value instanceof Font) {
155 			Font daNew = (Font) value;
156 			Font old = this.value;
157 			this.value = daNew;
158 			setFont(daNew);
159 			setSelectedItem(((Font) value).getFamily());
160 			extendButton.repaint();
161 			firePropertyChange(PROPERTY_VALUE_NAME, old, daNew);
162 
163 			return true;
164 		}
165 
166 		return false;
167 	}
168 
169 	/**
170 	 * Returns the selected font.
171 	 * 
172 	 * @return the selected font.
173 	 */
174 	public Object getPropertyValue() {
175 		return value;
176 	}
177 
178 	private void initialize() {
179 		setBorder(CosyUIElements.getPlainBorder(false));
180 		addActionListener(new ActionListener() {
181 			/**
182 			 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
183 			 */
184 			public void actionPerformed(ActionEvent e) {
185 				setPropertyValue(Font.decode((String) getSelectedItem()));
186 			}
187 		});
188 		initializeComboListeners();
189 		extendButton = new SimpleButton("...");
190 		extendButton.setBorder(CosyUIElements.getPlainBorder(true));
191 		extendButton.setPressedBorder(null);
192 		extendButton.setActionMode(SimpleButton.FAST_ACTION_MODE);
193 		extendButton.addActionListener(new ActionListener() {
194 			public void actionPerformed(ActionEvent e) {
195 				// TODO implement fontChooser dialog
196 				// Font c = FontChooser.showDialog(FontEditor.this, "Choose the
197 				// desired font", value);
198 				// setPropertyValue(c);
199 			}
200 		});
201 		setMode(MODE_BOTH);
202 	}
203 
204 	private void initializeComboListeners() {
205 	}
206 }
207 
208 /* __oOo__ */