]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/ComboBoxNode.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scenegraph.swing / src / org / simantics / scenegraph / swing / ComboBoxNode.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.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;
27
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;
33
34 import org.simantics.scenegraph.utils.DummyComponent;
35
36 public class ComboBoxNode extends ComponentNode<JComboBox> implements ActionListener, FocusListener, PropertyChangeListener, KeyListener {
37     /**
38      * 
39      */
40     private static final long serialVersionUID = 7073028693751719102L;
41
42     protected boolean editable = true;
43     protected String 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 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() {
68                         @Override
69                         public void popupMenuCanceled(PopupMenuEvent e) {
70                                 // TODO Auto-generated method stub
71                                 
72                         }
73
74                         @Override
75                         public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
76                                 // TODO Auto-generated method stub
77                                 
78                         }
79
80                         @Override
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)) {
85                           return;
86                         }
87                         /**
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.
90                          */
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());
96                         }
97         });
98         super.init();
99     }
100
101     @SyncField("items")
102     public void setItems(List<String> items) {
103         this.items = items;
104         component.removeAllItems();
105         for(String str : items) {
106                 component.addItem(str);
107         }
108     }
109
110     @SyncField("editable")
111     public void setEditable(boolean value) {
112         this.editable = value;
113
114         if(component != null) {
115             component.setEditable(value);
116             component.setEnabled(value);
117         }
118     }
119
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);
126 //        }
127     }
128
129     @SyncField("value")
130     public void setText(String value) {
131         this.value = value;
132         // RemoteViewer does not have component initialized
133         if (component != null) {
134             //System.out.println("MonitorNode.setText(" + value + ")");
135             component.setSelectedItem(value);
136             component.repaint();
137         }
138     }
139
140     @SyncField("tooltip")
141     public void setToolTipText(String tooltip) {
142         this.tooltip = tooltip;
143         if (component != null) {
144             component.setToolTipText(tooltip);
145         }
146     }
147
148     @PropertySetter("Font")
149     @SyncField("font")
150     public void setFont(Font font) {
151         this.font = font;
152         if (component != null) {
153             setComponentFont(font);
154         }
155     }
156
157     @PropertySetter("Color")
158     @SyncField("color")
159     public void setColor(Color color) {
160         this.color = color;
161         if (component != null) {
162             component.setForeground(color);
163         }
164     }
165
166     public String getText() {
167         return value;
168     }
169
170     public Font getFont() {
171         return font;
172     }
173
174     @Override
175     public void propertyChange(PropertyChangeEvent evt) {
176         if("value".equals(evt.getPropertyName()) && component != null) {
177                 component.setSelectedItem(evt.getNewValue());
178             component.repaint();
179         } else if("editable".equals(evt.getPropertyName()) && component != null) {
180             component.setEditable((Boolean)evt.getNewValue());
181             component.setEnabled((Boolean)evt.getNewValue());
182         }
183     }
184
185
186     public void setActionListener(ActionListener actionListener) {
187         this.actionListener = actionListener;
188     }
189
190     @Override
191     public void actionPerformed(ActionEvent e) {
192         loseFocus();
193     }
194
195     void loseFocus() {
196         if (component != null)
197             if (component.isFocusOwner())
198                 if (container.getParent() != null)
199                     container.getParent().requestFocusInWindow(); // Lose focus
200     }
201
202     @Override
203     public void focusGained(FocusEvent arg0) {
204     }
205
206     @Override
207     public void focusLost(FocusEvent arg0) {
208         if (component != null) {
209             ActionEvent e = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, (String) component.getSelectedItem());
210             performAction(e);
211         }
212     }
213
214     /**
215      * Wrapper method to send event to serverside
216      * 
217      * @param e
218      */
219     @ServerSide
220     public void performAction(ActionEvent e) {
221         if (actionListener != null) {
222             //System.out.println("MonitorNode.performAction(" + e + ")");
223             actionListener.actionPerformed(e);
224         }
225     }
226
227     @Override
228     public void keyPressed(KeyEvent e) {
229     }
230
231     @Override
232     public void keyTyped(KeyEvent e) {
233     }
234
235     @Override
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
240             loseFocus();
241         }
242     }
243
244     /**
245      * Quick hack to test widget feature..
246      * FIXME: Use existing methods or variables..
247      * 
248      * @param input
249      */
250     public void input(String input) {
251         ActionEvent e = new ActionEvent(new DummyComponent(), ActionEvent.ACTION_PERFORMED, input);
252         performAction(e);
253     }
254 }