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.basic;
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: * BasicFontChooserUI. <br>
044: *
045: */
046: public class BasicFontChooserUI extends FontChooserUI {
047:
048: public static ComponentUI createUI(JComponent component) {
049: return new BasicFontChooserUI();
050: }
051:
052: private JFontChooser chooser;
053:
054: private JPanel fontPanel;
055: private JTextField fontField;
056: private JList fontList;
057:
058: private JPanel fontSizePanel;
059: private JTextField fontSizeField;
060: private JList fontSizeList;
061:
062: private JCheckBox boldCheck;
063: private JCheckBox italicCheck;
064:
065: private JTextArea previewPanel;
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: fontPanel = new JPanel(new PercentLayout(
085: PercentLayout.VERTICAL, 2));
086: fontPanel.add(label = new JLabel(bundle
087: .getString("FontChooserUI.fontLabel")));
088: fontPanel.add(fontField = new JTextField(25));
089: fontField.setEditable(false);
090: fontPanel.add(new JScrollPane(fontList = new JList()), "*");
091: label.setLabelFor(fontList);
092: label.setDisplayedMnemonic(bundle.getString(
093: "FontChooserUI.fontLabel.mnemonic").charAt(0));
094: fontList.setVisibleRowCount(7);
095: fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
096:
097: String[] fontFamilies = chooser.getModel()
098: .getFontFamilies(null);
099: fontList.setListData(fontFamilies);
100:
101: fontSizePanel = new JPanel(new PercentLayout(
102: PercentLayout.VERTICAL, 2));
103:
104: fontSizePanel.add(label = new JLabel(bundle
105: .getString("FontChooserUI.styleLabel")));
106: fontSizePanel.add(boldCheck = new JCheckBox(bundle
107: .getString("FontChooserUI.style.bold")));
108: fontSizePanel.add(italicCheck = new JCheckBox(bundle
109: .getString("FontChooserUI.style.italic")));
110: boldCheck.setMnemonic(bundle.getString(
111: "FontChooserUI.style.bold.mnemonic").charAt(0));
112: italicCheck.setMnemonic(bundle.getString(
113: "FontChooserUI.style.italic.mnemonic").charAt(0));
114:
115: fontSizePanel.add(label = new JLabel(bundle
116: .getString("FontChooserUI.sizeLabel")));
117:
118: label.setDisplayedMnemonic(bundle.getString(
119: "FontChooserUI.sizeLabel.mnemonic").charAt(0));
120:
121: fontSizePanel.add(fontSizeField = new JTextField());
122: label.setLabelFor(fontSizeField);
123: fontSizePanel.add(new JScrollPane(fontSizeList = new JList()),
124: "*");
125:
126: int[] defaultFontSizes = chooser.getModel().getDefaultSizes();
127: String[] sizes = new String[defaultFontSizes.length];
128: for (int i = 0, c = sizes.length; i < c; i++) {
129: sizes[i] = String.valueOf(defaultFontSizes[i]);
130: }
131: fontSizeList.setListData(sizes);
132: fontSizeList
133: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
134: fontSizeList.setVisibleRowCount(2);
135:
136: chooser.setLayout(LookAndFeelTweaks.createBorderLayout());
137: JPanel panel = new JPanel();
138: panel.setLayout(LookAndFeelTweaks
139: .createHorizontalPercentLayout());
140: panel.add(fontPanel, "*");
141: panel.add(fontSizePanel);
142: chooser.add("Center", panel);
143:
144: previewPanel = new JTextArea();
145: previewPanel.setPreferredSize(new Dimension(100, 40));
146: previewPanel
147: .setText(chooser.getModel().getPreviewMessage(null));
148: JScrollPane scroll = new JScrollPane(previewPanel);
149: chooser.add("South", scroll);
150: }
151:
152: protected void installListeners() {
153: SelectedFontUpdater listener = new SelectedFontUpdater();
154: fontList.addListSelectionListener(listener);
155: fontSizeList.addListSelectionListener(listener);
156: fontSizeField.getDocument().addDocumentListener(listener);
157: boldCheck.addActionListener(listener);
158: italicCheck.addActionListener(listener);
159:
160: propertyListener = createPropertyChangeListener();
161: chooser.addPropertyChangeListener(
162: JFontChooser.SELECTED_FONT_CHANGED_KEY,
163: propertyListener);
164: }
165:
166: public void uninstallUI(JComponent c) {
167: chooser.remove(fontPanel);
168: chooser.remove(fontSizePanel);
169:
170: super .uninstallUI(c);
171: }
172:
173: public void uninstallListeners() {
174: chooser.removePropertyChangeListener(propertyListener);
175: }
176:
177: protected PropertyChangeListener createPropertyChangeListener() {
178: return new PropertyChangeListener() {
179: public void propertyChange(PropertyChangeEvent evt) {
180: updateDisplay();
181: }
182: };
183: }
184:
185: private void updateDisplay() {
186: Font selected = chooser.getSelectedFont();
187: if (selected != null) {
188: previewPanel.setFont(selected);
189: fontList.setSelectedValue(selected.getName(), true);
190: fontSizeField.setText(String.valueOf(selected.getSize()));
191: fontSizeList.setSelectedValue(String.valueOf(selected
192: .getSize()), true);
193: boldCheck.setSelected(selected.isBold());
194: italicCheck.setSelected(selected.isItalic());
195: }
196: }
197:
198: private void updateSelectedFont() {
199: Font currentFont = chooser.getSelectedFont();
200: String fontFamily = currentFont == null ? "SansSerif"
201: : currentFont.getName();
202: int fontSize = currentFont == null ? 11 : currentFont.getSize();
203:
204: if (fontList.getSelectedIndex() >= 0) {
205: fontFamily = (String) fontList.getSelectedValue();
206: }
207:
208: if (fontSizeField.getText().trim().length() > 0) {
209: try {
210: fontSize = Integer.parseInt(fontSizeField.getText()
211: .trim());
212: } catch (Exception e) {
213: // ignore the NumberFormatException
214: }
215: }
216:
217: Map attributes = new HashMap();
218: attributes.put(TextAttribute.SIZE, new Float(fontSize));
219: attributes.put(TextAttribute.FAMILY, fontFamily);
220: if (boldCheck.isSelected()) {
221: attributes.put(TextAttribute.WEIGHT,
222: TextAttribute.WEIGHT_BOLD);
223: }
224: if (italicCheck.isSelected()) {
225: attributes.put(TextAttribute.POSTURE,
226: TextAttribute.POSTURE_OBLIQUE);
227: }
228:
229: Font font = Font.getFont(attributes);
230: if (!font.equals(currentFont)) {
231: chooser.setSelectedFont(font);
232: previewPanel.setFont(font);
233: }
234: }
235:
236: private class SelectedFontUpdater implements ListSelectionListener,
237: DocumentListener, ActionListener {
238: public void valueChanged(ListSelectionEvent e) {
239: if (fontList == e.getSource()
240: && fontList.getSelectedValue() != null) {
241: fontField.setText((String) fontList.getSelectedValue());
242: }
243: if (fontSizeList == e.getSource()
244: && fontSizeList.getSelectedValue() != null) {
245: fontSizeField.setText((String) fontSizeList
246: .getSelectedValue());
247: }
248: updateSelectedFont();
249: }
250:
251: public void changedUpdate(DocumentEvent e) {
252: updateLater();
253: }
254:
255: public void insertUpdate(DocumentEvent e) {
256: updateLater();
257: }
258:
259: public void removeUpdate(DocumentEvent e) {
260: updateLater();
261: }
262:
263: public void actionPerformed(ActionEvent e) {
264: updateLater();
265: }
266:
267: void updateLater() {
268: SwingUtilities.invokeLater(new Runnable() {
269: public void run() {
270: updateSelectedFont();
271: }
272: });
273: }
274: }
275:
276: }
|