1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40
41
42
43 public class FontEditor extends SimpleComboBox implements PropertyEditor {
44 private class FontEditorUI extends SimpleComboBox.DropComboBoxUI {
45
46
47
48 protected SimpleButton getDropButton() {
49 return extendButton;
50 }
51 }
52
53 private class FontRenderer extends DefaultListCellRenderer {
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
77
78 setFont(Font.decode(name));
79
80 if (!isPopupVisible()) {
81 setBackground(FontEditor.this.getBackground());
82 }
83
84 return this;
85 }
86 }
87
88
89 public static final int MODE_BOTH = 0;
90
91
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
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
118
119 public void setDescription(String description) {
120 }
121
122
123
124
125 public String getDescription() {
126 return null;
127 }
128
129
130
131
132
133
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
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
171
172
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
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
196
197
198
199 }
200 });
201 setMode(MODE_BOTH);
202 }
203
204 private void initializeComboListeners() {
205 }
206 }
207
208