1   /*  ApperanceDialog.java
2    *
3    *  Copyright (c) 1998-2005, The University of Sheffield.
4    *
5    *  This file is part of GATE (see http://gate.ac.uk/), and is free
6    *  software, licenced under the GNU Library General Public License,
7    *  Version 2, June 1991 (in the distribution as file licence.html,
8    *  and also available at http://gate.ac.uk/gate/licence.html).
9    *
10   *  Valentin Tablan 12/04/2001
11   *
12   *  $Id: AppearanceDialog.java,v 1.7 2005/01/11 13:51:34 ian Exp $
13   *
14   */
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    }// ApperanceDialog
35  
36    public AppearanceDialog(Dialog owner, String title, boolean modal,
37                           Component[] targets) {
38      super(owner, title, modal);
39      this.targets = targets;
40      init();
41    }//ApperanceDialog
42  
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    }// initLocalData()
66  
67    protected void initGuiComponents() {
68      getContentPane().setLayout(new BoxLayout(getContentPane(),
69                                               BoxLayout.Y_AXIS));
70      //add the radio buttons
71      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      //add the font chooser
91      fontChooser = new JFontChooser();
92      getContentPane().add(fontChooser);
93  
94      //add the buttons
95      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   }// initGuiComponents()
107 
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       }// public void actionPerformed(ActionEvent e)
119     });
120 
121     componentsRBtn.addActionListener(new ActionListener() {
122       public void actionPerformed(ActionEvent e) {
123         if(componentsRBtn.isSelected())
124           fontChooser.setFontValue(componentsFont);
125       }// public void actionPerformed(ActionEvent e)
126     });
127 
128     textComponentsRBtn.addActionListener(new ActionListener() {
129       public void actionPerformed(ActionEvent e) {
130         if(textComponentsRBtn.isSelected())
131           fontChooser.setFontValue(textComponentsFont);
132       }// public void actionPerformed(ActionEvent e)
133     });
134   }// initListeners()
135 
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   }// show(Component[] targets)
144 
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   }// setUIDefaults(Object[] keys, Object value)
151 
152   /**
153    * Updates the Swing defaults table with the provided font to be used for the
154    * text components
155    */
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   /**
163    * Updates the Swing defaults table with the provided font to be used for the
164    * menu components
165    */
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   /**
173    * Updates the Swing defaults table with the provided font to be used for
174    * various compoents that neither text or menu components
175    */
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   /**
183    * Test code
184    */
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   }// public static void main(String[] args)
211 
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   /**
231    * Which font is being edited now. Possible vlues: "menu", "text",
232    * "components".
233    */
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     }// void actionPerformed(ActionEvent evt)
298   }
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   }// class OKAction extends AbstractAction
310 
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     }// void actionPerformed(ActionEvent evt)
333   }// class CancelAction extends AbstractAction
334 
335 }// class ApperanceDialog