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.Point;
17 import java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;
19 import java.awt.event.FocusEvent;
20 import java.awt.event.FocusListener;
21 import java.awt.event.KeyEvent;
22 import java.awt.event.KeyListener;
23 import java.awt.geom.Point2D;
24 import java.beans.PropertyChangeEvent;
25 import java.beans.PropertyChangeListener;
26 import java.util.List;
28 import javax.swing.JComboBox;
29 import javax.swing.JPopupMenu;
30 import javax.swing.SwingUtilities;
31 import javax.swing.event.PopupMenuEvent;
32 import javax.swing.event.PopupMenuListener;
34 import org.simantics.scenegraph.utils.DummyComponent;
36 public class ComboBoxNode extends ComponentNode<JComboBox> implements ActionListener, FocusListener, PropertyChangeListener, KeyListener {
40 private static final long serialVersionUID = 7073028693751719102L;
42 protected boolean editable = true;
43 protected String 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 JComboBox();
61 component.setLightWeightPopupEnabled(true);
62 component.setEditable(editable);
63 component.setEnabled(editable);
64 component.addActionListener(this);
65 component.addFocusListener(this);
66 component.addKeyListener(this);
67 component.addPopupMenuListener(new PopupMenuListener() {
69 public void popupMenuCanceled(PopupMenuEvent e) {
70 // TODO Auto-generated method stub
75 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
76 // TODO Auto-generated method stub
81 public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
82 JComboBox box = (JComboBox) e.getSource();
83 Object comp = box.getUI().getAccessibleChild(box, 0);
84 if (!(comp instanceof JPopupMenu)) {
88 * Relocate the popup under the combobox. This is necessary because scenegraph handles combobox rendering,
89 * thus the real location of the combobox is invalid.
91 JPopupMenu popup = (JPopupMenu) comp;
92 Point2D p = ComboBoxNode.this.localToControl(new Point2D.Double());
93 Point loc = new Point((int)p.getX(), (int)p.getY()+component.getHeight());
94 SwingUtilities.convertPointToScreen(loc, box.getParent());
95 popup.setLocation((int)loc.getX(), (int)loc.getY());
102 public void setItems(List<String> items) {
104 component.removeAllItems();
105 for(String str : items) {
106 component.addItem(str);
110 @SyncField("editable")
111 public void setEditable(boolean value) {
112 this.editable = value;
114 if(component != null) {
115 component.setEditable(value);
116 component.setEnabled(value);
120 @PropertySetter("Stroke Width")
121 @SyncField("borderWidth")
122 public void setBorderWidth(Float borderWidth) {
123 this.borderWidth = borderWidth;
124 // if(component != null) {
125 // ((TextField)component).setBorder(borderWidth);
130 public void setText(String value) {
132 // RemoteViewer does not have component initialized
133 if (component != null) {
134 //System.out.println("MonitorNode.setText(" + value + ")");
135 component.setSelectedItem(value);
140 @SyncField("tooltip")
141 public void setToolTipText(String tooltip) {
142 this.tooltip = tooltip;
143 if (component != null) {
144 component.setToolTipText(tooltip);
148 @PropertySetter("Font")
150 public void setFont(Font font) {
152 if (component != null) {
153 setComponentFont(font);
157 @PropertySetter("Color")
159 public void setColor(Color color) {
161 if (component != null) {
162 component.setForeground(color);
166 public String getText() {
170 public Font getFont() {
175 public void propertyChange(PropertyChangeEvent evt) {
176 if("value".equals(evt.getPropertyName()) && component != null) {
177 component.setSelectedItem(evt.getNewValue());
179 } else if("editable".equals(evt.getPropertyName()) && component != null) {
180 component.setEditable((Boolean)evt.getNewValue());
181 component.setEnabled((Boolean)evt.getNewValue());
186 public void setActionListener(ActionListener actionListener) {
187 this.actionListener = actionListener;
191 public void actionPerformed(ActionEvent e) {
196 if (component != null)
197 if (component.isFocusOwner())
198 if (container.getParent() != null)
199 container.getParent().requestFocusInWindow(); // Lose focus
203 public void focusGained(FocusEvent arg0) {
207 public void focusLost(FocusEvent arg0) {
208 if (component != null) {
209 ActionEvent e = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, (String) component.getSelectedItem());
215 * Wrapper method to send event to serverside
220 public void performAction(ActionEvent e) {
221 if (actionListener != null) {
222 //System.out.println("MonitorNode.performAction(" + e + ")");
223 actionListener.actionPerformed(e);
228 public void keyPressed(KeyEvent e) {
232 public void keyTyped(KeyEvent e) {
236 public void keyReleased(KeyEvent e) {
237 if (e.getModifiers() == 0 && e.getKeyCode() == KeyEvent.VK_ESCAPE) {
238 // ESC without modifiers == CANCEL edit
239 // TODO: signal about cancellation
245 * Quick hack to test widget feature..
246 * FIXME: Use existing methods or variables..
250 public void input(String input) {
251 ActionEvent e = new ActionEvent(new DummyComponent(), ActionEvent.ACTION_PERFORMED, input);