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.DefaultListModel;
27 import javax.swing.JList;
28 import javax.swing.JScrollPane;
30 import org.simantics.scenegraph.utils.DummyComponent;
32 public class ListBoxNode extends ComponentNode<JScrollPane> implements FocusListener, PropertyChangeListener, KeyListener {
36 private static final long serialVersionUID = 7073028693751719102L;
38 protected boolean editable = true;
39 protected Object value = "";
40 protected String tooltip = "";
41 protected double borderWidth = 0;
43 protected transient ActionListener actionListener = null;
45 protected Font font = null;
46 protected Color color = null;
47 protected List<String> items = null;
49 protected transient JList list = null;
52 public String toString() {
53 return super.toString() + "[editable=" + editable + ", value=" + value + "]";
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);
71 public void setItems(List<String> items) {
73 DefaultListModel model = (DefaultListModel)list.getModel();
75 for(String item : items) {
76 model.addElement(item);
79 list.setSelectedValue(items.get(0), true);
82 @SyncField("editable")
83 public void setEditable(boolean value) {
84 this.editable = value;
86 if(component != null) {
87 component.setEnabled(value);
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);
101 public void setValue(Object 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);
111 @SyncField("tooltip")
112 public void setToolTipText(String tooltip) {
113 this.tooltip = tooltip;
114 if (component != null) {
115 component.setToolTipText(tooltip);
119 @PropertySetter("Font")
121 public void setFont(Font font) {
123 if (component != null) {
124 setComponentFont(font);
128 @PropertySetter("Color")
130 public void setColor(Color color) {
132 if (component != null) {
133 component.setForeground(color);
137 public Object getValue() {
141 public Font getFont() {
146 public void propertyChange(PropertyChangeEvent evt) {
147 if("value".equals(evt.getPropertyName()) && component != null) {
148 list.setSelectedValue(evt.getNewValue(), true);
150 } else if("editable".equals(evt.getPropertyName()) && component != null) {
151 component.setEnabled((Boolean)evt.getNewValue());
156 public void setActionListener(ActionListener actionListener) {
157 this.actionListener = actionListener;
161 if (component != null)
162 if (component.isFocusOwner())
163 if (container.getParent() != null)
164 container.getParent().requestFocusInWindow(); // Lose focus
168 public void focusGained(FocusEvent arg0) {
172 public void focusLost(FocusEvent arg0) {
173 if (component != null) {
174 ActionEvent e = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, (String) list.getSelectedValue());
180 * Wrapper method to send event to serverside
185 public void performAction(ActionEvent e) {
186 if (actionListener != null) {
187 //System.out.println("MonitorNode.performAction(" + e + ")");
188 actionListener.actionPerformed(e);
193 public void keyPressed(KeyEvent e) {
197 public void keyTyped(KeyEvent e) {
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
210 * Quick hack to test widget feature..
211 * FIXME: Use existing methods or variables..
215 public void input(String input) {
216 ActionEvent e = new ActionEvent(new DummyComponent(), ActionEvent.ACTION_PERFORMED, input);