001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.gui.dialog;
019:
020: import java.awt.Dimension;
021: import java.awt.Font;
022: import java.awt.GraphicsEnvironment;
023: import java.awt.GridBagConstraints;
024: import java.awt.GridBagLayout;
025: import java.awt.Insets;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.KeyEvent;
029: import java.util.Locale;
030:
031: import javax.swing.JButton;
032: import javax.swing.JComponent;
033: import javax.swing.JDialog;
034: import javax.swing.JLabel;
035: import javax.swing.JList;
036: import javax.swing.JScrollPane;
037: import javax.swing.JTextField;
038: import javax.swing.KeyStroke;
039: import javax.swing.event.ListSelectionEvent;
040: import javax.swing.event.ListSelectionListener;
041:
042: import org.columba.core.gui.base.ButtonWithMnemonic;
043: import org.columba.core.resourceloader.GlobalResourceLoader;
044:
045: public class FontSelectionDialog extends JDialog implements
046: ActionListener, ListSelectionListener {
047: public static final int APPROVE_OPTION = 1;
048:
049: public static final int CANCEL_OPTION = 0;
050:
051: private static final String RESOURCE_BUNDLE_PATH = "org.columba.core.i18n.dialog";
052:
053: protected JList fontList;
054:
055: protected JList styleList;
056:
057: protected JList sizeList;
058:
059: protected JTextField preview;
060:
061: protected JTextField fontName;
062:
063: protected JTextField styleName;
064:
065: protected JTextField sizeName;
066:
067: protected JLabel fontLabel;
068:
069: protected JLabel sizeLabel;
070:
071: protected JLabel styleLabel;
072:
073: protected JLabel previewLabel;
074:
075: protected JButton okButton;
076:
077: protected JButton cancelButton;
078:
079: protected Font font;
080:
081: protected int status;
082:
083: public FontSelectionDialog(JDialog parent, Font f) {
084: super (parent, true);
085:
086: setTitle(GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH,
087: "font", "title"));
088:
089: font = f;
090:
091: setSize(450, 325);
092:
093: initData();
094: initComponents();
095:
096: setLocationRelativeTo(null);
097: }
098:
099: private void initComponents() {
100: JScrollPane scroller;
101: GridBagLayout gridbag = new GridBagLayout();
102: GridBagConstraints c = new GridBagConstraints();
103:
104: final double w1 = 3.0;
105: final double w2 = 2.0;
106: final double w3 = 0.7;
107:
108: getContentPane().setLayout(gridbag);
109:
110: // Spacings
111: c.insets = new Insets(1, 3, 1, 3);
112:
113: // First Line with Labels
114: c.gridheight = 1;
115: c.weighty = 1.0;
116: c.weightx = w1;
117: c.fill = GridBagConstraints.HORIZONTAL;
118: c.gridwidth = 1;
119: gridbag.setConstraints(fontLabel, c);
120: getContentPane().add(fontLabel);
121:
122: c.weightx = w2;
123: c.fill = GridBagConstraints.HORIZONTAL;
124: c.gridwidth = GridBagConstraints.RELATIVE;
125: gridbag.setConstraints(styleLabel, c);
126: getContentPane().add(styleLabel);
127:
128: c.weightx = w3;
129: c.fill = GridBagConstraints.HORIZONTAL;
130: c.gridwidth = GridBagConstraints.REMAINDER;
131: gridbag.setConstraints(sizeLabel, c);
132: getContentPane().add(sizeLabel);
133:
134: // Second Line with Names
135: c.weightx = w1;
136: c.fill = GridBagConstraints.HORIZONTAL;
137: c.gridwidth = 1;
138: gridbag.setConstraints(fontName, c);
139: getContentPane().add(fontName);
140:
141: c.weightx = w2;
142: c.fill = GridBagConstraints.HORIZONTAL;
143: c.gridwidth = GridBagConstraints.RELATIVE;
144: gridbag.setConstraints(styleName, c);
145: getContentPane().add(styleName);
146:
147: c.weightx = w3;
148: c.fill = GridBagConstraints.HORIZONTAL;
149: c.gridwidth = GridBagConstraints.REMAINDER;
150: gridbag.setConstraints(sizeName, c);
151: getContentPane().add(sizeName);
152:
153: // Third Line with Lists
154: c.weighty = 6.0;
155:
156: scroller = new JScrollPane(fontList);
157: c.weightx = w1;
158: c.fill = GridBagConstraints.BOTH;
159: c.gridwidth = 1;
160: gridbag.setConstraints(scroller, c);
161: getContentPane().add(scroller);
162:
163: scroller = new JScrollPane(styleList);
164: c.weightx = w2;
165: c.gridwidth = GridBagConstraints.RELATIVE;
166: gridbag.setConstraints(scroller, c);
167: getContentPane().add(scroller);
168:
169: scroller = new JScrollPane(sizeList);
170: c.weightx = w3;
171: c.gridwidth = GridBagConstraints.REMAINDER;
172: gridbag.setConstraints(scroller, c);
173: getContentPane().add(scroller);
174:
175: // 4. Line with PreviewLabel
176: c.weighty = 1.0;
177: c.fill = GridBagConstraints.HORIZONTAL;
178: c.gridwidth = GridBagConstraints.REMAINDER;
179: gridbag.setConstraints(previewLabel, c);
180: getContentPane().add(previewLabel);
181:
182: // 5. Line with Preview
183: c.weightx = 1.0;
184: c.weighty = 5.0;
185: c.gridwidth = GridBagConstraints.REMAINDER;
186: c.gridheight = GridBagConstraints.RELATIVE;
187: c.fill = GridBagConstraints.BOTH;
188: gridbag.setConstraints(preview, c);
189: getContentPane().add(preview);
190:
191: // 6. Line with Buttons
192: okButton = new ButtonWithMnemonic(GlobalResourceLoader
193: .getString("global", "global", "ok"));
194: okButton.addActionListener(this );
195:
196: c.weightx = 1.0;
197: c.weighty = 1.0;
198: c.insets = new Insets(10, 5, 10, 5);
199: c.anchor = GridBagConstraints.EAST;
200: c.gridwidth = 1;
201: c.gridheight = GridBagConstraints.REMAINDER;
202: c.fill = GridBagConstraints.NONE;
203: gridbag.setConstraints(okButton, c);
204: getContentPane().add(okButton);
205:
206: cancelButton = new ButtonWithMnemonic(GlobalResourceLoader
207: .getString("global", "global", "cancel"));
208: cancelButton.addActionListener(this );
209:
210: c.weightx = 1.0;
211: c.anchor = GridBagConstraints.WEST;
212: c.gridwidth = GridBagConstraints.REMAINDER;
213: c.gridheight = GridBagConstraints.REMAINDER;
214: c.fill = GridBagConstraints.NONE;
215: gridbag.setConstraints(cancelButton, c);
216: getContentPane().add(cancelButton);
217:
218: Dimension okSize;
219: Dimension cancelSize;
220:
221: okSize = okButton.getPreferredSize();
222: cancelSize = cancelButton.getPreferredSize();
223:
224: if (okSize.width < cancelSize.width) {
225: okSize.width = cancelSize.width;
226: okButton.setPreferredSize(okSize);
227: }
228:
229: getRootPane().setDefaultButton(okButton);
230: getRootPane().registerKeyboardAction(this ,
231: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
232: JComponent.WHEN_IN_FOCUSED_WINDOW);
233: }
234:
235: void initData() {
236: GraphicsEnvironment gEnv = GraphicsEnvironment
237: .getLocalGraphicsEnvironment();
238:
239: //String envfonts[] = gEnv.getAvailableFontFamilyNames();
240: String[] envfonts = gEnv.getAvailableFontFamilyNames(Locale
241: .getDefault());
242: fontList = new JList(envfonts);
243: fontList.setSelectedIndex(0);
244:
245: styleList = new JList(new Object[] {
246: GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH,
247: "font", "plain"),
248: GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH,
249: "font", "bold"),
250: GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH,
251: "font", "italic"),
252: GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH,
253: "font", "bold_italic") });
254: styleList.setSelectedIndex(0);
255:
256: //fill sizes string array with numbers from 7 to 18
257: Object[] sizes = new String[12];
258:
259: for (int i = 7; i < 19; i++) {
260: sizes[i - 7] = Integer.toString(i);
261: }
262:
263: sizeList = new JList(sizes);
264: sizeList.setSelectedIndex(0);
265:
266: preview = new JTextField("abcdefgh ABCDEFGH");
267: preview.setHorizontalAlignment(JTextField.CENTER);
268:
269: styleName = new JTextField();
270: sizeName = new JTextField();
271: fontName = new JTextField();
272:
273: if (font == null) {
274: font = preview.getFont();
275: }
276:
277: fontList.setSelectedValue(font.getName(), true);
278: styleList.setSelectedIndex(font.getStyle());
279: sizeList.setSelectedValue(new Integer(font.getSize())
280: .toString(), true);
281:
282: fontName.setText((String) fontList.getSelectedValue());
283: styleName.setText((String) styleList.getSelectedValue());
284: sizeName.setText((String) sizeList.getSelectedValue());
285:
286: styleList.addListSelectionListener(this );
287: sizeList.addListSelectionListener(this );
288: fontList.addListSelectionListener(this );
289:
290: fontLabel = new JLabel(GlobalResourceLoader.getString(
291: RESOURCE_BUNDLE_PATH, "font", "font"));
292: sizeLabel = new JLabel(GlobalResourceLoader.getString(
293: RESOURCE_BUNDLE_PATH, "font", "size"));
294: styleLabel = new JLabel(GlobalResourceLoader.getString(
295: RESOURCE_BUNDLE_PATH, "font", "style"));
296: previewLabel = new JLabel(GlobalResourceLoader.getString(
297: RESOURCE_BUNDLE_PATH, "font", "preview"));
298: }
299:
300: public void actionPerformed(ActionEvent e) {
301: Object source = e.getSource();
302:
303: if (source == okButton) {
304: status = APPROVE_OPTION;
305: } else if (source == cancelButton) {
306: status = CANCEL_OPTION;
307: }
308:
309: dispose();
310: }
311:
312: public int showDialog() {
313: setVisible(true);
314:
315: return status;
316: }
317:
318: public Font getSelectedFont() {
319: return font;
320: }
321:
322: public void valueChanged(ListSelectionEvent e) {
323: Object list = e.getSource();
324: String fontchoice;
325: int stChoice;
326: int siChoice;
327:
328: if (list == fontList) {
329: fontName.setText((String) fontList.getSelectedValue());
330: } else if (list == styleList) {
331: styleName.setText((String) styleList.getSelectedValue());
332: } else if (list == sizeList) {
333: sizeName.setText((String) sizeList.getSelectedValue());
334: }
335:
336: fontchoice = fontName.getText();
337: stChoice = styleList.getSelectedIndex();
338:
339: siChoice = new Integer(sizeName.getText()).intValue();
340:
341: font = new Font(fontchoice, stChoice, siChoice);
342:
343: preview.setFont(font);
344: preview.repaint();
345: }
346: }
|