View Javadoc

1   package com.explosion.utilities.dialog;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.awt.BorderLayout;
24  import java.awt.Component;
25  import java.awt.Font;
26  import java.awt.GraphicsEnvironment;
27  import java.awt.event.ActionEvent;
28  
29  import javax.swing.JComboBox;
30  import javax.swing.JPanel;
31  
32  import com.explosion.utilities.GeneralConstants;
33  
34  /***
35   * Thisclass is a dialog box which willalowtheuserto select the desired font
36   */
37  
38  public class FontSelectorPanel extends JPanel
39  {
40  
41      private JComboBox styleCombo = new JComboBox();
42  
43      private JComboBox sizeCombo = new JComboBox();
44  
45      private JComboBox nameCombo = new JComboBox();
46  
47      private JPanel selectionPanel = new JPanel();
48  
49      private Font initialFont = GeneralConstants.DEFAULT_WINDOW_FONT;
50  
51      private Component testArea = null;
52  
53      public FontSelectorPanel(Font currentFont)
54      {
55          try
56          {
57              if (currentFont != null) initialFont = currentFont;
58              init();
59          } catch (Exception e)
60          {
61              com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
62          }
63      }
64  
65      private void init() throws Exception
66      {
67          String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
68  
69          int style = initialFont.getStyle();
70          int size = initialFont.getSize();
71          String name = initialFont.getName();
72  
73          for (int i = 0; i < fonts.length; i++)
74              nameCombo.addItem(fonts[i]);
75  
76          for (int i = 6; i < 46; i++)
77              sizeCombo.addItem(Integer.toString(i));
78  
79          styleCombo.addItem("Plain");
80          styleCombo.addItem("Bold");
81          styleCombo.addItem("Italic");
82          styleCombo.addItem("Bold italic");
83  
84          Font font;
85          nameCombo.addActionListener(new java.awt.event.ActionListener()
86          {
87  
88              public void actionPerformed(ActionEvent e)
89              {
90                  combo_actionPerformed(e);
91              }
92          });
93          styleCombo.addActionListener(new java.awt.event.ActionListener()
94          {
95  
96              public void actionPerformed(ActionEvent e)
97              {
98                  combo_actionPerformed(e);
99              }
100         });
101         sizeCombo.addActionListener(new java.awt.event.ActionListener()
102         {
103 
104             public void actionPerformed(ActionEvent e)
105             {
106                 combo_actionPerformed(e);
107             }
108         });
109         selectionPanel.add(nameCombo);
110         selectionPanel.add(styleCombo);
111         selectionPanel.add(sizeCombo);
112 
113         setLayout(new BorderLayout());
114         add(selectionPanel, BorderLayout.CENTER);
115 
116         nameCombo.setEditable(false);
117         styleCombo.setEditable(false);
118         sizeCombo.setEditable(false);
119 
120         nameCombo.setSelectedItem(name);
121         styleCombo.setSelectedIndex(style);
122         sizeCombo.setSelectedIndex(size - 6);
123     }
124 
125     public Font getSelectedFont()
126     {
127         return new Font((String) nameCombo.getSelectedItem(), styleCombo.getSelectedIndex(), (new Integer((String) sizeCombo.getSelectedItem())).intValue());
128     }
129 
130     public void setSelectedFont(Font font)
131     {
132         if (font != null)
133         {
134             initialFont = font;
135             setSelectionsToCurrent();
136         }
137     }
138 
139     public void setSelectionsToCurrent()
140     {
141         int style = initialFont.getStyle();
142         int size = initialFont.getSize();
143         String name = initialFont.getName();
144 
145         nameCombo.setSelectedItem(name);
146         styleCombo.setSelectedIndex(style);
147         sizeCombo.setSelectedIndex(size - 6);
148 
149         testArea.setFont(new Font((String) nameCombo.getSelectedItem(), styleCombo.getSelectedIndex(), (new Integer((String) sizeCombo.getSelectedItem())).intValue()));
150         testArea.repaint();
151     }
152 
153     void combo_actionPerformed(ActionEvent e)
154     {
155         if (e.getActionCommand().equalsIgnoreCase("comboBoxChanged"))
156         {
157             if (testArea != null)
158             {
159                 testArea.setFont(new Font((String) nameCombo.getSelectedItem(), styleCombo.getSelectedIndex(), (new Integer((String) sizeCombo.getSelectedItem())).intValue()));
160                 testArea.repaint();
161                 //testArea.revalidate();
162             }
163         }
164     }
165 
166     public void setTestArea(Component testArea)
167     {
168         this.testArea = testArea;
169     }
170 
171 }