Initial revision
[old-projects.git] / ekit / com / hexidec / ekit / component / FontSelectorDialog.java
CommitLineData
c2da4d40
JL
1/*
2GNU Lesser General Public License
3
4FontSelectorDialog
5Copyright (C) 2003 Howard Kistler
6
7This library is free software; you can redistribute it and/or
8modify it under the terms of the GNU Lesser General Public
9License as published by the Free Software Foundation; either
10version 2.1 of the License, or (at your option) any later version.
11
12This library is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public
18License along with this library; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20*/
21
22package com.hexidec.ekit.component;
23
24import java.awt.Frame;
25import java.awt.GraphicsEnvironment;
26import java.awt.Insets;
27import java.awt.event.ActionEvent;
28import java.awt.event.ActionListener;
29import java.awt.event.ItemEvent;
30import java.awt.event.ItemListener;
31import java.awt.event.WindowAdapter;
32import java.awt.event.WindowEvent;
33import java.beans.PropertyChangeEvent;
34import java.beans.PropertyChangeListener;
35import java.util.Vector;
36import javax.swing.JComboBox;
37import javax.swing.JDialog;
38import javax.swing.JOptionPane;
39import javax.swing.JTextPane;
40import javax.swing.text.html.HTMLDocument;
41import javax.swing.text.html.HTMLEditorKit;
42
43import com.hexidec.util.Translatrix;
44
45/** Class for providing a dialog that lets the user specify values for tag attributes
46 */
47public 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