AppearanceDialog.java
001 /*  ApperanceDialog.java
002  *
003  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
004  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
005  *
006  *  This file is part of GATE (see http://gate.ac.uk/), and is free
007  *  software, licenced under the GNU Library General Public License,
008  *  Version 2, June 1991 (in the distribution as file licence.html,
009  *  and also available at http://gate.ac.uk/gate/licence.html).
010  *
011  *  Valentin Tablan 12/04/2001
012  *
013  *  $Id: AppearanceDialog.java 12943 2010-08-09 10:14:54Z thomas_heitz $
014  *
015  */
016 package gate.gui;
017 
018 import java.awt.*;
019 import java.awt.event.*;
020 
021 import javax.swing.*;
022 import javax.swing.plaf.FontUIResource;
023 
024 import gate.Gate;
025 import gate.GateConstants;
026 import gate.swing.JFontChooser;
027 
028 /**
029  * No more used and replaced by {@link OptionsDialog}.
030  @deprecated
031  */
032 public class AppearanceDialog extends JDialog {
033 
034   public AppearanceDialog(Frame owner, String title, boolean modal,
035                          Component[] targets) {
036     super(owner, title, modal);
037     this.targets = targets;
038     init();
039   }// ApperanceDialog
040 
041   public AppearanceDialog(Dialog owner, String title, boolean modal,
042                          Component[] targets) {
043     super(owner, title, modal);
044     this.targets = targets;
045     init();
046   }//ApperanceDialog
047 
048   protected void init() {
049     initLocalData();
050     initGuiComponents();
051     initListeners();
052     bGroup.setSelected(menusRBtn.getModel()true);
053     cancelBtn.getAction().actionPerformed(null);
054   }
055 
056   protected void initLocalData() {
057     Font font = Gate.getUserConfig().getFont(GateConstants.MENUS_FONT);
058     oldMenusFont = menusFont = font == null ?
059                                UIManager.getFont("Menu.font":
060                                font;
061 
062     font = Gate.getUserConfig().getFont(GateConstants.OTHER_COMPONENTS_FONT);
063     oldComponentsFont = componentsFont = font == null ?
064                                          UIManager.getFont("Button.font"):
065                                          font;
066 
067     font = Gate.getUserConfig().getFont(GateConstants.TEXT_COMPONENTS_FONT);
068     oldTextComponentsFont = textComponentsFont =
069           font == null ? UIManager.getFont("TextPane.font": font;
070   }// initLocalData()
071 
072   protected void initGuiComponents() {
073     getContentPane().setLayout(new BoxLayout(getContentPane(),
074                                              BoxLayout.Y_AXIS));
075     //add the radio buttons
076     Box box = Box.createHorizontalBox();
077     Box tempBox = Box.createVerticalBox();
078     bGroup = new ButtonGroup();
079     menusRBtn = new JRadioButton("Menus"false);
080     menusRBtn.setActionCommand("menus");
081     bGroup.add(menusRBtn);
082     tempBox.add(menusRBtn);
083     componentsRBtn = new JRadioButton("Components"false);
084     componentsRBtn.setActionCommand("components");
085     bGroup.add(componentsRBtn);
086     tempBox.add(componentsRBtn);
087     textComponentsRBtn = new JRadioButton("Text components"false);
088     textComponentsRBtn.setActionCommand("text components");
089     bGroup.add(textComponentsRBtn);
090     tempBox.add(textComponentsRBtn);
091     box.add(tempBox);
092     box.add(Box.createHorizontalGlue());
093     getContentPane().add(box);
094 
095     //add the font chooser
096     fontChooser = new JFontChooser();
097     getContentPane().add(fontChooser);
098 
099     //add the buttons
100     box = Box.createHorizontalBox();
101     okBtn = new JButton(new OKAction());
102     box.add(okBtn);
103     cancelBtn = new JButton(new CancelAction());
104     box.add(cancelBtn);
105     applyBtn = new JButton(new ApplyAction());
106     box.add(applyBtn);
107     getContentPane().add(box);
108 
109     setResizable(false);
110 
111   }// initGuiComponents()
112 
113   protected void initListeners() {
114     fontChooser.addComponentListener(new ComponentAdapter() {
115       public void componentResized(ComponentEvent e) {
116         pack();
117      }
118     });
119 
120     menusRBtn.addActionListener(new ActionListener() {
121       public void actionPerformed(ActionEvent e) {
122         if(menusRBtn.isSelected()) fontChooser.setFontValue(menusFont);
123       }// public void actionPerformed(ActionEvent e)
124     });
125 
126     componentsRBtn.addActionListener(new ActionListener() {
127       public void actionPerformed(ActionEvent e) {
128         if(componentsRBtn.isSelected())
129           fontChooser.setFontValue(componentsFont);
130       }// public void actionPerformed(ActionEvent e)
131     });
132 
133     textComponentsRBtn.addActionListener(new ActionListener() {
134       public void actionPerformed(ActionEvent e) {
135         if(textComponentsRBtn.isSelected())
136           fontChooser.setFontValue(textComponentsFont);
137       }// public void actionPerformed(ActionEvent e)
138     });
139   }// initListeners()
140 
141   public void show(Component[] targets) {
142     this.targets = targets;
143     oldMenusFont = menusFont = UIManager.getFont("Menu.font");
144     oldComponentsFont = componentsFont = UIManager.getFont("Button.font");
145     oldTextComponentsFont = textComponentsFont =
146                             UIManager.getFont("TextPane.font");
147     super.setVisible(true);
148   }// show(Component[] targets)
149 
150 
151   protected static void setUIDefaults(Object[] keys, Object value) {
152     for(int i = 0; i < keys.length; i++){
153       UIManager.put(keys[i], value);
154     }
155   }// setUIDefaults(Object[] keys, Object value)
156 
157   /**
158    * Updates the Swing defaults table with the provided font to be used for the
159    * text components
160    */
161   public static void setTextComponentsFont(Font textComponentsFont){
162     setUIDefaults(textComponentsKeys, new FontUIResource(textComponentsFont));
163     Gate.getUserConfig().put(GateConstants.TEXT_COMPONENTS_FONT,
164                              textComponentsFont);
165   }
166 
167   /**
168    * Updates the Swing defaults table with the provided font to be used for the
169    * menu components
170    */
171   public static void setMenuComponentsFont(Font menuComponentsFont){
172     setUIDefaults(menuKeys, new FontUIResource(menuComponentsFont));
173     Gate.getUserConfig().put(GateConstants.MENUS_FONT,
174                              menuComponentsFont);
175   }
176 
177   /**
178    * Updates the Swing defaults table with the provided font to be used for
179    * various compoents that neither text or menu components
180    */
181   public static void setComponentsFont(Font componentsFont){
182     setUIDefaults(componentsKeys, new FontUIResource(componentsFont));
183     Gate.getUserConfig().put(GateConstants.OTHER_COMPONENTS_FONT,
184                              componentsFont);
185   }
186 
187   /**
188    * Test code
189    */
190   public static void main(String[] args) {
191     try {
192       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
193     catch(Exception e){
194       e.printStackTrace();
195     }
196 
197     JFrame frame = new JFrame("Foo frame");
198     final AppearanceDialog apperanceDialog1 = new AppearanceDialog(frame,
199                                                            "Font appearance",
200                                                            true,
201                                                            new Component[]{frame});
202     apperanceDialog1.pack();
203 
204     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
205     JButton btn = new JButton("Show dialog");
206     btn.addActionListener(new ActionListener() {
207       public void actionPerformed(ActionEvent e) {
208         apperanceDialog1.setVisible(true);
209       }
210     });
211 
212     frame.getContentPane().add(btn);
213     frame.setSize(new Dimension(300300));
214     frame.setVisible(true);
215   }// public static void main(String[] args)
216 
217   JRadioButton menusRBtn;
218   JRadioButton componentsRBtn;
219   JRadioButton textComponentsRBtn;
220   JFontChooser fontChooser;
221 
222   JButton okBtn;
223   JButton applyBtn;
224   JButton cancelBtn;
225   ButtonGroup bGroup;
226 
227   Font menusFont;
228   Font componentsFont;
229   Font textComponentsFont;
230 
231   Font oldMenusFont;
232   Font oldComponentsFont;
233   Font oldTextComponentsFont;
234 
235   /**
236    * Which font is being edited now. Possible vlues: "menu", "text",
237    * "components".
238    */
239   String currentFont;
240   Component[] targets;
241 
242   public static String[] menuKeys = new String[]{"CheckBoxMenuItem.acceleratorFont",
243                                           "CheckBoxMenuItem.font",
244                                           "Menu.acceleratorFont",
245                                           "Menu.font",
246                                           "MenuBar.font",
247                                           "MenuItem.acceleratorFont",
248                                           "MenuItem.font",
249                                           "RadioButtonMenuItem.acceleratorFont",
250                                           "RadioButtonMenuItem.font"};
251 
252   public static String[] componentsKeys =
253                              new String[]{"Button.font",
254                                           "CheckBox.font",
255                                           "ColorChooser.font",
256                                           "ComboBox.font",
257                                           "InternalFrame.titleFont",
258                                           "Label.font",
259                                           "List.font",
260                                           "OptionPane.font",
261                                           "Panel.font",
262                                           "PasswordField.font",
263                                           "PopupMenu.font",
264                                           "ProgressBar.font",
265                                           "RadioButton.font",
266                                           "ScrollPane.font",
267                                           "TabbedPane.font",
268                                           "Table.font",
269                                           "TableHeader.font",
270                                           "TitledBorder.font",
271                                           "ToggleButton.font",
272                                           "ToolBar.font",
273                                           "ToolTip.font",
274                                           "Tree.font",
275                                           "Viewport.font"};
276 
277   public static String[] textComponentsKeys =
278                              new String[]{"EditorPane.font",
279                                           "TextArea.font",
280                                           "TextField.font",
281                                           "TextPane.font"};
282 
283   class ApplyAction extends AbstractAction{
284     ApplyAction(){
285       super("Apply");
286     }
287 
288     public void actionPerformed(ActionEvent evt) {
289       setMenuComponentsFont(menusFont);
290       setComponentsFont(componentsFont);
291       setTextComponentsFont(textComponentsFont);
292       SwingUtilities.updateComponentTreeUI(AppearanceDialog.this);
293       for(int i = 0; i< targets.length; i++){
294         if(targets[iinstanceof Window) {
295           SwingUtilities.updateComponentTreeUI(targets[i]);
296         else {
297           SwingUtilities.updateComponentTreeUI(
298             SwingUtilities.getRoot(targets[i])
299           );
300         }
301       }
302     }// void actionPerformed(ActionEvent evt)
303   }
304 
305   class OKAction extends AbstractAction {
306     OKAction(){
307       super("OK");
308     }
309 
310     public void actionPerformed(ActionEvent evt){
311       applyBtn.getAction().actionPerformed(evt);
312       setVisible(false);
313     }
314   }// class OKAction extends AbstractAction
315 
316   class CancelAction extends AbstractAction {
317     CancelAction(){
318       super("Cancel");
319     }
320 
321     public void actionPerformed(ActionEvent evt){
322       setUIDefaults(menuKeys, new FontUIResource(oldMenusFont));
323       setUIDefaults(componentsKeys, new FontUIResource(oldComponentsFont));
324       setUIDefaults(textComponentsKeys, new FontUIResource(oldTextComponentsFont));
325       SwingUtilities.updateComponentTreeUI(
326                                   SwingUtilities.getRoot(AppearanceDialog.this));
327       for(int i = 0; i< targets.length; i++){
328         if(targets[iinstanceof Window){
329           SwingUtilities.updateComponentTreeUI(targets[i]);
330         else {
331           SwingUtilities.updateComponentTreeUI(
332             SwingUtilities.getRoot(targets[i])
333           );
334         }
335       }
336       setVisible(false);
337     }// void actionPerformed(ActionEvent evt)
338   }// class CancelAction extends AbstractAction
339 
340 }// class ApperanceDialog