001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.properties.editors;
015:
016: import com.jgoodies.forms.layout.CellConstraints;
017: import com.jgoodies.forms.layout.FormLayout;
018: import org.jetbrains.annotations.NonNls;
019: import org.jetbrains.annotations.NotNull;
020: import org.jetbrains.annotations.Nullable;
021: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
022: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
023: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
024:
025: import javax.swing.*;
026: import javax.swing.event.ListSelectionEvent;
027: import javax.swing.event.ListSelectionListener;
028: import java.awt.*;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.util.HashMap;
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034:
035: /**
036: * User: Martin
037: * Date: 01.11.2005
038: * Time: 10:23:10
039: */
040: public class FontChooser {
041: @NonNls
042: @NotNull
043: private static final Logger LOG = Logger
044: .getLogger(FontChooser.class.getName());
045:
046: private FontChooser() {
047: }
048:
049: @Nullable
050: public static Font showFontChooser(@NotNull
051: JComponent parent, @NotNull
052: String title, @Nullable
053: Font font) {
054: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
055: .createDialog(parent, title,
056: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
057:
058: @NonNls
059: FormLayout formLayout = new FormLayout(
060: "0dlu, fill:default:grow, 4dlu, fill:default:grow, 4dlu, fill:default:grow, 0dlu",
061: "0dlu, " + "pref, " + "4dlu, " + "fill:default:grow, "
062: + "4dlu, " + "fill:40dlu, " + "0dlu");
063:
064: JPanel centerPanel = new JPanel(formLayout);
065: @NonNls
066: CellConstraints cc = new CellConstraints();
067:
068: DefaultListModel nameListModel = new DefaultListModel();
069: String[] availableFontFamilyNames = GraphicsEnvironment
070: .getLocalGraphicsEnvironment()
071: .getAvailableFontFamilyNames();
072: for (String famName : availableFontFamilyNames) {
073: nameListModel.addElement(famName);
074: }
075: final JList nameList = new JList(nameListModel);
076: nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
077:
078: centerPanel.add(new JLabel(TranslationManager.getInstance()
079: .getTranslation("R", "FontChooser.Name")), cc.xy(2, 2));
080: centerPanel.add(new JScrollPane(nameList), cc.xy(2, 4));
081:
082: //noinspection AutoBoxing
083: final DefaultComboBoxModel sizeComboBoxModel = new DefaultComboBoxModel(
084: new Object[] { 6, 8, 10, 12, 14, 16, 18, 20, 24, 28, 36 });
085: final JComboBox comboBox = new JComboBox(sizeComboBoxModel);
086: comboBox.setEditable(true);
087:
088: centerPanel.add(new JLabel(TranslationManager.getInstance()
089: .getTranslation("R", "FontChooser.Size")), cc.xy(4, 2));
090: centerPanel.add(comboBox, cc.xy(4, 4, "fill, top"));
091:
092: DefaultListModel styleListModel = new DefaultListModel();
093: styleListModel.addElement(Integer.valueOf(Font.PLAIN));
094: styleListModel.addElement(Integer.valueOf(Font.BOLD));
095: styleListModel.addElement(Integer.valueOf(Font.ITALIC));
096: styleListModel.addElement(Integer.valueOf(Font.ITALIC
097: | Font.BOLD));
098: final JList styleList = new JList(styleListModel);
099: styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
100:
101: centerPanel
102: .add(new JLabel(TranslationManager.getInstance()
103: .getTranslation("R", "FontChooser.Style")), cc
104: .xy(6, 2));
105: centerPanel.add(new JScrollPane(styleList), cc.xy(6, 4));
106:
107: final JLabel sampleLabel = new JLabel(TranslationManager
108: .getInstance().getTranslation("R",
109: "FontChooser.SampleText"));
110: sampleLabel.setBorder(BorderFactory.createLoweredBevelBorder());
111: centerPanel.add(sampleLabel, cc.xyw(2, 6, 5));
112:
113: styleList
114: .setCellRenderer(new FontChooser.StyleListCellRenderer());
115:
116: if (font != null) {
117: sampleLabel.setFont(font);
118: nameList.setSelectedValue(font.getFamily(), true);
119: comboBox.setSelectedItem(Integer.valueOf(font.getSize()));
120:
121: styleList.setSelectedValue(
122: Integer.valueOf(font.getStyle()), true);
123: }
124:
125: nameList.addListSelectionListener(new ListSelectionListener() {
126: public void valueChanged(@NotNull
127: ListSelectionEvent e) {
128: Font f = new Font((String) nameList.getSelectedValue(),
129: ((Integer) styleList.getSelectedValue())
130: .intValue(), ((Integer) comboBox
131: .getSelectedItem()).intValue());
132: sampleLabel.setFont(f);
133: }
134: });
135:
136: styleList.addListSelectionListener(new ListSelectionListener() {
137: public void valueChanged(@NotNull
138: ListSelectionEvent e) {
139: Font f = new Font((String) nameList.getSelectedValue(),
140: ((Integer) styleList.getSelectedValue())
141: .intValue(), ((Integer) comboBox
142: .getSelectedItem()).intValue());
143: sampleLabel.setFont(f);
144: }
145: });
146:
147: comboBox.addActionListener(new ActionListener() {
148: public void actionPerformed(@NotNull
149: ActionEvent e) {
150: int size = 0;
151: try {
152: size = ((Integer) comboBox.getSelectedItem())
153: .intValue();
154: } catch (Exception e1) {
155: if (LOG.isLoggable(Level.FINE))
156: LOG.log(Level.FINE,
157: "FontChooser.actionPerformed ", e1);
158: comboBox.setSelectedItem(Integer
159: .valueOf(sampleLabel.getFont().getSize()));
160: }
161:
162: Font f = new Font((String) nameList.getSelectedValue(),
163: ((Integer) styleList.getSelectedValue())
164: .intValue(), size);
165: sampleLabel.setFont(f);
166: }
167: });
168:
169: final boolean[] action = new boolean[] { false };
170:
171: JButton okButton = new JButton(TranslationManager.getInstance()
172: .getTranslation("R", "Button.ok"));
173: okButton.addActionListener(new ActionListener() {
174: public void actionPerformed(@NotNull
175: ActionEvent e) {
176: action[0] = true;
177: centerPanelDialog.dispose();
178: }
179: });
180:
181: JButton cancelButton = new JButton(TranslationManager
182: .getInstance().getTranslation("R", "Button.cancel"));
183: cancelButton.addActionListener(new ActionListener() {
184: public void actionPerformed(@NotNull
185: ActionEvent e) {
186: action[0] = false;
187: centerPanelDialog.dispose();
188: }
189: });
190:
191: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
192: cancelButton);
193:
194: centerPanelDialog.setCenterPanel(centerPanel);
195: centerPanelDialog.setSize(400, 400);
196: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
197: centerPanelDialog.setVisible(true);
198:
199: if (action[0]) {
200: return sampleLabel.getFont();
201: }
202: return font;
203: }
204:
205: private static class StyleListCellRenderer extends
206: DefaultListCellRenderer {
207: @NotNull
208: private HashMap<Integer, String> values;
209:
210: private StyleListCellRenderer() {
211: values = new HashMap<Integer, String>();
212: values.put(Integer.valueOf(Font.PLAIN), TranslationManager
213: .getInstance().getTranslation("R",
214: "FontChooser.Style.Plain"));
215: values.put(Integer.valueOf(Font.BOLD), TranslationManager
216: .getInstance().getTranslation("R",
217: "FontChooser.Style.Bold"));
218: values.put(Integer.valueOf(Font.ITALIC), TranslationManager
219: .getInstance().getTranslation("R",
220: "FontChooser.Style.Italic"));
221: values.put(Integer.valueOf(Font.BOLD | Font.ITALIC),
222: TranslationManager.getInstance().getTranslation(
223: "R", "FontChooser.Style.BoldItalic"));
224: }
225:
226: @NotNull
227: public Component getListCellRendererComponent(@NotNull
228: JList list, @Nullable
229: Object value, int index, boolean isSelected,
230: boolean cellHasFocus) {
231: //noinspection SuspiciousMethodCalls
232: return super.getListCellRendererComponent(list, values
233: .get(value), index, isSelected, cellHasFocus);
234: }
235: }
236:
237: }
|