View Javadoc

1   package org.wcb.common;
2   
3   import javax.swing.*;
4   import java.awt.*;
5   import java.awt.event.ActionListener;
6   import java.awt.event.ActionEvent;
7   
8   /***
9    * Copyright (C) 2003  Walter Bogaardt
10   *
11   * This library is free software; you can redistribute it and/or
12   * modify it under the terms of the GNU Lesser General Public
13   * License as published by the Free Software Foundation; either
14   * version 2 of the License, or (at your option) any later version.
15   *
16   * This library is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   * Lesser General Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser General Public
22   * License along with this library; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
24   *
25   *  Project:   Home Automation Interface
26   *
27   *  Abstract:  User Interface options dialog box
28   *  Filename:  $Id: FontChoiceDialog.java,v 1.2 2004/07/22 03:06:50 wbogaardt Exp $
29   *
30   *  $Log: FontChoiceDialog.java,v $
31   *  Revision 1.2  2004/07/22 03:06:50  wbogaardt
32   *  removed deprecated method calls.
33   *
34   *  Revision 1.1  2003/12/31 00:08:12  wbogaardt
35   *  fixed bug with setting look and feel and having it persist for user next time open
36   *
37   */
38  public class FontChoiceDialog extends JPanel {
39  
40      private Font font;
41      private JCheckBox italic, bold;
42      private JComboBox fontsBox, sizeBox;
43      private static Component tableButton;
44      private JLabel exampleLabel;
45      private static final String[] fonts = {"Dialog","DialogInput","SansSerif","Serif","Monospaced"};
46      private static final String[] sizes =  { "9", "10", "12", "14", "18", "24" };
47  
48      public FontChoiceDialog(Component comp) {
49          JPanel display = new JPanel();
50          JPanel fontDisplay = new JPanel();
51          exampleLabel = new JLabel("Example");
52          italic = new JCheckBox("Italic");
53          bold = new JCheckBox("Bold");
54          fontsBox = new JComboBox(fonts);
55          sizeBox = new JComboBox(sizes);
56          font = comp.getFont();
57          setFont(comp.getFont());
58          tableButton = (JButton)comp;
59          display.add(italic);
60          display.add(bold);
61          fontDisplay.add(fontsBox);
62          fontDisplay.add(sizeBox);
63          add(BorderLayout.NORTH, display);
64          add(BorderLayout.CENTER, fontDisplay);
65          add(BorderLayout.SOUTH,  exampleLabel);
66      }
67  
68      public Font getFont() {
69          if (font==null) return new Font("Dialog",Font.BOLD,12);
70          int style=0;
71          if(bold.isSelected() && italic.isSelected()) style = 3;
72          if(bold.isSelected()) style = Font.BOLD;
73          if(italic.isSelected()) style = Font.ITALIC;
74          if(!bold.isSelected() && !italic.isSelected()) style = Font.PLAIN;
75          String fontName = (String)fontsBox.getSelectedItem();
76          int fontSize = Integer.parseInt((String)sizeBox.getSelectedItem());
77          font = new Font(fontName, style, fontSize);
78          return font;
79      }
80  
81      public void setFont(Font curFont) {
82          this.font=curFont;
83          if (font.isBold()) bold.setSelected(true);
84          if (font.isItalic()) italic.setSelected(true);
85          if (!font.isBold() && !font.isItalic()) {
86              bold.setSelected(false);
87              italic.setSelected(false);
88          }
89          fontsBox.setSelectedItem(font.getFamily());
90          String fsize = new String(""+font.getSize());
91          sizeBox.setSelectedItem(fsize);
92          exampleLabel.setFont(font);
93          exampleLabel.setText(font.getFamily());
94      }
95  
96      public static JDialog createDialog(Component c, String title, boolean modal,
97                                         FontChoiceDialog fc,
98                                         ActionListener okListener,
99                                         ActionListener cancelListener) {
100         final JDialog chooserDialog = new JDialog(new Frame(),title, modal);
101         JPanel buttonPanel = new JPanel();
102         JButton okButton = new JButton("OK");
103         JButton cancelButton = new JButton("Close");
104         buttonPanel.add(okButton);
105         buttonPanel.add(cancelButton);
106         chooserDialog.getContentPane().add(BorderLayout.CENTER, fc);
107         chooserDialog.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
108         okButton.addActionListener(okListener);
109         if (cancelListener == null) {
110             ActionListener cl=new ActionListener() {
111                 public void actionPerformed(ActionEvent ev) {
112                     chooserDialog.dispose();
113                 }
114             };
115             cancelButton.addActionListener(cl);
116         } else if(cancelListener != null) {
117             cancelButton.addActionListener(cancelListener);
118         }
119         return chooserDialog;
120     }
121 }