2 GNU Lesser General Public License
5 Copyright (C) 2003 Howard Kistler
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.
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.
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
22 package com
.hexidec
.ekit
.component
;
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
;
43 import com
.hexidec
.util
.Translatrix
;
45 /** Class for providing a dialog that lets the user specify values for tag attributes
47 public class FontSelectorDialog
extends JDialog
implements ItemListener
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
;
56 public FontSelectorDialog(Frame parent
, String title
, boolean bModal
, String attribName
, String demoText
)
58 super(parent
, title
, bModal
);
60 if(demoText
!= null
&& demoText
.length() > 0)
62 if(demoText
.length() > 24)
64 defaultText
= demoText
.substring(0, 24);
68 defaultText
= demoText
;
73 defaultText
= "aAbBcCdDeEfFgGhH,.0123";
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
++)
81 if(!fonts
[i
].equals("Dialog") && !fonts
[i
].equals("DialogInput") && !fonts
[i
].equals("Monospaced") && !fonts
[i
].equals("SansSerif") && !fonts
[i
].equals("Serif"))
83 vcFontnames
.add(fonts
[i
]);
86 jcmbFontlist
= new JComboBox(vcFontnames
);
87 jcmbFontlist
.addItemListener(this);
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") };
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
);
104 addWindowListener(new WindowAdapter() {
105 public void windowClosing(WindowEvent we
)
107 jOptionPane
.setValue(new Integer(JOptionPane
.CLOSED_OPTION
));
111 jOptionPane
.addPropertyChangeListener(new PropertyChangeListener() {
112 public void propertyChange(PropertyChangeEvent e
)
114 String prop
= e
.getPropertyName();
116 && (e
.getSource() == jOptionPane
)
117 && (prop
.equals(JOptionPane
.VALUE_PROPERTY
) || prop
.equals(JOptionPane
.INPUT_VALUE_PROPERTY
)))
119 Object value
= jOptionPane
.getValue();
120 if(value
== JOptionPane
.UNINITIALIZED_VALUE
)
124 jOptionPane
.setValue(JOptionPane
.UNINITIALIZED_VALUE
);
125 if(value
.equals(buttonLabels
[0]))
127 fontName
= (String
)(jcmbFontlist
.getSelectedItem());
142 /* ItemListener method */
143 public void itemStateChanged(ItemEvent ie
)
145 if(ie
.getStateChange() == ItemEvent
.SELECTED
)
147 jtpFontPreview
.setText(getFontSampleString(defaultText
));
151 public FontSelectorDialog(Frame parent
, String title
, boolean bModal
, String attribName
)
153 this(parent
, title
, bModal
, attribName
, "");
156 public String
getFontName()
161 private String
getFontSampleString(String demoText
)
163 return "<HTML><BODY><FONT FACE=" + '"' + jcmbFontlist
.getSelectedItem() + '"' + ">" + demoText
+ "</FONT></BODY></HTML>";