1
15 package gate.gui;
16
17 import java.awt.*;
18 import java.awt.event.*;
19
20 import javax.swing.*;
21 import javax.swing.plaf.FontUIResource;
22
23 import gate.Gate;
24 import gate.GateConstants;
25 import gate.swing.JFontChooser;
26
27 public class AppearanceDialog extends JDialog {
28
29 public AppearanceDialog(Frame owner, String title, boolean modal,
30 Component[] targets) {
31 super(owner, title, modal);
32 this.targets = targets;
33 init();
34 }
36 public AppearanceDialog(Dialog owner, String title, boolean modal,
37 Component[] targets) {
38 super(owner, title, modal);
39 this.targets = targets;
40 init();
41 }
43 protected void init() {
44 initLocalData();
45 initGuiComponents();
46 initListeners();
47 bGroup.setSelected(menusRBtn.getModel(), true);
48 cancelBtn.getAction().actionPerformed(null);
49 }
50
51 protected void initLocalData() {
52 Font font = Gate.getUserConfig().getFont(GateConstants.MENUS_FONT);
53 oldMenusFont = menusFont = font == null ?
54 UIManager.getFont("Menu.font") :
55 font;
56
57 font = Gate.getUserConfig().getFont(GateConstants.OTHER_COMPONENTS_FONT);
58 oldComponentsFont = componentsFont = font == null ?
59 UIManager.getFont("Button.font"):
60 font;
61
62 font = Gate.getUserConfig().getFont(GateConstants.TEXT_COMPONENTS_FONT);
63 oldTextComponentsFont = textComponentsFont =
64 font == null ? UIManager.getFont("TextPane.font") : font;
65 }
67 protected void initGuiComponents() {
68 getContentPane().setLayout(new BoxLayout(getContentPane(),
69 BoxLayout.Y_AXIS));
70 Box box = Box.createHorizontalBox();
72 Box tempBox = Box.createVerticalBox();
73 bGroup = new ButtonGroup();
74 menusRBtn = new JRadioButton("Menus", false);
75 menusRBtn.setActionCommand("menus");
76 bGroup.add(menusRBtn);
77 tempBox.add(menusRBtn);
78 componentsRBtn = new JRadioButton("Components", false);
79 componentsRBtn.setActionCommand("components");
80 bGroup.add(componentsRBtn);
81 tempBox.add(componentsRBtn);
82 textComponentsRBtn = new JRadioButton("Text components", false);
83 textComponentsRBtn.setActionCommand("text components");
84 bGroup.add(textComponentsRBtn);
85 tempBox.add(textComponentsRBtn);
86 box.add(tempBox);
87 box.add(Box.createHorizontalGlue());
88 getContentPane().add(box);
89
90 fontChooser = new JFontChooser();
92 getContentPane().add(fontChooser);
93
94 box = Box.createHorizontalBox();
96 okBtn = new JButton(new OKAction());
97 box.add(okBtn);
98 cancelBtn = new JButton(new CancelAction());
99 box.add(cancelBtn);
100 applyBtn = new JButton(new ApplyAction());
101 box.add(applyBtn);
102 getContentPane().add(box);
103
104 setResizable(false);
105
106 }
108 protected void initListeners() {
109 fontChooser.addComponentListener(new ComponentAdapter() {
110 public void componentResized(ComponentEvent e) {
111 pack();
112 }
113 });
114
115 menusRBtn.addActionListener(new ActionListener() {
116 public void actionPerformed(ActionEvent e) {
117 if(menusRBtn.isSelected()) fontChooser.setFontValue(menusFont);
118 } });
120
121 componentsRBtn.addActionListener(new ActionListener() {
122 public void actionPerformed(ActionEvent e) {
123 if(componentsRBtn.isSelected())
124 fontChooser.setFontValue(componentsFont);
125 } });
127
128 textComponentsRBtn.addActionListener(new ActionListener() {
129 public void actionPerformed(ActionEvent e) {
130 if(textComponentsRBtn.isSelected())
131 fontChooser.setFontValue(textComponentsFont);
132 } });
134 }
136 public void show(Component[] targets) {
137 this.targets = targets;
138 oldMenusFont = menusFont = UIManager.getFont("Menu.font");
139 oldComponentsFont = componentsFont = UIManager.getFont("Button.font");
140 oldTextComponentsFont = textComponentsFont =
141 UIManager.getFont("TextPane.font");
142 super.setVisible(true);
143 }
145
146 protected static void setUIDefaults(Object[] keys, Object value) {
147 for(int i = 0; i < keys.length; i++){
148 UIManager.put(keys[i], value);
149 }
150 }
152
156 public static void setTextComponentsFont(Font textComponentsFont){
157 setUIDefaults(textComponentsKeys, new FontUIResource(textComponentsFont));
158 Gate.getUserConfig().put(GateConstants.TEXT_COMPONENTS_FONT,
159 textComponentsFont);
160 }
161
162
166 public static void setMenuComponentsFont(Font menuComponentsFont){
167 setUIDefaults(menuKeys, new FontUIResource(menuComponentsFont));
168 Gate.getUserConfig().put(GateConstants.MENUS_FONT,
169 menuComponentsFont);
170 }
171
172
176 public static void setComponentsFont(Font componentsFont){
177 setUIDefaults(componentsKeys, new FontUIResource(componentsFont));
178 Gate.getUserConfig().put(GateConstants.OTHER_COMPONENTS_FONT,
179 componentsFont);
180 }
181
182
185 public static void main(String[] args) {
186 try {
187 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
188 } catch(Exception e){
189 e.printStackTrace();
190 }
191
192 JFrame frame = new JFrame("Foo frame");
193 final AppearanceDialog apperanceDialog1 = new AppearanceDialog(frame,
194 "Font appearance",
195 true,
196 new Component[]{frame});
197 apperanceDialog1.pack();
198
199 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
200 JButton btn = new JButton("Show dialog");
201 btn.addActionListener(new ActionListener() {
202 public void actionPerformed(ActionEvent e) {
203 apperanceDialog1.setVisible(true);
204 }
205 });
206
207 frame.getContentPane().add(btn);
208 frame.setSize(new Dimension(300, 300));
209 frame.setVisible(true);
210 }
212 JRadioButton menusRBtn;
213 JRadioButton componentsRBtn;
214 JRadioButton textComponentsRBtn;
215 JFontChooser fontChooser;
216
217 JButton okBtn;
218 JButton applyBtn;
219 JButton cancelBtn;
220 ButtonGroup bGroup;
221
222 Font menusFont;
223 Font componentsFont;
224 Font textComponentsFont;
225
226 Font oldMenusFont;
227 Font oldComponentsFont;
228 Font oldTextComponentsFont;
229
230
234 String currentFont;
235 Component[] targets;
236
237 public static String[] menuKeys = new String[]{"CheckBoxMenuItem.acceleratorFont",
238 "CheckBoxMenuItem.font",
239 "Menu.acceleratorFont",
240 "Menu.font",
241 "MenuBar.font",
242 "MenuItem.acceleratorFont",
243 "MenuItem.font",
244 "RadioButtonMenuItem.acceleratorFont",
245 "RadioButtonMenuItem.font"};
246
247 public static String[] componentsKeys =
248 new String[]{"Button.font",
249 "CheckBox.font",
250 "ColorChooser.font",
251 "ComboBox.font",
252 "InternalFrame.titleFont",
253 "Label.font",
254 "List.font",
255 "OptionPane.font",
256 "Panel.font",
257 "PasswordField.font",
258 "PopupMenu.font",
259 "ProgressBar.font",
260 "RadioButton.font",
261 "ScrollPane.font",
262 "TabbedPane.font",
263 "Table.font",
264 "TableHeader.font",
265 "TitledBorder.font",
266 "ToggleButton.font",
267 "ToolBar.font",
268 "ToolTip.font",
269 "Tree.font",
270 "Viewport.font"};
271
272 public static String[] textComponentsKeys =
273 new String[]{"EditorPane.font",
274 "TextArea.font",
275 "TextField.font",
276 "TextPane.font"};
277
278 class ApplyAction extends AbstractAction{
279 ApplyAction(){
280 super("Apply");
281 }
282
283 public void actionPerformed(ActionEvent evt) {
284 setMenuComponentsFont(menusFont);
285 setComponentsFont(componentsFont);
286 setTextComponentsFont(textComponentsFont);
287 SwingUtilities.updateComponentTreeUI(AppearanceDialog.this);
288 for(int i = 0; i< targets.length; i++){
289 if(targets[i] instanceof Window) {
290 SwingUtilities.updateComponentTreeUI(targets[i]);
291 } else {
292 SwingUtilities.updateComponentTreeUI(
293 SwingUtilities.getRoot(targets[i])
294 );
295 }
296 }
297 } }
299
300 class OKAction extends AbstractAction {
301 OKAction(){
302 super("OK");
303 }
304
305 public void actionPerformed(ActionEvent evt){
306 applyBtn.getAction().actionPerformed(evt);
307 setVisible(false);
308 }
309 }
311 class CancelAction extends AbstractAction {
312 CancelAction(){
313 super("Cancel");
314 }
315
316 public void actionPerformed(ActionEvent evt){
317 setUIDefaults(menuKeys, new FontUIResource(oldMenusFont));
318 setUIDefaults(componentsKeys, new FontUIResource(oldComponentsFont));
319 setUIDefaults(textComponentsKeys, new FontUIResource(oldTextComponentsFont));
320 SwingUtilities.updateComponentTreeUI(
321 SwingUtilities.getRoot(AppearanceDialog.this));
322 for(int i = 0; i< targets.length; i++){
323 if(targets[i] instanceof Window){
324 SwingUtilities.updateComponentTreeUI(targets[i]);
325 } else {
326 SwingUtilities.updateComponentTreeUI(
327 SwingUtilities.getRoot(targets[i])
328 );
329 }
330 }
331 setVisible(false);
332 } }
335 }