| AppearanceDialog |

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

| AppearanceDialog |