KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > util > FontDialogSwing


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.util;
33
34 import java.awt.Color JavaDoc;
35 import java.awt.Container JavaDoc;
36 import java.awt.Dimension JavaDoc;
37 import java.awt.FlowLayout JavaDoc;
38 import java.awt.Font JavaDoc;
39 import java.awt.GraphicsEnvironment JavaDoc;
40 import java.awt.event.ActionEvent JavaDoc;
41 import java.awt.event.ActionListener JavaDoc;
42 import java.awt.event.ItemEvent JavaDoc;
43 import java.awt.event.ItemListener JavaDoc;
44
45 import javax.swing.ImageIcon JavaDoc;
46 import javax.swing.JButton JavaDoc;
47 import javax.swing.JCheckBox JavaDoc;
48 import javax.swing.JColorChooser JavaDoc;
49 import javax.swing.JComboBox JavaDoc;
50 import javax.swing.JDialog JavaDoc;
51 import javax.swing.JFrame JavaDoc;
52
53 // weconsultants@users 20041109 - original swing port
54
// weconsultants@users 20050215 - version 1.8.0 - Update: Compatbilty fix for JDK 1.3
55
// - Replaced: Objects JSpinner spinnerFontSizes and SpinnerNumberModel spinnerModelSizes
56
// for JComboBox fontSizesComboBox and String fontSizes[];
57
public class FontDialogSwing extends JDialog JavaDoc {
58
59     private static boolean isRunning = false;
60     private static final String JavaDoc BACKGROUND = "Background";
61     private static String JavaDoc defaultFont = "Dialog";
62     private static final String JavaDoc FOREGROUND = "Foreground";
63     private static JButton JavaDoc bgColorButton;
64     private static JCheckBox JavaDoc ckbbold;
65     private static JButton JavaDoc closeButton;
66     private static JButton JavaDoc fgColorButton;
67     private static JComboBox JavaDoc fontsComboBox;
68
69     // weconsultants@users 20050215 - Added for Compatbilty fix for JDK 1.3
70
private static JComboBox JavaDoc fontSizesComboBox;
71     private static final String JavaDoc[] fontSizes = {
72         "8", "9", "10", "11", "12", "13", "14", "16", "18", "24", "36"
73     };
74
75     // weconsultants@users 20050215 - Commented out for Compatbilty fix for JDK 1.3
76
// private static JSpinner spinnerFontSizes;
77
// private static SpinnerNumberModel spinnerModelSizes;
78
private static DatabaseManagerSwing fOwner;
79     private static JFrame JavaDoc frame =
80         new JFrame JavaDoc("DataBaseManagerSwing Font Selection Dialog");
81     private static JCheckBox JavaDoc ckbitalic;
82
83     /**
84      * Create and display FontDialogSwing Dialog.
85      *
86      */

87     public static void CreatFontDialog(DatabaseManagerSwing owner) {
88
89         if (isRunning) {
90             frame.setVisible(true);
91         } else {
92             CommonSwing.setSwingLAF(frame, CommonSwing.Native);
93
94             fOwner = owner;
95
96             frame.setIconImage(CommonSwing.getIcon("Frame"));
97
98             isRunning = true;
99
100             frame.setSize(600, 100);
101             CommonSwing.setFramePositon(frame);
102
103             ckbitalic = new JCheckBox JavaDoc(
104                 new ImageIcon JavaDoc(CommonSwing.getIcon("ItalicFont")));
105
106             ckbitalic.putClientProperty("is3DEnabled", Boolean.TRUE);
107             ckbitalic.addActionListener(new ActionListener JavaDoc() {
108
109                 public void actionPerformed(ActionEvent JavaDoc e) {
110                     setStyle();
111                 }
112             });
113
114             ckbbold =
115                 new JCheckBox JavaDoc(new ImageIcon JavaDoc(CommonSwing.getIcon("BoldFont")));
116
117             ckbbold.putClientProperty("is3DEnabled", Boolean.TRUE);
118             ckbbold.addActionListener(new ActionListener JavaDoc() {
119
120                 public void actionPerformed(ActionEvent JavaDoc e) {
121                     setStyle();
122                 }
123             });
124
125             fgColorButton = new JButton JavaDoc(
126                 "Foreground",
127                 new ImageIcon JavaDoc(CommonSwing.getIcon("ColorSelection")));
128
129             fgColorButton.putClientProperty("is3DEnabled", Boolean.TRUE);
130             fgColorButton.addActionListener(new ActionListener JavaDoc() {
131
132                 public void actionPerformed(ActionEvent JavaDoc e) {
133                     setColor(FOREGROUND);
134                 }
135             });
136
137             bgColorButton = new JButton JavaDoc(
138                 "Background",
139                 new ImageIcon JavaDoc(CommonSwing.getIcon("ColorSelection")));
140
141             bgColorButton.putClientProperty("is3DEnabled", Boolean.TRUE);
142             bgColorButton.addActionListener(new ActionListener JavaDoc() {
143
144                 public void actionPerformed(ActionEvent JavaDoc e) {
145                     setColor(BACKGROUND);
146                 }
147             });
148
149             closeButton =
150                 new JButton JavaDoc("Close",
151                             new ImageIcon JavaDoc(CommonSwing.getIcon("Close")));
152
153             closeButton.putClientProperty("is3DEnabled", Boolean.TRUE);
154             closeButton.addActionListener(new ActionListener JavaDoc() {
155
156                 public void actionPerformed(ActionEvent JavaDoc e) {
157                     frame.setVisible(false);
158                 }
159             });
160
161             GraphicsEnvironment JavaDoc ge =
162                 GraphicsEnvironment.getLocalGraphicsEnvironment();
163             String JavaDoc[] fontNames = ge.getAvailableFontFamilyNames();
164             Dimension JavaDoc fontsComboBoxDimension = new Dimension JavaDoc(160, 25);
165
166             fontsComboBox = new JComboBox JavaDoc(fontNames);
167
168             fontsComboBox.putClientProperty("is3DEnabled", Boolean.TRUE);
169             fontsComboBox.setMaximumSize(fontsComboBoxDimension);
170             fontsComboBox.setPreferredSize(fontsComboBoxDimension);
171             fontsComboBox.setMaximumSize(fontsComboBoxDimension);
172             fontsComboBox.setEditable(false);
173             fontsComboBox.setSelectedItem(defaultFont);
174             fontsComboBox.addActionListener(new ActionListener JavaDoc() {
175
176                 public void actionPerformed(ActionEvent JavaDoc e) {
177                     setFont();
178                 }
179             });
180
181             // weconsultants@users 20050215 - Added for Compatbilty fix for JDK 1.3
182
fontSizesComboBox = new JComboBox JavaDoc(fontSizes);
183
184             Dimension JavaDoc spinnerDimension = new Dimension JavaDoc(45, 25);
185
186             fontSizesComboBox.putClientProperty("is3DEnabled", Boolean.TRUE);
187             fontSizesComboBox.setMinimumSize(spinnerDimension);
188             fontSizesComboBox.setPreferredSize(spinnerDimension);
189             fontSizesComboBox.setMaximumSize(spinnerDimension);
190             fontSizesComboBox.addItemListener(new ItemListener JavaDoc() {
191
192                 public void itemStateChanged(ItemEvent JavaDoc evt) {
193
194                     if (evt.getStateChange() == ItemEvent.SELECTED) {
195                         setFontSize((String JavaDoc) evt.getItem());
196                     }
197                 }
198             });
199
200             // weconsultants@users 20050215 - Commented out for Compatbilty fix for JDK 1.3
201
// Dimension spinnerDimension = new Dimension(50, 25);
202
// spinnerFontSizes = new JSpinner();
203
// spinnerFontSizes.putClientProperty("is3DEnabled", Boolean.TRUE);
204
// spinnerFontSizes.setMinimumSize(spinnerDimension);
205
// spinnerFontSizes.setPreferredSize(spinnerDimension);
206
// spinnerFontSizes.setMaximumSize(spinnerDimension);
207
// spinnerModelSizes = new SpinnerNumberModel(12, 8, 72, 1);
208
// spinnerFontSizes.setModel(spinnerModelSizes);
209
// spinnerFontSizes.addChangeListener(new ChangeListener() {
210
// public void stateChanged(ChangeEvent e) {
211
// setFontSize();
212
// }
213
// });
214
Container JavaDoc contentPane = frame.getContentPane();
215
216             contentPane.setLayout(new FlowLayout JavaDoc());
217             contentPane.add(fontsComboBox);
218
219             // weconsultants@users 20050215 - Commented out for Compatbilty fix for 1.3
220
// contentPane.add(spinnerFontSizes);
221
// weconsultants@users 20050215 - Added for Compatbilty fix for 1.3
222
contentPane.add(fontSizesComboBox);
223             contentPane.add(ckbbold);
224             contentPane.add(ckbitalic);
225             contentPane.add(fgColorButton);
226             contentPane.add(bgColorButton);
227             contentPane.add(closeButton);
228             frame.pack();
229             frame.setVisible(false);
230         }
231     }
232
233     public static void setFont() {
234
235         Font JavaDoc txtResultFont = fOwner.txtResult.getFont();
236
237         fOwner.txtResult.setFont(
238             new Font JavaDoc(
239                 fontsComboBox.getSelectedItem().toString(),
240                 txtResultFont.getStyle(), txtResultFont.getSize()));
241
242         Font JavaDoc txtCommandFont = fOwner.txtResult.getFont();
243
244         fOwner.txtCommand.setFont(
245             new Font JavaDoc(
246                 fontsComboBox.getSelectedItem().toString(),
247                 txtCommandFont.getStyle(), txtCommandFont.getSize()));
248
249         Font JavaDoc txtTreeFont = fOwner.txtResult.getFont();
250
251         fOwner.tTree.setFont(
252             new Font JavaDoc(
253                 fontsComboBox.getSelectedItem().toString(),
254                 txtTreeFont.getStyle(), txtTreeFont.getSize()));
255     }
256
257     /**
258      * Displays a color chooser and Sets the selected color.
259      */

260     public static void setFontSize(String JavaDoc inFontSize) {
261
262         // weconsultants@users 20050215 - Changed for Compatbilty fix for JDK 1.3
263
// Convert Strng to float for deriveFont() call
264
Float JavaDoc stageFloat = new Float JavaDoc(inFontSize);
265         float fontSize = stageFloat.floatValue();
266         Font JavaDoc fonttTree = fOwner.tTree.getFont().deriveFont(fontSize);
267
268         fOwner.tTree.setFont(fonttTree);
269
270         Font JavaDoc fontTxtCommand =
271             fOwner.txtCommand.getFont().deriveFont(fontSize);
272
273         fOwner.txtCommand.setFont(fontTxtCommand);
274
275         Font JavaDoc fontTxtResult = fOwner.txtResult.getFont().deriveFont(fontSize);
276
277         fOwner.txtResult.setFont(fontTxtResult);
278     }
279
280     /**
281      * Changes the style (Bold, Italic ) of the selected text by checking the
282      * style buttons
283      */

284     public static void setStyle() {
285
286         int style = Font.PLAIN;
287
288         if (ckbbold.isSelected()) {
289             style |= Font.BOLD;
290         }
291
292         if (ckbitalic.isSelected()) {
293             style |= Font.ITALIC;
294         }
295
296         fOwner.tTree.setFont(fOwner.txtCommand.getFont().deriveFont(style));
297         fOwner.txtCommand.setFont(
298             fOwner.txtCommand.getFont().deriveFont(style));
299         fOwner.txtResult.setFont(
300             fOwner.txtResult.getFont().deriveFont(style));
301     }
302
303     public static void setColor(String JavaDoc inTarget) {
304
305         if (inTarget.equals(BACKGROUND)) {
306             Color JavaDoc backgroundColor = JColorChooser.showDialog(null,
307                 "DataBaseManagerSwing Choose Background Color",
308                 fOwner.txtResult.getBackground());
309
310             if (backgroundColor != null) {
311                 bgColorButton.setBackground(backgroundColor);
312                 fOwner.txtCommand.setBackground(backgroundColor);
313                 fOwner.txtResult.setBackground(backgroundColor);
314             }
315         } else {
316             Color JavaDoc foregroundColor = JColorChooser.showDialog(null,
317                 "DataBaseManagerSwing Choose Foreground Color",
318                 fOwner.txtResult.getForeground());
319
320             if (foregroundColor != null) {
321                 fgColorButton.setBackground(foregroundColor);
322                 fOwner.txtCommand.setForeground(foregroundColor);
323                 fOwner.txtResult.setForeground(foregroundColor);
324             }
325         }
326     }
327 }
328
Popular Tags