001: package net.xoetrope.builder.editor.dialog;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Container;
006: import java.awt.Dimension;
007: import java.awt.Font;
008: import java.awt.Frame;
009: import java.awt.GraphicsEnvironment;
010: import java.awt.GridLayout;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import javax.swing.Box;
014: import javax.swing.BoxLayout;
015: import javax.swing.JButton;
016: import javax.swing.JCheckBox;
017: import javax.swing.JDialog;
018: import javax.swing.JLabel;
019: import javax.swing.JList;
020: import javax.swing.JPanel;
021: import javax.swing.JScrollPane;
022: import javax.swing.JTextArea;
023: import javax.swing.border.EmptyBorder;
024: import javax.swing.event.ListSelectionEvent;
025: import javax.swing.event.ListSelectionListener;
026:
027: import net.xoetrope.builder.editor.XEditorUtilities;
028: import net.xoetrope.builder.editor.XuiDefaults;
029:
030: /**
031: * An font selection dialog
032: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
033: * $Revision: 1.3 $
034: */
035: public class XFontChooserDialog extends JDialog {
036: private JLabel font;
037: private JList fontChoices, sizeChoices;
038: private JScrollPane fontChoiceScroll, previewScroll, sizeScroll;
039: private JCheckBox checkBold, checkItalic;
040:
041: private JTextArea previewTextArea;
042: private static JButton apply, cancel;
043:
044: private Font previewFont;
045:
046: private static final String SAMPLE_TEXT = "How does this font fit?";
047:
048: /**
049: * Create a font chooser
050: */
051: public XFontChooserDialog(Frame c, Font initialFont, String title,
052: String sampleText) {
053: super (c);
054: setTitle(title);
055: setModal(true);
056:
057: if (initialFont != null)
058: previewFont = initialFont;
059: else
060: previewFont = getAvailableFont();
061:
062: Container dialogPanel = getContentPane();
063: JPanel contentPanel = new JPanel();
064: contentPanel.setBorder(new EmptyBorder(8, 8, 8, 8));
065: contentPanel.setLayout(new BorderLayout());
066:
067: JPanel topPanel = new JPanel(new BorderLayout());
068:
069: //--------------------------------------------------------------------------
070: fontChoices = new JList(systemFonts);
071: fontChoices.setFont(XuiDefaults.defaultFont);
072:
073: //Set up initial Font or 0 if none...
074: fontChoices.setSelectedIndex(0);
075: if (previewFont != null) {
076: String previewFontName = previewFont.getFontName();
077: int i = 0;
078: for (; i < systemFonts.length; i++) {
079: if (systemFonts[i].equalsIgnoreCase(previewFontName)) {
080: fontChoices.setSelectedIndex(i);
081: break;
082: }
083: }
084: if ((i == systemFonts.length)
085: && (previewFontName.indexOf(' ') > 0)) {
086: previewFontName = previewFontName.substring(0,
087: previewFontName.indexOf(' '));
088: for (i = 0; i < systemFonts.length; i++) {
089: if (systemFonts[i]
090: .equalsIgnoreCase(previewFontName)) {
091: fontChoices.setSelectedIndex(i);
092: break;
093: }
094: }
095: }
096: if (i == systemFonts.length) {
097: previewFont = getAvailableFont();
098: fontChoices.setSelectedIndex(0);
099: }
100: }
101:
102: fontChoices
103: .addListSelectionListener(new ListSelectionListener() {
104: public void valueChanged(ListSelectionEvent ae) {
105: updateSelection();
106: }
107: });
108:
109: fontChoiceScroll = new JScrollPane(fontChoices);
110: fontChoiceScroll.setPreferredSize(new Dimension(200, 150));
111: topPanel.add(fontChoiceScroll, BorderLayout.WEST);
112: //--------------------------------------------------------------------------
113:
114: //--------------------------------------------------------------------------
115: sizeChoices = new JList(fontSizeArray);
116: sizeChoices.setFont(XuiDefaults.defaultFont);
117: //Set up initial Font size or the 12th size (28ish)
118: sizeChoices.setSelectedIndex(12);
119:
120: if (previewFont != null) {
121: for (int j = 0; j < fontSizeArray.length; j++) {
122: if (Integer.parseInt(fontSizeArray[j]) == previewFont
123: .getSize())
124: sizeChoices.setSelectedIndex(j);
125: }
126: }
127:
128: sizeChoices
129: .addListSelectionListener(new ListSelectionListener() {
130: public void valueChanged(ListSelectionEvent ae) {
131: updateSelection();
132: }
133: });
134:
135: sizeScroll = new JScrollPane(sizeChoices);
136: sizeScroll.setPreferredSize(new Dimension(48, 150));
137: topPanel.add(sizeScroll, BorderLayout.CENTER);
138: //--------------------------------------------------------------------------
139:
140: //--------------------------------------------------------------------------
141: JPanel upperRight = new JPanel(new GridLayout(3, 1, 5, 3));
142: JPanel stylePanel = new JPanel(new GridLayout(2, 1));
143: stylePanel.setBorder(new EmptyBorder(8, 8, 8, 8));
144:
145: checkBold = new JCheckBox("Bold");
146: checkBold.setFont(XuiDefaults.defaultFont);
147: checkBold.addActionListener(new ActionListener() {
148: public void actionPerformed(ActionEvent ae) {
149: updateSelection();
150: }
151: });
152:
153: if ((previewFont != null) && (previewFont.isBold()))
154: checkBold.setSelected(true);
155:
156: stylePanel.add(checkBold);
157:
158: checkItalic = new JCheckBox("Italic");
159: checkItalic.setFont(XuiDefaults.defaultFont);
160: checkItalic.addActionListener(new ActionListener() {
161: public void actionPerformed(ActionEvent ae) {
162: updateSelection();
163: }
164: });
165: // Set initial style.
166: if ((previewFont != null) && (previewFont.isItalic()))
167: checkItalic.setSelected(true);
168:
169: stylePanel.add(checkItalic);
170:
171: upperRight.add(stylePanel);
172: //--------------------------------------------------------------------------
173:
174: JPanel status = new JPanel(new GridLayout(2, 1));
175: status.setBorder(new EmptyBorder(8, 8, 8, 8));
176:
177: JLabel jl = new JLabel("Current Selection:");
178: jl.setFont(XuiDefaults.defaultFont);
179: status.add(jl);
180:
181: font = new JLabel();
182: font.setFont(XuiDefaults.defaultFont);
183: font.setForeground(Color.black);
184: status.add(font);
185:
186: upperRight.add(status);
187:
188: topPanel.add(upperRight, BorderLayout.EAST);
189:
190: previewTextArea = new JTextArea(
191: sampleText == null ? SAMPLE_TEXT : sampleText);
192: previewScroll = new JScrollPane(previewTextArea);
193: previewScroll.setPreferredSize(new Dimension(400, 80));
194:
195: JLabel spacer = new JLabel();
196: spacer.setBorder(new EmptyBorder(4, 4, 4, 4));
197:
198: JPanel spacerPanel = new JPanel(new BorderLayout());
199: spacerPanel.add(spacer, BorderLayout.NORTH);
200: spacerPanel.add(previewScroll, BorderLayout.CENTER);
201:
202: JPanel buttons = new JPanel();
203: buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
204: buttons.setBorder(new EmptyBorder(8, 8, 0, 0));
205:
206: buttons.add(Box.createHorizontalGlue());
207: buttons.add(cancel = new JButton("Cancel"));
208: cancel.setFont(XuiDefaults.defaultFont);
209: cancel.setMnemonic('C');
210:
211: cancel.addActionListener(new ActionListener() {
212: public void actionPerformed(ActionEvent ae) {
213: previewFont = null;
214: dispose();
215: setVisible(false);
216: }
217: });
218:
219: buttons.add(apply = new JButton("OK"));
220: apply.setMnemonic('O');
221: apply.setFont(XuiDefaults.defaultFont);
222:
223: apply.addActionListener(new ActionListener() {
224: public void actionPerformed(ActionEvent ae) {
225: previewFont = getSelectedFont();
226: dispose();
227: setVisible(false);
228: }
229: });
230:
231: JPanel bottomPanel = new JPanel();
232: bottomPanel.setLayout(new BorderLayout());
233: bottomPanel.add(spacerPanel, BorderLayout.NORTH);
234: bottomPanel.add(buttons, BorderLayout.SOUTH);
235:
236: contentPanel.add(topPanel, BorderLayout.CENTER);
237: contentPanel.add(bottomPanel, BorderLayout.SOUTH);
238:
239: dialogPanel.add(contentPanel);
240:
241: updateSelection();
242: }
243:
244: /**
245: * Shows the dialog
246: * @return Font the selected font or null if cancel was choosen.
247: */
248: public Font showDialog() {
249: pack();
250: setResizable(false);
251:
252: setVisible(true);
253: return previewFont;
254: }
255:
256: private static Font getAvailableFont() {
257: return new Font(systemFonts[0], Font.PLAIN, 18);
258: }
259:
260: private Font getSelectedFont() {
261: String s = (String) sizeChoices.getSelectedValue();
262: if (s != null) {
263: int i = Integer.parseInt(s);
264: int style = 0;
265: if (checkBold.isSelected())
266: style |= Font.BOLD;
267: if (checkItalic.isSelected())
268: style |= Font.ITALIC;
269:
270: return new Font((String) fontChoices.getSelectedValue(),
271: style, i);
272: } else
273: return null;
274: }
275:
276: private void updateSelection() {
277: Font newFont = getSelectedFont();
278: if ((newFont != null) && (previewTextArea != null)) {
279: previewTextArea.setFont(newFont);
280: if (newFont != null)
281: previewFont = newFont;
282:
283: String name;
284: if (previewFont.getName().length() > 11)
285: name = previewFont.getName().substring(0, 12);
286: else
287: name = previewFont.getName();
288:
289: font.setText(name + " " + previewFont.getSize() + "pt.");
290: }
291: }
292:
293: private String fontSizeArray[] = { "8", "9", "10", "11", "12",
294: "13", "14", "16", "18", "20", "24", "26", "28", "32", "36",
295: "40", "48", "56", "64", "72" };
296:
297: private static String[] systemFonts = GraphicsEnvironment
298: .getLocalGraphicsEnvironment()
299: .getAvailableFontFamilyNames();
300:
301: }
|