1 package com.explosion.utilities.dialog;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.BorderLayout;
24 import java.awt.Dialog;
25 import java.awt.Dimension;
26 import java.awt.Font;
27 import java.awt.Frame;
28 import java.awt.Toolkit;
29 import java.awt.event.ActionEvent;
30
31 import javax.swing.BorderFactory;
32 import javax.swing.JButton;
33 import javax.swing.JDialog;
34 import javax.swing.JLabel;
35 import javax.swing.JPanel;
36
37 import com.explosion.utilities.GeneralConstants;
38
39 /***
40 * Thisclass is a dialog box which willalowtheuserto select the desired font
41 */
42
43 public class FontSelector extends JDialog
44 {
45
46 private FontSelectorPanel fontSelectorPanel;
47
48 private JPanel controlPanel = new JPanel();
49
50 private JButton cancelButton = new JButton("Cancel");
51
52 private JButton okButton = new JButton("Ok");
53
54 private Font initialFont;
55
56 private JLabel label = new JLabel("The rain in spain");
57
58 private int result = 0;
59
60 public static final int RESULT_OK = 0;
61
62 public static final int RESULT_CANCEL = 1;
63
64 public FontSelector(String title, Dialog owner, Font font)
65 {
66 super((Dialog) owner, title);
67
68 try
69 {
70 setTitle(title);
71 if (font != null)
72 initialFont = font;
73 else
74 initialFont = GeneralConstants.DEFAULT_WINDOW_FONT;
75 init();
76 } catch (Exception e)
77 {
78 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
79 }
80 }
81
82 public FontSelector(String title, Frame owner, Font font)
83 {
84 super((Frame) owner, title);
85
86 try
87 {
88 setTitle(title);
89 if (font != null)
90 initialFont = font;
91 else
92 initialFont = GeneralConstants.DEFAULT_WINDOW_FONT;
93 init();
94 } catch (Exception e)
95 {
96 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
97 }
98 }
99
100 public FontSelector(String title)
101 {
102 try
103 {
104 setTitle(title);
105 initialFont = GeneralConstants.DEFAULT_WINDOW_FONT;
106 init();
107 } catch (Exception e)
108 {
109 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
110 }
111 }
112
113 private void init() throws Exception
114 {
115 fontSelectorPanel = new FontSelectorPanel(initialFont);
116 fontSelectorPanel.setTestArea(label);
117
118 controlPanel.add(okButton);
119 controlPanel.add(cancelButton);
120
121 okButton.addActionListener(new java.awt.event.ActionListener()
122 {
123
124 public void actionPerformed(ActionEvent e)
125 {
126 okButton_actionPerformed(e);
127 }
128 });
129 cancelButton.addActionListener(new java.awt.event.ActionListener()
130 {
131
132 public void actionPerformed(ActionEvent e)
133 {
134 cancelButton_actionPerformed(e);
135 }
136 });
137
138 JPanel labelPanel = new JPanel(new BorderLayout());
139 labelPanel.setBorder(BorderFactory.createEtchedBorder());
140 labelPanel.add(label, BorderLayout.CENTER);
141 label.setHorizontalAlignment((int) JLabel.CENTER_ALIGNMENT);
142 label.setVerticalAlignment((int) JLabel.CENTER_ALIGNMENT);
143 label.setHorizontalTextPosition((int) JLabel.CENTER_ALIGNMENT);
144 label.setVerticalTextPosition((int) JLabel.CENTER_ALIGNMENT);
145
146 JPanel otherPanel = new JPanel(new BorderLayout());
147 otherPanel.setBorder(BorderFactory.createEtchedBorder());
148 otherPanel.add(fontSelectorPanel, BorderLayout.CENTER);
149 otherPanel.add(controlPanel, BorderLayout.SOUTH);
150
151 this.getContentPane().setLayout(new BorderLayout());
152 this.getContentPane().add(labelPanel, BorderLayout.CENTER);
153 this.getContentPane().add(otherPanel, BorderLayout.SOUTH);
154
155 this.setModal(true);
156 this.setSize(new Dimension(360, 150));
157
158 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
159 this.setLocation((d.width - this.getSize().width) / 2, (d.height - this.getSize().height) / 2);
160
161 fontSelectorPanel.setSelectionsToCurrent();
162 }
163
164 void okButton_actionPerformed(ActionEvent e)
165 {
166 result = RESULT_OK;
167 setVisible(false);
168 }
169
170 void cancelButton_actionPerformed(ActionEvent e)
171 {
172 result = RESULT_CANCEL;
173 fontSelectorPanel.setSelectedFont(initialFont);
174 this.setVisible(false);
175 }
176
177 public static void main(String[] args)
178 {
179 FontSelector sel = new FontSelector("Select a font");
180 sel.setVisible(true);
181 }
182
183 public Font getSelectedFont()
184 {
185 return fontSelectorPanel.getSelectedFont();
186 }
187
188 public int getResult()
189 {
190 return result;
191 }
192
193 }