2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 package org.scilab.modules.preferences.Component;
15 import java.awt.Dimension;
17 import java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;
20 import javax.swing.GroupLayout;
21 import javax.swing.JButton;
22 import javax.swing.JFrame;
23 import javax.swing.JTextField;
24 import javax.swing.JPanel;
25 import javax.swing.SwingConstants;
26 import javax.swing.SwingUtilities;
28 import org.w3c.dom.Node;
30 import org.scilab.modules.gui.bridge.fontchooser.SwingScilabFontChooser;
31 import org.scilab.modules.preferences.XChooser;
32 import org.scilab.modules.preferences.XCommonManager;
33 import org.scilab.modules.preferences.XComponent;
34 import org.scilab.modules.preferences.XConfigManager;
37 * Implementation of Label compliant with extended management.
39 * @author Calixte DENIZET
42 public class FontSelector extends Panel implements XComponent, XChooser {
44 /** Universal identifier for serialization.
47 private static final long serialVersionUID = -4842434795956015959L;
48 private ActionListener actionListener;
49 private JTextField textField;
50 private JButton button;
51 private Font font = new Font("Monospaced", Font.PLAIN, 12);
52 private int defaultSize;
54 /** Define the set of actuators.
56 * @return array of actuator names.
58 public final String [] actuators() {
59 return new String[] {"font-name", "font-face", "font-size", "enable"};
64 * @param peer : associated view DOM node.
66 public FontSelector(final Node peer) {
68 GroupLayout layout = new GroupLayout(this);
71 textField = new JTextField();
72 defaultSize = textField.getFont().getSize();
73 textField.setEditable(false);
75 button = new JButton("...");
76 button.addActionListener(new ActionListener() {
77 public void actionPerformed(ActionEvent e) {
78 JFrame frame = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, FontSelector.this);
79 SwingScilabFontChooser fontChooser = new SwingScilabFontChooser(frame, font, false);
80 fontChooser.setLocationRelativeTo(frame);
81 fontChooser.displayAndWait();
83 Font newFont = fontChooser.getSelectedFont();
84 if (newFont != null && !newFont.equals(font)) {
87 if (actionListener != null) {
88 actionListener.actionPerformed(new ActionEvent(FontSelector.this, 0, "Font Selector Value changed", System.currentTimeMillis(), 0));
94 layout.setHorizontalGroup(layout.createSequentialGroup().addComponent(textField).addComponent(button));
95 layout.setVerticalGroup(layout.createParallelGroup().addComponent(textField).addComponent(button));
97 setRequestFocusEnabled(true);
100 String fontname = XConfigManager.getAttribute(peer , "font-name");
103 String fontface = XConfigManager.getAttribute(peer , "font-face");
106 String fontsize = XConfigManager.getAttribute(peer , "font-size");
110 private void setTextField() {
111 textField.setFont(font.deriveFont((float) defaultSize));
112 textField.setText(fontname() + " " + fontsize() + " " + fontface());
115 /** Refresh the component by the use of actuators.
117 * @param peer the corresponding view DOM node
119 public void refresh(final Node peer) {
120 String fontname = XConfigManager.getAttribute(peer , "font-name");
121 if (!fontname.equals(fontname())) {
125 String fontface = XConfigManager.getAttribute(peer , "font-face");
126 if (!fontface.equals(fontface())) {
130 String fontsize = XConfigManager.getAttribute(peer , "font-size");
131 if (!fontsize.equals(fontsize())) {
135 String enable = XConfigManager.getAttribute(peer , "enable", "true");
136 textField.setEnabled(enable.equals("true"));
137 button.setEnabled(enable.equals("true"));
140 public Dimension getMaximumSize() {
141 return getPreferredSize();
144 /** Sensor for 'font-size' attribute.
146 * @return the attribute value.
148 public final String fontsize() {
149 return Integer.toString(font.getSize());
152 /** Sensor for 'font-face' attribute.
154 * @return the attribute value.
156 public final String fontface() {
157 switch (font.getStyle()) {
164 case Font.BOLD | Font.ITALIC :
165 return "bold italic";
171 /** Sensor for 'font-name' attribute.
173 * @return the attribute value.
175 public final String fontname() {
176 return font.getName();
179 /** Actuator for 'font-size' attribute.
181 * @param text : the attribute value.
183 public final void fontsize(String fontsize) {
184 if (!fontsize.equals(XCommonManager.NAV) && !fontsize.equals("")) {
186 int size = Integer.parseInt(fontsize);
187 font = font.deriveFont((float) size);
189 } catch (NumberFormatException e) { }
193 /** Actuator for 'font-face' attribute.
195 * @param text : the attribute value.
197 public final void fontface(String fontface) {
198 if (!fontface.equals(XCommonManager.NAV) && !fontface.equals("")) {
199 int style = Font.PLAIN;
200 if (fontface.equalsIgnoreCase("bold")) {
202 } else if (fontface.equalsIgnoreCase("italic")) {
204 } else if (fontface.equalsIgnoreCase("bold italic")) {
205 style = Font.BOLD | Font.ITALIC;
207 font = font.deriveFont(style);
212 /** Actuator for 'font-name' attribute.
214 * @param text : the attribute value.
216 public final void fontname(String fontname) {
217 if (!fontname.equals(XCommonManager.NAV) && !fontname.equals("")) {
218 font = new Font(fontname, font.getStyle(), font.getSize());
223 /** Actual response read by the listener.
225 * @return response read by the listener.
227 public final Object choose() {
228 return new String[] {fontname(), fontface(), fontsize()};
231 public void addActionListener(ActionListener actionListener) {
232 this.actionListener = actionListener;
235 /** Developer serialization method.
237 * @return equivalent signature.
239 public final String toString() {
240 StringBuilder signature = new StringBuilder("FontSelector");
242 if (!fontname().equals(XConfigManager.NAV)) {
243 signature.append(" font-name='");
244 signature.append(fontname());
245 signature.append("'");
248 if (!fontface().equals(XConfigManager.NAV)) {
249 signature.append(" font-face='");
250 signature.append(fontface());
251 signature.append("'");
254 if (!fontsize().equals(XConfigManager.NAV)) {
255 signature.append(" font-size='");
256 signature.append(fontsize());
257 signature.append("'");
260 return signature.toString();