]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/ListBoxNode.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scenegraph.swing / src / org / simantics / scenegraph / swing / ListBoxNode.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.DefaultListModel;
27 import javax.swing.JList;
28 import javax.swing.JScrollPane;
29
30 import org.simantics.scenegraph.utils.DummyComponent;
31
32 public class ListBoxNode extends ComponentNode<JScrollPane> implements FocusListener, PropertyChangeListener, KeyListener {
33     /**
34      * 
35      */
36     private static final long serialVersionUID = 7073028693751719102L;
37
38     protected boolean editable = true;
39     protected Object value = "";
40     protected String tooltip = "";
41     protected double borderWidth = 0;
42
43     protected transient ActionListener actionListener = null;
44
45     protected Font font = null;
46     protected Color color = null;
47     protected List<String> items = null;
48     
49     protected transient JList list = null;
50     
51     @Override
52     public String toString() {
53         return super.toString() + "[editable=" + editable + ", value=" + value + "]";
54     }
55     
56     @Override
57     public void init() {
58         list = new JList(new DefaultListModel());
59         component = new JScrollPane(list);
60         list.setEnabled(editable);
61         component.setEnabled(editable);
62 //        component.addChangeListener(this);
63         list.addFocusListener(this);
64         list.addKeyListener(this);
65         list.setLocation(0, 0);
66         
67         super.init();
68     }
69
70     @SyncField("items")
71     public void setItems(List<String> items) {
72         this.items = items;
73         DefaultListModel model = (DefaultListModel)list.getModel();
74         model.clear();
75         for(String item : items) {
76                 model.addElement(item);
77         }
78         if(items.size() > 0)
79                 list.setSelectedValue(items.get(0), true);
80     }
81
82     @SyncField("editable")
83     public void setEditable(boolean value) {
84         this.editable = value;
85
86         if(component != null) {
87             component.setEnabled(value);
88         }
89     }
90
91     @PropertySetter("Stroke Width")
92     @SyncField("borderWidth")
93     public void setBorderWidth(Float borderWidth) {
94         this.borderWidth = borderWidth;
95 //        if(component != null) {
96 //            ((TextField)component).setBorder(borderWidth);
97 //        }
98     }
99
100     @SyncField("value")
101     public void setValue(Object value) {
102         this.value = value;
103         // RemoteViewer does not have component initialized
104         if (component != null && value != null) {
105             //System.out.println("MonitorNode.setText(" + value + ")");
106                 list.setSelectedValue(value, true);
107             component.repaint();
108         }
109     }
110
111     @SyncField("tooltip")
112     public void setToolTipText(String tooltip) {
113         this.tooltip = tooltip;
114         if (component != null) {
115             component.setToolTipText(tooltip);
116         }
117     }
118
119     @PropertySetter("Font")
120     @SyncField("font")
121     public void setFont(Font font) {
122         this.font = font;
123         if (component != null) {
124             setComponentFont(font);
125         }
126     }
127
128     @PropertySetter("Color")
129     @SyncField("color")
130     public void setColor(Color color) {
131         this.color = color;
132         if (component != null) {
133             component.setForeground(color);
134         }
135     }
136     
137     public Object getValue() {
138         return value;
139     }
140
141     public Font getFont() {
142         return font;
143     }
144
145     @Override
146     public void propertyChange(PropertyChangeEvent evt) {
147         if("value".equals(evt.getPropertyName()) && component != null) {
148                 list.setSelectedValue(evt.getNewValue(), true);
149             component.repaint();
150         } else if("editable".equals(evt.getPropertyName()) && component != null) {
151             component.setEnabled((Boolean)evt.getNewValue());
152         }
153     }
154
155
156     public void setActionListener(ActionListener actionListener) {
157         this.actionListener = actionListener;
158     }
159
160     void loseFocus() {
161         if (component != null)
162             if (component.isFocusOwner())
163                 if (container.getParent() != null)
164                     container.getParent().requestFocusInWindow(); // Lose focus
165     }
166
167     @Override
168     public void focusGained(FocusEvent arg0) {
169     }
170
171     @Override
172     public void focusLost(FocusEvent arg0) {
173         if (component != null) {
174             ActionEvent e = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, (String) list.getSelectedValue());
175             performAction(e);
176         }
177     }
178
179     /**
180      * Wrapper method to send event to serverside
181      * 
182      * @param e
183      */
184     @ServerSide
185     public void performAction(ActionEvent e) {
186         if (actionListener != null) {
187             //System.out.println("MonitorNode.performAction(" + e + ")");
188             actionListener.actionPerformed(e);
189         }
190     }
191
192     @Override
193     public void keyPressed(KeyEvent e) {
194     }
195
196     @Override
197     public void keyTyped(KeyEvent e) {
198     }
199
200     @Override
201     public void keyReleased(KeyEvent e) {
202         if (e.getModifiers() == 0 && e.getKeyCode() == KeyEvent.VK_ESCAPE) {
203             // ESC without modifiers == CANCEL edit
204             // TODO: signal about cancellation
205             loseFocus();
206         }
207     }
208
209     /**
210      * Quick hack to test widget feature..
211      * FIXME: Use existing methods or variables..
212      * 
213      * @param input
214      */
215     public void input(String input) {
216         ActionEvent e = new ActionEvent(new DummyComponent(), ActionEvent.ACTION_PERFORMED, input);
217         performAction(e);
218     }
219 }