1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.swing;
14 import java.awt.Color;
16 import java.awt.event.ActionEvent;
17 import java.awt.event.ActionListener;
18 import java.awt.event.FocusEvent;
19 import java.awt.event.FocusListener;
20 import java.awt.event.KeyEvent;
21 import java.awt.event.KeyListener;
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.util.List;
26 import javax.swing.JComponent;
27 import javax.swing.JFormattedTextField;
28 import javax.swing.JSpinner;
29 import javax.swing.JTextField;
30 import javax.swing.SpinnerListModel;
31 import javax.swing.event.ChangeEvent;
32 import javax.swing.event.ChangeListener;
34 import org.simantics.scenegraph.utils.DummyComponent;
36 public class SpinnerNode extends ComponentNode<JSpinner> implements ChangeListener, FocusListener, PropertyChangeListener, KeyListener {
40 private static final long serialVersionUID = 7073028693751719102L;
42 protected boolean editable = true;
43 protected Object value = "";
44 protected String tooltip = "";
45 protected double borderWidth = 0;
47 protected transient ActionListener actionListener = null;
49 protected Font font = null;
50 protected Color color = null;
51 protected List<String> items = null;
54 public String toString() {
55 return super.toString() + "[editable=" + editable + ", value=" + value + "]";
60 component = new JSpinner(new SpinnerListModel());
61 component.setEnabled(editable);
62 component.addChangeListener(this);
63 component.setVisible(true);
64 component.addFocusListener(this);
65 component.addKeyListener(this);
66 component.setLocation(0, 0);
68 JComponent editor = component.getEditor();
69 if (editor instanceof JSpinner.DefaultEditor) {
70 JFormattedTextField ftf = ((JSpinner.DefaultEditor)editor).getTextField();
72 ftf.setColumns(8); //specify more width than we need
73 ftf.setHorizontalAlignment(JTextField.LEFT);
81 public void setItems(List<String> items) {
83 SpinnerListModel model = (SpinnerListModel)component.getModel();
87 @SyncField("editable")
88 public void setEditable(boolean value) {
89 this.editable = value;
91 if(component != null) {
92 component.setEnabled(value);
96 @PropertySetter("Stroke Width")
97 @SyncField("borderWidth")
98 public void setBorderWidth(Float borderWidth) {
99 this.borderWidth = borderWidth;
100 // if(component != null) {
101 // ((TextField)component).setBorder(borderWidth);
106 public void setValue(Object value) {
108 // RemoteViewer does not have component initialized
109 if (component != null && value != null) {
110 System.out.println("SpinnerNode.setValue(" + value + ")");
112 component.setValue(value);
114 } catch(IllegalArgumentException e) {
120 @SyncField("tooltip")
121 public void setToolTipText(String tooltip) {
122 this.tooltip = tooltip;
123 if (component != null) {
124 component.setToolTipText(tooltip);
128 @PropertySetter("Font")
130 public void setFont(Font font) {
132 if (component != null) {
133 setComponentFont(font);
137 @PropertySetter("Color")
139 public void setColor(Color color) {
141 if (component != null) {
142 component.setForeground(color);
146 public Object getValue() {
150 public Font getFont() {
155 public void propertyChange(PropertyChangeEvent evt) {
156 if("value".equals(evt.getPropertyName()) && component != null) {
157 component.setValue(evt.getNewValue());
159 } else if("editable".equals(evt.getPropertyName()) && component != null) {
160 component.setEnabled((Boolean)evt.getNewValue());
164 public void setActionListener(ActionListener actionListener) {
165 this.actionListener = actionListener;
169 if (component != null)
170 if (component.isFocusOwner())
171 if (container.getParent() != null)
172 container.getParent().requestFocusInWindow(); // Lose focus
176 public void focusGained(FocusEvent arg0) {
180 public void focusLost(FocusEvent arg0) {
181 if (component != null) {
182 ActionEvent e = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, (String) component.getValue());
188 * Wrapper method to send event to serverside
193 public void performAction(ActionEvent e) {
194 if (actionListener != null) {
195 //System.out.println("MonitorNode.performAction(" + e + ")");
196 actionListener.actionPerformed(e);
201 public void keyPressed(KeyEvent e) {
205 public void keyTyped(KeyEvent e) {
209 public void keyReleased(KeyEvent e) {
210 if (e.getModifiers() == 0 && e.getKeyCode() == KeyEvent.VK_ESCAPE) {
211 // ESC without modifiers == CANCEL edit
212 // TODO: signal about cancellation
218 * Quick hack to test widget feature..
219 * FIXME: Use existing methods or variables..
223 public void input(String input) {
224 ActionEvent e = new ActionEvent(new DummyComponent(), ActionEvent.ACTION_PERFORMED, input);
229 public void stateChanged(ChangeEvent e) {
230 JSpinner spi = (JSpinner)e.getSource();
234 catch (Exception ex){
235 ex.printStackTrace();