001: /*
002: * FontSelector.java - Font selector
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2003 Slava Pestov
007: * Portions copyright (C) 1999 Jason Ginchereau
008: * Portions copyright (C) 2003 mike dillon
009: *
010: * This program is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU General Public License
012: * as published by the Free Software Foundation; either version 2
013: * of the License, or any later version.
014: *
015: * This program is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: * GNU General Public License for more details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with this program; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
023: */
024:
025: package org.gjt.sp.jedit.gui;
026:
027: //{{{ Imports
028: import java.awt.event.*;
029: import java.awt.*;
030: import java.util.Vector;
031: import javax.swing.border.*;
032: import javax.swing.event.*;
033: import javax.swing.*;
034: import org.gjt.sp.jedit.*;
035: import org.gjt.sp.util.Log;
036:
037: //}}}
038:
039: //{{{ FontSelector class
040: /**
041: * A font chooser widget.
042: * @author Slava Pestov
043: * @version $Id: FontSelector.java 5067 2004-06-28 06:45:27Z spestov $
044: */
045: public class FontSelector extends JButton {
046: //{{{ FontSelector constructor
047: /**
048: * Creates a new font selector control.
049: * @param font The font
050: */
051: public FontSelector(Font font) {
052: this (font, false);
053: } //}}}
054:
055: //{{{ FontSelector constructor
056: /**
057: * Creates a new font selector control.
058: * @param font The font
059: * @param antiAlias Is anti-aliasing enabled?
060: * @since jEdit 4.2pre7
061: */
062: public FontSelector(Font font, boolean antiAlias) {
063: setFont(font);
064: this .antiAlias = antiAlias;
065:
066: updateText();
067:
068: setRequestFocusEnabled(false);
069:
070: addActionListener(new ActionHandler());
071: } //}}}
072:
073: //{{{ paintComponent() method
074: public void paintComponent(Graphics g) {
075: setAntiAliasEnabled(g);
076: super .paintComponent(g);
077: } //}}}
078:
079: //{{{ isAntiAliasEnabled() method
080: public boolean isAntiAliasEnabled() {
081: return antiAlias;
082: } //}}}
083:
084: //{{{ setAntiAliasEnabled() method
085: public void setAntiAliasEnabled(boolean antiAlias) {
086: this .antiAlias = antiAlias;
087: } //}}}
088:
089: //{{{ updateText() method
090: private void updateText() {
091: Font font = getFont();
092: String styleString;
093: switch (font.getStyle()) {
094: case Font.PLAIN:
095: styleString = jEdit.getProperty("font-selector.plain");
096: break;
097: case Font.BOLD:
098: styleString = jEdit.getProperty("font-selector.bold");
099: break;
100: case Font.ITALIC:
101: styleString = jEdit.getProperty("font-selector.italic");
102: break;
103: case Font.BOLD | Font.ITALIC:
104: styleString = jEdit.getProperty("font-selector.bolditalic");
105: break;
106: default:
107: styleString = "UNKNOWN!!!???";
108: break;
109: }
110:
111: setText(font.getName() + " " + font.getSize() + " "
112: + styleString);
113: } //}}}
114:
115: //{{{ setAntiAliasEnabled() method
116: void setAntiAliasEnabled(Graphics g) {
117: if (antiAlias) {
118: Graphics2D g2 = (Graphics2D) g;
119: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
120: RenderingHints.VALUE_ANTIALIAS_ON);
121: }
122: } //}}}
123:
124: private boolean antiAlias;
125:
126: //{{{ ActionHandler class
127: class ActionHandler implements ActionListener {
128: public void actionPerformed(ActionEvent evt) {
129: Font font;
130:
131: JDialog dialog = GUIUtilities
132: .getParentDialog(FontSelector.this );
133: if (dialog == null) {
134: font = new FontSelectorDialog(JOptionPane
135: .getFrameForComponent(FontSelector.this ),
136: getFont(), FontSelector.this ).getSelectedFont();
137: } else {
138: font = new FontSelectorDialog(dialog, getFont(),
139: FontSelector.this ).getSelectedFont();
140: }
141:
142: if (font != null) {
143: setFont(font);
144: updateText();
145: }
146: }
147: } //}}}
148: } //}}}
149:
150: //{{{ FontSelectorDialog class
151: class FontSelectorDialog extends EnhancedDialog {
152: //{{{ FontSelectorDialog constructor
153: public FontSelectorDialog(Frame parent, Font font) {
154: super (parent, jEdit.getProperty("font-selector.title"), true);
155: init(font);
156: } //}}}
157:
158: //{{{ FontSelectorDialog constructor
159: public FontSelectorDialog(Dialog parent, Font font) {
160: super (parent, jEdit.getProperty("font-selector.title"), true);
161: init(font);
162: } //}}}
163:
164: //{{{ FontSelectorDialog constructor
165: public FontSelectorDialog(Frame parent, Font font,
166: FontSelector fontSelector) {
167: super (parent, jEdit.getProperty("font-selector.title"), true);
168: this .fontSelector = fontSelector;
169: init(font);
170: } //}}}
171:
172: //{{{ FontSelectorDialog constructor
173: public FontSelectorDialog(Dialog parent, Font font,
174: FontSelector fontSelector) {
175: super (parent, jEdit.getProperty("font-selector.title"), true);
176: this .fontSelector = fontSelector;
177: init(font);
178: } //}}}
179:
180: //{{{ ok() method
181: public void ok() {
182: isOK = true;
183: dispose();
184: } //}}}
185:
186: //{{{ cancel() method
187: public void cancel() {
188: dispose();
189: } //}}}
190:
191: //{{{ getSelectedFont() method
192: public Font getSelectedFont() {
193: if (!isOK)
194: return null;
195:
196: int size;
197: try {
198: size = Integer.parseInt(sizeField.getText());
199: } catch (Exception e) {
200: size = 12;
201: }
202:
203: return new Font(familyField.getText(), styleList
204: .getSelectedIndex(), size);
205: } //}}}
206:
207: //{{{ Private members
208:
209: //{{{ Instance variables
210: private FontSelector fontSelector;
211: private boolean isOK;
212: private JTextField familyField;
213: private JList familyList;
214: private JTextField sizeField;
215: private JList sizeList;
216: private JTextField styleField;
217: private JList styleList;
218: private JLabel preview;
219: private JButton ok;
220: private JButton cancel;
221: //}}}
222:
223: /**
224: * For some reason the default Java fonts show up in the
225: * list with .bold, .bolditalic, and .italic extensions.
226: */
227: private static final String[] HIDEFONTS = { ".bold", ".italic" };
228:
229: //{{{ init() method
230: private void init(Font font) {
231: JPanel content = new JPanel(new BorderLayout());
232: content.setBorder(new EmptyBorder(12, 12, 12, 12));
233: setContentPane(content);
234:
235: JPanel listPanel = new JPanel(new GridLayout(1, 3, 6, 6));
236:
237: String[] fonts;
238: try {
239: fonts = getFontList();
240: } catch (Exception e) {
241: Log.log(Log.ERROR, this , "Broken Java implementation!");
242: /* Log.log(Log.ERROR,this,"Using deprecated Toolkit.getFontList()"); */
243: Log.log(Log.ERROR, this , e);
244:
245: /* fonts = getToolkit().getFontList(); */
246: fonts = new String[] { "Broken Java implementation!" };
247: }
248:
249: JPanel familyPanel = createTextFieldAndListPanel(
250: "font-selector.family", familyField = new JTextField(),
251: familyList = new JList(fonts));
252: listPanel.add(familyPanel);
253:
254: String[] sizes = { "9", "10", "12", "14", "16", "18", "24" };
255: JPanel sizePanel = createTextFieldAndListPanel(
256: "font-selector.size", sizeField = new JTextField(),
257: sizeList = new JList(sizes));
258: listPanel.add(sizePanel);
259:
260: String[] styles = { jEdit.getProperty("font-selector.plain"),
261: jEdit.getProperty("font-selector.bold"),
262: jEdit.getProperty("font-selector.italic"),
263: jEdit.getProperty("font-selector.bolditalic") };
264:
265: JPanel stylePanel = createTextFieldAndListPanel(
266: "font-selector.style", styleField = new JTextField(),
267: styleList = new JList(styles));
268: styleField.setEditable(false);
269: listPanel.add(stylePanel);
270:
271: familyList.setSelectedValue(font.getFamily(), true);
272: familyField.setText(font.getFamily());
273: sizeList.setSelectedValue(String.valueOf(font.getSize()), true);
274: sizeField.setText(String.valueOf(font.getSize()));
275: styleList.setSelectedIndex(font.getStyle());
276: styleField.setText((String) styleList.getSelectedValue());
277:
278: ListHandler listHandler = new ListHandler();
279: familyList.addListSelectionListener(listHandler);
280: sizeList.addListSelectionListener(listHandler);
281: styleList.addListSelectionListener(listHandler);
282:
283: content.add(BorderLayout.NORTH, listPanel);
284:
285: preview = new JLabel(jEdit
286: .getProperty("font-selector.long-text")) {
287: public void paintComponent(Graphics g) {
288: if (fontSelector != null)
289: fontSelector.setAntiAliasEnabled(g);
290: super .paintComponent(g);
291: }
292: };
293: preview.setBorder(new TitledBorder(jEdit
294: .getProperty("font-selector.preview")));
295:
296: updatePreview();
297:
298: Dimension prefSize = preview.getPreferredSize();
299: prefSize.height = 50;
300: preview.setPreferredSize(prefSize);
301:
302: content.add(BorderLayout.CENTER, preview);
303:
304: JPanel buttons = new JPanel();
305: buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
306: buttons.setBorder(new EmptyBorder(12, 0, 0, 0));
307: buttons.add(Box.createGlue());
308:
309: ok = new JButton(jEdit.getProperty("common.ok"));
310: ok.addActionListener(new ActionHandler());
311: getRootPane().setDefaultButton(ok);
312: buttons.add(ok);
313:
314: buttons.add(Box.createHorizontalStrut(6));
315:
316: cancel = new JButton(jEdit.getProperty("common.cancel"));
317: cancel.addActionListener(new ActionHandler());
318: buttons.add(cancel);
319:
320: buttons.add(Box.createGlue());
321:
322: content.add(BorderLayout.SOUTH, buttons);
323:
324: pack();
325: setLocationRelativeTo(getParent());
326: setVisible(true);
327: } //}}}
328:
329: //{{{ getFontList() method
330: private String[] getFontList() {
331: String[] nameArray = GraphicsEnvironment
332: .getLocalGraphicsEnvironment()
333: .getAvailableFontFamilyNames();
334: Vector nameVector = new Vector(nameArray.length);
335:
336: for (int i = 0, j; i < nameArray.length; i++) {
337: for (j = 0; j < HIDEFONTS.length; j++) {
338: if (nameArray[i].indexOf(HIDEFONTS[j]) >= 0)
339: break;
340: }
341:
342: if (j == HIDEFONTS.length)
343: nameVector.addElement(nameArray[i]);
344: }
345:
346: String[] _array = new String[nameVector.size()];
347: nameVector.copyInto(_array);
348: return _array;
349: } //}}}
350:
351: //{{{ createTextFieldAndListPanel() method
352: private JPanel createTextFieldAndListPanel(String label,
353: JTextField textField, JList list) {
354: GridBagLayout layout = new GridBagLayout();
355: JPanel panel = new JPanel(layout);
356:
357: GridBagConstraints cons = new GridBagConstraints();
358: cons.gridx = cons.gridy = 0;
359: cons.gridwidth = cons.gridheight = 1;
360: cons.fill = GridBagConstraints.BOTH;
361: cons.weightx = 1.0f;
362:
363: JLabel _label = new JLabel(jEdit.getProperty(label));
364: layout.setConstraints(_label, cons);
365: panel.add(_label);
366:
367: cons.gridy = 1;
368: Component vs = Box.createVerticalStrut(6);
369: layout.setConstraints(vs, cons);
370: panel.add(vs);
371:
372: cons.gridy = 2;
373: layout.setConstraints(textField, cons);
374: panel.add(textField);
375:
376: cons.gridy = 3;
377: vs = Box.createVerticalStrut(6);
378: layout.setConstraints(vs, cons);
379: panel.add(vs);
380:
381: cons.gridy = 4;
382: cons.gridheight = GridBagConstraints.REMAINDER;
383: cons.weighty = 1.0f;
384: JScrollPane scroller = new JScrollPane(list);
385: layout.setConstraints(scroller, cons);
386: panel.add(scroller);
387:
388: return panel;
389: } //}}}
390:
391: //{{{ updatePreview() method
392: private void updatePreview() {
393: String family = familyField.getText();
394: int size;
395: try {
396: size = Integer.parseInt(sizeField.getText());
397: } catch (Exception e) {
398: size = 12;
399: }
400: int style = styleList.getSelectedIndex();
401:
402: preview.setFont(new Font(family, style, size));
403: } //}}}
404:
405: //}}}
406:
407: //{{{ ActionHandler class
408: class ActionHandler implements ActionListener {
409: public void actionPerformed(ActionEvent evt) {
410: if (evt.getSource() == ok)
411: ok();
412: else if (evt.getSource() == cancel)
413: cancel();
414: }
415: } //}}}
416:
417: //{{{ ListHandler class
418: class ListHandler implements ListSelectionListener {
419: public void valueChanged(ListSelectionEvent evt) {
420: Object source = evt.getSource();
421: if (source == familyList) {
422: String family = (String) familyList.getSelectedValue();
423: if (family != null)
424: familyField.setText(family);
425: } else if (source == sizeList) {
426: String size = (String) sizeList.getSelectedValue();
427: if (size != null)
428: sizeField.setText(size);
429: } else if (source == styleList) {
430: String style = (String) styleList.getSelectedValue();
431: if (style != null)
432: styleField.setText(style);
433: }
434:
435: updatePreview();
436: }
437: } //}}}
438: } //}}}
|