]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/SpinnerNode.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scenegraph.swing / src / org / simantics / scenegraph / swing / SpinnerNode.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scenegraph.swing;
13
14 import java.awt.Color;
15 import java.awt.Font;
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;
25
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;
33
34 import org.simantics.scenegraph.utils.DummyComponent;
35
36 public class SpinnerNode extends ComponentNode<JSpinner> implements ChangeListener, FocusListener, PropertyChangeListener, KeyListener {
37     /**
38      * 
39      */
40     private static final long serialVersionUID = 7073028693751719102L;
41
42     protected boolean editable = true;
43     protected Object value = "";
44     protected String tooltip = "";
45     protected double borderWidth = 0;
46
47     protected transient ActionListener actionListener = null;
48
49     protected Font font = null;
50     protected Color color = null;
51     protected List<String> items = null;
52     
53     @Override
54     public String toString() {
55         return super.toString() + "[editable=" + editable + ", value=" + value + "]";
56     }
57     
58     @Override
59     public void init() {
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);
67         
68         JComponent editor = component.getEditor();
69         if (editor instanceof JSpinner.DefaultEditor) {
70                 JFormattedTextField ftf = ((JSpinner.DefaultEditor)editor).getTextField();
71                 if (ftf != null ) {
72                     ftf.setColumns(8); //specify more width than we need
73                     ftf.setHorizontalAlignment(JTextField.LEFT);
74                 }
75         }
76
77         super.init();
78     }
79
80     @SyncField("items")
81     public void setItems(List<String> items) {
82         this.items = items;
83         SpinnerListModel model = (SpinnerListModel)component.getModel();
84         model.setList(items);
85     }
86     
87     @SyncField("editable")
88     public void setEditable(boolean value) {
89         this.editable = value;
90
91         if(component != null) {
92             component.setEnabled(value);
93         }
94     }
95
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);
102 //        }
103     }
104
105     @SyncField("value")
106     public void setValue(Object value) {
107         this.value = value;
108         // RemoteViewer does not have component initialized
109         if (component != null && value != null) {
110             System.out.println("SpinnerNode.setValue(" + value + ")");
111             try {
112                 component.setValue(value);
113                 component.repaint();
114             } catch(IllegalArgumentException e) {
115                 // FIXME..
116             }
117         }
118     }
119
120     @SyncField("tooltip")
121     public void setToolTipText(String tooltip) {
122         this.tooltip = tooltip;
123         if (component != null) {
124             component.setToolTipText(tooltip);
125         }
126     }
127
128     @PropertySetter("Font")
129     @SyncField("font")
130     public void setFont(Font font) {
131         this.font = font;
132         if (component != null) {
133             setComponentFont(font);
134         }
135     }
136
137     @PropertySetter("Color")
138     @SyncField("color")
139     public void setColor(Color color) {
140         this.color = color;
141         if (component != null) {
142             component.setForeground(color);
143         }
144     }
145
146     public Object getValue() {
147         return value;
148     }
149
150     public Font getFont() {
151         return font;
152     }
153
154     @Override
155     public void propertyChange(PropertyChangeEvent evt) {
156         if("value".equals(evt.getPropertyName()) && component != null) {
157                 component.setValue(evt.getNewValue());
158             component.repaint();
159         } else if("editable".equals(evt.getPropertyName()) && component != null) {
160             component.setEnabled((Boolean)evt.getNewValue());
161         }
162     }
163
164     public void setActionListener(ActionListener actionListener) {
165         this.actionListener = actionListener;
166     }
167
168     void loseFocus() {
169         if (component != null)
170             if (component.isFocusOwner())
171                 if (container.getParent() != null)
172                     container.getParent().requestFocusInWindow(); // Lose focus
173     }
174
175     @Override
176     public void focusGained(FocusEvent arg0) {
177     }
178
179     @Override
180     public void focusLost(FocusEvent arg0) {
181         if (component != null) {
182             ActionEvent e = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, (String) component.getValue());
183             performAction(e);
184         }
185     }
186
187     /**
188      * Wrapper method to send event to serverside
189      * 
190      * @param e
191      */
192     @ServerSide
193     public void performAction(ActionEvent e) {
194         if (actionListener != null) {
195             //System.out.println("MonitorNode.performAction(" + e + ")");
196             actionListener.actionPerformed(e);
197         }
198     }
199
200     @Override
201     public void keyPressed(KeyEvent e) {
202     }
203
204     @Override
205     public void keyTyped(KeyEvent e) {
206     }
207
208     @Override
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
213             loseFocus();
214         }
215     }
216
217     /**
218      * Quick hack to test widget feature..
219      * FIXME: Use existing methods or variables..
220      * 
221      * @param input
222      */
223     public void input(String input) {
224         ActionEvent e = new ActionEvent(new DummyComponent(), ActionEvent.ACTION_PERFORMED, input);
225         performAction(e);
226     }
227
228         @Override
229         public void stateChanged(ChangeEvent e) {
230         JSpinner spi = (JSpinner)e.getSource();
231         try{
232           spi.commitEdit();
233         }
234         catch (Exception ex){
235           ex.printStackTrace();
236         }
237 //              loseFocus();
238         }
239 }