001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.swing.plaf.windows;
018:
019: import com.l2fprod.common.swing.JFontChooser;
020: import com.l2fprod.common.swing.LookAndFeelTweaks;
021: import com.l2fprod.common.swing.PercentLayout;
022: import com.l2fprod.common.swing.plaf.FontChooserUI;
023:
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.font.TextAttribute;
029: import java.beans.PropertyChangeEvent;
030: import java.beans.PropertyChangeListener;
031: import java.util.HashMap;
032: import java.util.Map;
033: import java.util.ResourceBundle;
034:
035: import javax.swing.*;
036: import javax.swing.event.DocumentEvent;
037: import javax.swing.event.DocumentListener;
038: import javax.swing.event.ListSelectionEvent;
039: import javax.swing.event.ListSelectionListener;
040: import javax.swing.plaf.ComponentUI;
041:
042: /**
043: * Windows implementation of the JFontChooser pluggable UI.
044: */
045: public class WindowsFontChooserUI extends FontChooserUI {
046:
047: public static ComponentUI createUI(JComponent component) {
048: return new WindowsFontChooserUI();
049: }
050:
051: private JFontChooser chooser;
052:
053: private JPanel fontPanel;
054: private JTextField fontField;
055: private JList fontList;
056:
057: private JTextField fontEffectField;
058: private JList fontEffectList;
059:
060: private JPanel fontSizePanel;
061: private JTextField fontSizeField;
062: private JList fontSizeList;
063:
064: private JTextArea previewPanel;
065: private JComboBox charSetCombo;
066:
067: private PropertyChangeListener propertyListener;
068:
069: public void installUI(JComponent c) {
070: super .installUI(c);
071:
072: chooser = (JFontChooser) c;
073:
074: installComponents();
075: installListeners();
076: }
077:
078: protected void installComponents() {
079: JLabel label;
080:
081: ResourceBundle bundle = ResourceBundle
082: .getBundle(FontChooserUI.class.getName() + "RB");
083:
084: // FIRST PANEL with Font list
085: fontPanel = new JPanel(new PercentLayout(
086: PercentLayout.VERTICAL, 2));
087: fontPanel.add(label = new JLabel(bundle
088: .getString("FontChooserUI.fontLabel")));
089: fontPanel.add(fontField = new JTextField(25));
090: fontField.setEditable(false);
091: fontPanel.add(new JScrollPane(fontList = new JList()), "*");
092: label.setLabelFor(fontList);
093: label.setDisplayedMnemonic(bundle.getString(
094: "FontChooserUI.fontLabel.mnemonic").charAt(0));
095: fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
096:
097: String[] fontFamilies = chooser.getModel()
098: .getFontFamilies(null);
099: fontList.setListData(fontFamilies);
100:
101: // SECOND PANEL with Bold, Italic, Charset
102: JPanel fontEffectPanel = new JPanel(new PercentLayout(
103: PercentLayout.VERTICAL, 2));
104: fontEffectPanel.add(label = new JLabel(bundle
105: .getString("FontChooserUI.styleLabel")));
106: fontEffectPanel.add(fontEffectField = new JTextField(10));
107: fontEffectField.setEditable(false);
108: fontEffectPanel.add(new JScrollPane(
109: fontEffectList = new JList()), "*");
110: label.setLabelFor(fontEffectList);
111: label.setDisplayedMnemonic(bundle.getString(
112: "FontChooserUI.styleLabel.mnemonic").charAt(0));
113: fontEffectList
114: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
115:
116: FontStyle[] fontStyles = new FontStyle[] {
117: new FontStyle(Font.PLAIN, bundle
118: .getString("FontChooserUI.style.plain")),
119: new FontStyle(Font.BOLD, bundle
120: .getString("FontChooserUI.style.bold")),
121: new FontStyle(Font.ITALIC, bundle
122: .getString("FontChooserUI.style.italic")),
123: new FontStyle(Font.BOLD | Font.ITALIC, bundle
124: .getString("FontChooserUI.style.bolditalic")), };
125: fontEffectList.setListData(fontStyles);
126:
127: // The SIZE PANEL
128: fontSizePanel = new JPanel(new PercentLayout(
129: PercentLayout.VERTICAL, 2));
130: fontSizePanel.add(label = new JLabel(bundle
131: .getString("FontChooserUI.sizeLabel")));
132:
133: label.setDisplayedMnemonic(bundle.getString(
134: "FontChooserUI.sizeLabel.mnemonic").charAt(0));
135:
136: fontSizePanel.add(fontSizeField = new JTextField(5));
137: label.setLabelFor(fontSizeField);
138: fontSizePanel.add(new JScrollPane(fontSizeList = new JList()),
139: "*");
140:
141: int[] defaultFontSizes = chooser.getModel().getDefaultSizes();
142: String[] sizes = new String[defaultFontSizes.length];
143: for (int i = 0, c = sizes.length; i < c; i++) {
144: sizes[i] = String.valueOf(defaultFontSizes[i]);
145: }
146: fontSizeList.setPrototypeCellValue("012345");
147: fontSizeList.setListData(sizes);
148: fontSizeList
149: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
150: fontSizeList.setVisibleRowCount(2);
151:
152: chooser.setLayout(LookAndFeelTweaks.createBorderLayout());
153: JPanel panel = new JPanel();
154: panel.setLayout(LookAndFeelTweaks
155: .createHorizontalPercentLayout());
156: panel.add(fontPanel, "*");
157: panel.add(fontEffectPanel);
158: panel.add(fontSizePanel);
159:
160: previewPanel = new JTextArea();
161: previewPanel
162: .setText(chooser.getModel().getPreviewMessage(null));
163: JScrollPane scroll = new JScrollPane(previewPanel);
164:
165: JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
166: split.setBorder(null);
167: split.setTopComponent(panel);
168: split.setBottomComponent(scroll);
169: split.setDividerLocation(0.5);
170: split.setOneTouchExpandable(true);
171: chooser.add("Center", split);
172:
173: // allow the split pane to completely hide the top panel
174: panel.setMinimumSize(new Dimension(0, 0));
175:
176: JPanel charSetPanel = new JPanel(new PercentLayout(
177: PercentLayout.HORIZONTAL, 2));
178: label = new JLabel("CHARSET");
179: label.setHorizontalAlignment(JLabel.RIGHT);
180: charSetPanel.add(label, "*");
181:
182: charSetCombo = new JComboBox(chooser.getModel().getCharSets());
183: charSetPanel.add(charSetCombo);
184: // PENDING(fred) implement charset
185: // chooser.add("South", charSetPanel);
186: }
187:
188: protected void installListeners() {
189: SelectedFontUpdater listener = new SelectedFontUpdater();
190: fontList.addListSelectionListener(listener);
191: fontEffectList.addListSelectionListener(listener);
192: fontSizeList.addListSelectionListener(listener);
193: fontSizeField.getDocument().addDocumentListener(listener);
194:
195: propertyListener = createPropertyChangeListener();
196: chooser.addPropertyChangeListener(
197: JFontChooser.SELECTED_FONT_CHANGED_KEY,
198: propertyListener);
199: }
200:
201: public void uninstallUI(JComponent c) {
202: chooser.remove(fontPanel);
203: chooser.remove(fontSizePanel);
204:
205: super .uninstallUI(c);
206: }
207:
208: public void uninstallListeners() {
209: chooser.removePropertyChangeListener(propertyListener);
210: }
211:
212: protected PropertyChangeListener createPropertyChangeListener() {
213: return new PropertyChangeListener() {
214:
215: public void propertyChange(PropertyChangeEvent evt) {
216: updateDisplay();
217: }
218: };
219: }
220:
221: private void updateDisplay() {
222: Font selected = chooser.getSelectedFont();
223: if (selected != null) {
224: /** PENDING(fred) implement charset
225: String charset = (String)charSetCombo.getSelectedItem();
226: String text = chooser.getModel().getPreviewMessage(charset);
227: boolean canDisplay = selected.canDisplayUpTo(text) == -1;
228: if (canDisplay) {
229: previewPanel.setText(text);
230: } else {
231: previewPanel.setText("Charset not supported");
232: }
233: **/
234: previewPanel.setFont(selected);
235: fontList.setSelectedValue(selected.getName(), true);
236: fontSizeField.setText(String.valueOf(selected.getSize()));
237: fontSizeList.setSelectedValue(String.valueOf(selected
238: .getSize()), true);
239:
240: FontStyle style = new FontStyle(selected.getStyle(), null);
241: fontEffectList.setSelectedValue(style, true);
242: style = (FontStyle) fontEffectList.getSelectedValue();
243: fontEffectField.setText(style.toString());
244: }
245: }
246:
247: private void updateSelectedFont() {
248: Font currentFont = chooser.getSelectedFont();
249: String fontFamily = currentFont == null ? "SansSerif"
250: : currentFont.getName();
251: int fontSize = currentFont == null ? 11 : currentFont.getSize();
252:
253: if (fontList.getSelectedIndex() >= 0) {
254: fontFamily = (String) fontList.getSelectedValue();
255: }
256:
257: if (fontSizeField.getText().trim().length() > 0) {
258: try {
259: fontSize = Integer.parseInt(fontSizeField.getText()
260: .trim());
261: } catch (Exception e) {
262: // ignore the NumberFormatException
263: }
264: }
265:
266: Map attributes = new HashMap();
267: attributes.put(TextAttribute.SIZE, new Float(fontSize));
268: attributes.put(TextAttribute.FAMILY, fontFamily);
269:
270: FontStyle style = (FontStyle) fontEffectList.getSelectedValue();
271: if (style != null) {
272: if (style.isBold()) {
273: attributes.put(TextAttribute.WEIGHT,
274: TextAttribute.WEIGHT_BOLD);
275: }
276: if (style.isItalic()) {
277: attributes.put(TextAttribute.POSTURE,
278: TextAttribute.POSTURE_OBLIQUE);
279: }
280: }
281:
282: Font font = Font.getFont(attributes);
283: if (!font.equals(currentFont)) {
284: chooser.setSelectedFont(font);
285: previewPanel.setFont(font);
286: }
287: }
288:
289: private class SelectedFontUpdater implements ListSelectionListener,
290: DocumentListener, ActionListener {
291:
292: public void valueChanged(ListSelectionEvent e) {
293: if (fontList == e.getSource()
294: && fontList.getSelectedValue() != null) {
295: fontField.setText((String) fontList.getSelectedValue());
296: }
297: if (fontSizeList == e.getSource()
298: && fontSizeList.getSelectedValue() != null) {
299: fontSizeField.setText((String) fontSizeList
300: .getSelectedValue());
301: }
302: updateSelectedFont();
303: }
304:
305: public void changedUpdate(DocumentEvent e) {
306: updateLater();
307: }
308:
309: public void insertUpdate(DocumentEvent e) {
310: updateLater();
311: }
312:
313: public void removeUpdate(DocumentEvent e) {
314: updateLater();
315: }
316:
317: public void actionPerformed(ActionEvent e) {
318: updateLater();
319: }
320:
321: void updateLater() {
322: SwingUtilities.invokeLater(new Runnable() {
323:
324: public void run() {
325: updateSelectedFont();
326: }
327: });
328: }
329: }
330:
331: static private class FontStyle {
332:
333: String display;
334: int value;
335:
336: public FontStyle(int value, String display) {
337: this .value = value;
338: this .display = display;
339: }
340:
341: public int value() {
342: return value;
343: }
344:
345: public String toString() {
346: return display;
347: }
348:
349: public boolean isBold() {
350: return (value & Font.BOLD) != 0;
351: }
352:
353: public boolean isItalic() {
354: return (value & Font.ITALIC) != 0;
355: }
356:
357: public int hashCode() {
358: return value;
359: }
360:
361: public boolean equals(Object obj) {
362: return (obj instanceof FontStyle)
363: && (((FontStyle) obj).value == value);
364: }
365:
366: }
367:
368: }
|