Initial revision
[old-projects.git] / ekit / com / hexidec / ekit / component / FontSelectorDialog.java
1 /*
2 GNU Lesser General Public License
3
4 FontSelectorDialog
5 Copyright (C) 2003 Howard Kistler
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 package com.hexidec.ekit.component;
23
24 import java.awt.Frame;
25 import java.awt.GraphicsEnvironment;
26 import java.awt.Insets;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.ItemEvent;
30 import java.awt.event.ItemListener;
31 import java.awt.event.WindowAdapter;
32 import java.awt.event.WindowEvent;
33 import java.beans.PropertyChangeEvent;
34 import java.beans.PropertyChangeListener;
35 import java.util.Vector;
36 import javax.swing.JComboBox;
37 import javax.swing.JDialog;
38 import javax.swing.JOptionPane;
39 import javax.swing.JTextPane;
40 import javax.swing.text.html.HTMLDocument;
41 import javax.swing.text.html.HTMLEditorKit;
42
43 import com.hexidec.util.Translatrix;
44
45 /** Class for providing a dialog that lets the user specify values for tag attributes
46 */
47 public class FontSelectorDialog extends JDialog implements ItemListener
48 {
49 private Vector vcFontnames = (Vector)null;
50 private final JComboBox jcmbFontlist;
51 private String fontName = new String();
52 private JOptionPane jOptionPane;
53 private final JTextPane jtpFontPreview;
54 private String defaultText;
55
56 public FontSelectorDialog(Frame parent, String title, boolean bModal, String attribName, String demoText)
57 {
58 super(parent, title, bModal);
59
60 if(demoText != null && demoText.length() > 0)
61 {
62 if(demoText.length() > 24)
63 {
64 defaultText = demoText.substring(0, 24);
65 }
66 else
67 {
68 defaultText = demoText;
69 }
70 }
71 else
72 {
73 defaultText = "aAbBcCdDeEfFgGhH,.0123";
74 }
75
76 /* Obtain available fonts */
77 String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
78 vcFontnames = new Vector(fonts.length - 5);
79 for(int i = 0; i < fonts.length; i++)
80 {
81 if(!fonts[i].equals("Dialog") && !fonts[i].equals("DialogInput") && !fonts[i].equals("Monospaced") && !fonts[i].equals("SansSerif") && !fonts[i].equals("Serif"))
82 {
83 vcFontnames.add(fonts[i]);
84 }
85 }
86 jcmbFontlist = new JComboBox(vcFontnames);
87 jcmbFontlist.addItemListener(this);
88
89 jtpFontPreview = new JTextPane();
90 final HTMLEditorKit kitFontPreview = new HTMLEditorKit();
91 final HTMLDocument docFontPreview = (HTMLDocument)(kitFontPreview.createDefaultDocument());
92 jtpFontPreview.setEditorKit(kitFontPreview);
93 jtpFontPreview.setDocument(docFontPreview);
94 jtpFontPreview.setMargin(new Insets(4, 4, 4, 4));
95 jtpFontPreview.setBounds(0, 0, 120, 18);
96 jtpFontPreview.setText(getFontSampleString(defaultText));
97 Object[] panelContents = { attribName, jcmbFontlist, Translatrix.getTranslationString("FontSample"), jtpFontPreview };
98 final Object[] buttonLabels = { Translatrix.getTranslationString("DialogAccept"), Translatrix.getTranslationString("DialogCancel") };
99
100 jOptionPane = new JOptionPane(panelContents, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, buttonLabels, buttonLabels[0]);
101 setContentPane(jOptionPane);
102 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
103
104 addWindowListener(new WindowAdapter() {
105 public void windowClosing(WindowEvent we)
106 {
107 jOptionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
108 }
109 });
110
111 jOptionPane.addPropertyChangeListener(new PropertyChangeListener() {
112 public void propertyChange(PropertyChangeEvent e)
113 {
114 String prop = e.getPropertyName();
115 if(isVisible()
116 && (e.getSource() == jOptionPane)
117 && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop.equals(JOptionPane.INPUT_VALUE_PROPERTY)))
118 {
119 Object value = jOptionPane.getValue();
120 if(value == JOptionPane.UNINITIALIZED_VALUE)
121 {
122 return;
123 }
124 jOptionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
125 if(value.equals(buttonLabels[0]))
126 {
127 fontName = (String)(jcmbFontlist.getSelectedItem());
128 setVisible(false);
129 }
130 else
131 {
132 fontName = null;
133 setVisible(false);
134 }
135 }
136 }
137 });
138 this.pack();
139 this.show();
140 }
141
142 /* ItemListener method */
143 public void itemStateChanged(ItemEvent ie)
144 {
145 if(ie.getStateChange() == ItemEvent.SELECTED)
146 {
147 jtpFontPreview.setText(getFontSampleString(defaultText));
148 }
149 }
150
151 public FontSelectorDialog(Frame parent, String title, boolean bModal, String attribName)
152 {
153 this(parent, title, bModal, attribName, "");
154 }
155
156 public String getFontName()
157 {
158 return fontName;
159 }
160
161 private String getFontSampleString(String demoText)
162 {
163 return "<HTML><BODY><FONT FACE=" + '"' + jcmbFontlist.getSelectedItem() + '"' + ">" + demoText + "</FONT></BODY></HTML>";
164 }
165
166 }
167