001: package net.sourceforge.squirrel_sql.fw.gui;
002:
003: /*
004: * Copyright (C) 2001-2004 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.BorderLayout;
022: import java.awt.Dialog;
023: import java.awt.Dimension;
024: import java.awt.Font;
025: import java.awt.Frame;
026: import java.awt.GraphicsEnvironment;
027: import java.awt.GridBagConstraints;
028: import java.awt.GridBagLayout;
029: import java.awt.Insets;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032:
033: import javax.swing.BorderFactory;
034: import javax.swing.JButton;
035: import javax.swing.JCheckBox;
036: import javax.swing.JComboBox;
037: import javax.swing.JDialog;
038: import javax.swing.JLabel;
039: import javax.swing.JPanel;
040:
041: import net.sourceforge.squirrel_sql.fw.util.StringManager;
042: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
043:
044: /**
045: * A dialog allow selection and a font and its associated info.
046: *
047: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
048: */
049: public class FontChooser extends JDialog {
050: /** Internationalized strings for this class. */
051: private static final StringManager s_stringMgr = StringManagerFactory
052: .getStringManager(FontChooser.class);
053:
054: private final boolean _selectStyles;
055:
056: private JComboBox _fontNamesCmb;
057: private final JComboBox _fontSizesCmb = new JComboBox(new String[] {
058: "8", "9", "10", "12", "14" });
059: private final JCheckBox _boldChk = new JCheckBox(s_stringMgr
060: .getString("FontChooser.bold"));
061: private final JCheckBox _italicChk = new JCheckBox(s_stringMgr
062: .getString("FontChooser.italic"));
063: private final JLabel _previewLbl = new JLabel(s_stringMgr
064: .getString("FontChooser.previewText"));
065:
066: private Font _font;
067:
068: private ActionListener _previewUpdater;
069:
070: /**
071: * Default ctor.
072: */
073: public FontChooser() {
074: this ((Frame) null);
075: }
076:
077: /**
078: * ctor specifying whether styles can be selected.
079: *
080: * @param selectStyles If <TT>true</TT> bold and italic checkboxes displayed.
081: */
082: public FontChooser(boolean selectStyles) {
083: this ((Frame) null, selectStyles);
084: }
085:
086: /**
087: * ctor specifying the parent frame.
088: *
089: * @param owner Parent frame.
090: */
091: public FontChooser(Frame owner) {
092: super (owner, s_stringMgr.getString("FontChooser.title"), true);
093: _selectStyles = true;
094: createUserInterface();
095: }
096:
097: /**
098: * ctor specifying the parent frame and whether styles can be selected.
099: *
100: * @param owner Parent frame.
101: * @param selectStyles If <TT>true</TT> bold and italic checkboxes displayed.
102: */
103: public FontChooser(Frame owner, boolean selectStyles) {
104: super (owner, s_stringMgr.getString("FontChooser.title"), true);
105: _selectStyles = selectStyles;
106: createUserInterface();
107: }
108:
109: /**
110: * ctor specifying the parent dialog.
111: *
112: * @param owner Parent frame.
113: */
114: public FontChooser(Dialog owner) {
115: super (owner, s_stringMgr.getString("FontChooser.title"), true);
116: _selectStyles = true;
117: createUserInterface();
118: }
119:
120: /**
121: * ctor specifying the parent dialog and whether styles can be selected.
122: *
123: * @param owner Parent frame.
124: * @param selectStyles If <TT>true</TT> bold and italic checkboxes displayed.
125: */
126: public FontChooser(Dialog owner, boolean selectStyles) {
127: super (owner, s_stringMgr.getString("FontChooser.title"), true);
128: _selectStyles = selectStyles;
129: createUserInterface();
130: }
131:
132: /**
133: * Component is being added to its parent.
134: */
135: public void addNotify() {
136: super .addNotify();
137: if (_previewUpdater == null) {
138: _previewUpdater = new PreviewLabelUpdater();
139: _fontNamesCmb.addActionListener(_previewUpdater);
140: _fontSizesCmb.addActionListener(_previewUpdater);
141: _boldChk.addActionListener(_previewUpdater);
142: _italicChk.addActionListener(_previewUpdater);
143: }
144: }
145:
146: /**
147: * Component is being removed from its parent.
148: */
149: public void removeNotify() {
150: super .removeNotify();
151: if (_previewUpdater != null) {
152: _fontNamesCmb.removeActionListener(_previewUpdater);
153: _fontSizesCmb.removeActionListener(_previewUpdater);
154: _boldChk.removeActionListener(_previewUpdater);
155: _italicChk.removeActionListener(_previewUpdater);
156: _previewUpdater = null;
157: }
158: }
159:
160: public Font showDialog() {
161: return showDialog(null);
162: }
163:
164: /**
165: * Show dialog defaulting to the passed font.
166: *
167: * @param font The font to default to.
168: */
169: public Font showDialog(Font font) {
170: if (font != null) {
171: _fontNamesCmb.setSelectedItem(font.getName());
172: _fontSizesCmb.setSelectedItem("" + font.getSize());
173: _boldChk.setSelected(_selectStyles && font.isBold());
174: _italicChk.setSelected(_selectStyles && font.isItalic());
175: } else {
176: _fontNamesCmb.setSelectedIndex(0);
177: _fontSizesCmb.setSelectedIndex(0);
178: _boldChk.setSelected(false);
179: _italicChk.setSelected(false);
180: }
181: setupPreviewLabel();
182: setVisible(true);
183: return _font;
184: }
185:
186: public Font getSelectedFont() {
187: return _font;
188: }
189:
190: protected void setupFontFromDialog() {
191: int size = 12;
192: try {
193: size = Integer.parseInt((String) _fontSizesCmb
194: .getSelectedItem());
195: } catch (Exception ignore) {
196: // Ignore.
197: }
198: FontInfo fi = new FontInfo();
199: fi.setFamily((String) _fontNamesCmb.getSelectedItem());
200: fi.setSize(size);
201: fi.setIsBold(_boldChk.isSelected());
202: fi.setIsItalic(_italicChk.isSelected());
203: _font = fi.createFont();
204: }
205:
206: private void createUserInterface() {
207: final JPanel content = new JPanel(new GridBagLayout());
208: final GridBagConstraints gbc = new GridBagConstraints();
209:
210: setContentPane(content);
211: content.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
212:
213: gbc.anchor = GridBagConstraints.WEST;
214: gbc.insets = new Insets(2, 2, 2, 2);
215: gbc.fill = GridBagConstraints.HORIZONTAL;
216:
217: gbc.gridx = gbc.gridy = 0;
218: content.add(new JLabel(s_stringMgr
219: .getString("FontChooser.font")), gbc);
220:
221: ++gbc.gridx;
222: content.add(new JLabel(s_stringMgr
223: .getString("FontChooser.size")), gbc);
224:
225: if (_selectStyles) {
226: ++gbc.gridx;
227: content.add(new JLabel(s_stringMgr
228: .getString("FontChooser.style")), gbc);
229: }
230:
231: ++gbc.gridy;
232: gbc.gridx = 0;
233: _fontNamesCmb = new JComboBox(GraphicsEnvironment
234: .getLocalGraphicsEnvironment()
235: .getAvailableFontFamilyNames());
236: content.add(_fontNamesCmb, gbc);
237:
238: ++gbc.gridx;
239: _fontSizesCmb.setEditable(true);
240: content.add(_fontSizesCmb, gbc);
241:
242: if (_selectStyles) {
243: ++gbc.gridx;
244: content.add(_boldChk, gbc);
245: ++gbc.gridy;
246: content.add(_italicChk, gbc);
247: }
248:
249: gbc.gridx = 0;
250: ++gbc.gridy;
251: gbc.gridwidth = GridBagConstraints.REMAINDER;
252: gbc.fill = GridBagConstraints.BOTH;
253: gbc.anchor = GridBagConstraints.CENTER;
254: content.add(createPreviewPanel(), gbc);
255:
256: ++gbc.gridy;
257: gbc.fill = GridBagConstraints.HORIZONTAL;
258: content.add(createButtonsPanel(), gbc);
259:
260: pack();
261: GUIUtils.centerWithinParent(this );
262: setResizable(true);
263: }
264:
265: private JPanel createPreviewPanel() {
266: final JPanel pnl = new JPanel(new BorderLayout());
267: pnl.setBorder(BorderFactory.createTitledBorder(s_stringMgr
268: .getString("FontChooser.previewTitle")));
269: Dimension prefSize = _previewLbl.getPreferredSize();
270: prefSize.height = 50;
271: _previewLbl.setPreferredSize(prefSize);
272: pnl.add(_previewLbl, BorderLayout.CENTER);
273: setupPreviewLabel();
274:
275: return pnl;
276: }
277:
278: private JPanel createButtonsPanel() {
279: JPanel pnl = new JPanel();
280:
281: JButton okBtn = new JButton(s_stringMgr
282: .getString("FontChooser.ok"));
283: okBtn.addActionListener(new ActionListener() {
284: public void actionPerformed(ActionEvent evt) {
285: setupFontFromDialog();
286: dispose();
287: }
288: });
289: JButton cancelBtn = new JButton(s_stringMgr
290: .getString("FontChooser.cancel"));
291: cancelBtn.addActionListener(new ActionListener() {
292: public void actionPerformed(ActionEvent evt) {
293: FontChooser.this ._font = null;
294: dispose();
295: }
296: });
297:
298: pnl.add(okBtn);
299: pnl.add(cancelBtn);
300:
301: GUIUtils.setJButtonSizesTheSame(new JButton[] { okBtn,
302: cancelBtn });
303: getRootPane().setDefaultButton(okBtn);
304:
305: return pnl;
306: }
307:
308: private void setupPreviewLabel() {
309: setupFontFromDialog();
310: _previewLbl.setFont(_font);
311: }
312:
313: private final class PreviewLabelUpdater implements ActionListener {
314: public void actionPerformed(ActionEvent evt) {
315: setupPreviewLabel();
316: }
317: }
318: }
|