]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
index 80be13f91739a17af628f895905002d97ad64f9b..83de471a016ec66d6c5b372aaaf203913274398d 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.scenegraph.swing;\r
-\r
-import java.awt.Color;\r
-import java.awt.Font;\r
-import java.awt.Point;\r
-import java.awt.event.ActionEvent;\r
-import java.awt.event.ActionListener;\r
-import java.awt.event.FocusEvent;\r
-import java.awt.event.FocusListener;\r
-import java.awt.event.KeyEvent;\r
-import java.awt.event.KeyListener;\r
-import java.awt.geom.Point2D;\r
-import java.beans.PropertyChangeEvent;\r
-import java.beans.PropertyChangeListener;\r
-import java.util.List;\r
-\r
-import javax.swing.JComboBox;\r
-import javax.swing.JPopupMenu;\r
-import javax.swing.SwingUtilities;\r
-import javax.swing.event.PopupMenuEvent;\r
-import javax.swing.event.PopupMenuListener;\r
-\r
-import org.simantics.scenegraph.utils.DummyComponent;\r
-\r
-public class ComboBoxNode extends ComponentNode<JComboBox> implements ActionListener, FocusListener, PropertyChangeListener, KeyListener {\r
-    /**\r
-     * \r
-     */\r
-    private static final long serialVersionUID = 7073028693751719102L;\r
-\r
-    protected boolean editable = true;\r
-    protected String value = "";\r
-    protected String tooltip = "";\r
-    protected double borderWidth = 0;\r
-\r
-    protected transient ActionListener actionListener = null;\r
-\r
-    protected Font font = null;\r
-    protected Color color = null;\r
-    protected List<String> items = null;\r
-    \r
-    @Override\r
-    public String toString() {\r
-        return super.toString() + "[editable=" + editable + ", value=" + value + "]";\r
-    }\r
-    \r
-    @Override\r
-    public void init() {\r
-        component = new JComboBox();\r
-        component.setLightWeightPopupEnabled(true);\r
-        component.setEditable(editable);\r
-        component.setEnabled(editable);\r
-        component.addActionListener(this);\r
-        component.addFocusListener(this);\r
-        component.addKeyListener(this);\r
-        component.addPopupMenuListener(new PopupMenuListener() {\r
-                       @Override\r
-                       public void popupMenuCanceled(PopupMenuEvent e) {\r
-                               // TODO Auto-generated method stub\r
-                               \r
-                       }\r
-\r
-                       @Override\r
-                       public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {\r
-                               // TODO Auto-generated method stub\r
-                               \r
-                       }\r
-\r
-                       @Override\r
-                       public void popupMenuWillBecomeVisible(PopupMenuEvent e) {\r
-                       JComboBox box = (JComboBox) e.getSource();\r
-                       Object comp = box.getUI().getAccessibleChild(box, 0);\r
-                       if (!(comp instanceof JPopupMenu)) {\r
-                         return;\r
-                       }\r
-                       /**\r
-                        * Relocate the popup under the combobox. This is necessary because scenegraph handles combobox rendering, \r
-                        * thus the real location of the combobox is invalid.\r
-                        */\r
-                       JPopupMenu popup = (JPopupMenu) comp;\r
-                       Point2D p = ComboBoxNode.this.localToControl(new Point2D.Double());\r
-                       Point loc = new Point((int)p.getX(), (int)p.getY()+component.getHeight());\r
-                       SwingUtilities.convertPointToScreen(loc, box.getParent());\r
-                       popup.setLocation((int)loc.getX(), (int)loc.getY());\r
-                       }\r
-        });\r
-        super.init();\r
-    }\r
-\r
-    @SyncField("items")\r
-    public void setItems(List<String> items) {\r
-        this.items = items;\r
-        component.removeAllItems();\r
-        for(String str : items) {\r
-               component.addItem(str);\r
-        }\r
-    }\r
-\r
-    @SyncField("editable")\r
-    public void setEditable(boolean value) {\r
-        this.editable = value;\r
-\r
-        if(component != null) {\r
-            component.setEditable(value);\r
-            component.setEnabled(value);\r
-        }\r
-    }\r
-\r
-    @PropertySetter("Stroke Width")\r
-    @SyncField("borderWidth")\r
-    public void setBorderWidth(Float borderWidth) {\r
-        this.borderWidth = borderWidth;\r
-//        if(component != null) {\r
-//            ((TextField)component).setBorder(borderWidth);\r
-//        }\r
-    }\r
-\r
-    @SyncField("value")\r
-    public void setText(String value) {\r
-        this.value = value;\r
-        // RemoteViewer does not have component initialized\r
-        if (component != null) {\r
-            //System.out.println("MonitorNode.setText(" + value + ")");\r
-            component.setSelectedItem(value);\r
-            component.repaint();\r
-        }\r
-    }\r
-\r
-    @SyncField("tooltip")\r
-    public void setToolTipText(String tooltip) {\r
-        this.tooltip = tooltip;\r
-        if (component != null) {\r
-            component.setToolTipText(tooltip);\r
-        }\r
-    }\r
-\r
-    @PropertySetter("Font")\r
-    @SyncField("font")\r
-    public void setFont(Font font) {\r
-        this.font = font;\r
-        if (component != null) {\r
-            setComponentFont(font);\r
-        }\r
-    }\r
-\r
-    @PropertySetter("Color")\r
-    @SyncField("color")\r
-    public void setColor(Color color) {\r
-        this.color = color;\r
-        if (component != null) {\r
-            component.setForeground(color);\r
-        }\r
-    }\r
-\r
-    public String getText() {\r
-        return value;\r
-    }\r
-\r
-    public Font getFont() {\r
-        return font;\r
-    }\r
-\r
-    @Override\r
-    public void propertyChange(PropertyChangeEvent evt) {\r
-        if("value".equals(evt.getPropertyName()) && component != null) {\r
-               component.setSelectedItem(evt.getNewValue());\r
-            component.repaint();\r
-        } else if("editable".equals(evt.getPropertyName()) && component != null) {\r
-            component.setEditable((Boolean)evt.getNewValue());\r
-            component.setEnabled((Boolean)evt.getNewValue());\r
-        }\r
-    }\r
-\r
-\r
-    public void setActionListener(ActionListener actionListener) {\r
-        this.actionListener = actionListener;\r
-    }\r
-\r
-    @Override\r
-    public void actionPerformed(ActionEvent e) {\r
-        loseFocus();\r
-    }\r
-\r
-    void loseFocus() {\r
-        if (component != null)\r
-            if (component.isFocusOwner())\r
-                if (container.getParent() != null)\r
-                    container.getParent().requestFocusInWindow(); // Lose focus\r
-    }\r
-\r
-    @Override\r
-    public void focusGained(FocusEvent arg0) {\r
-    }\r
-\r
-    @Override\r
-    public void focusLost(FocusEvent arg0) {\r
-        if (component != null) {\r
-            ActionEvent e = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, (String) component.getSelectedItem());\r
-            performAction(e);\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Wrapper method to send event to serverside\r
-     * \r
-     * @param e\r
-     */\r
-    @ServerSide\r
-    public void performAction(ActionEvent e) {\r
-        if (actionListener != null) {\r
-            //System.out.println("MonitorNode.performAction(" + e + ")");\r
-            actionListener.actionPerformed(e);\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public void keyPressed(KeyEvent e) {\r
-    }\r
-\r
-    @Override\r
-    public void keyTyped(KeyEvent e) {\r
-    }\r
-\r
-    @Override\r
-    public void keyReleased(KeyEvent e) {\r
-        if (e.getModifiers() == 0 && e.getKeyCode() == KeyEvent.VK_ESCAPE) {\r
-            // ESC without modifiers == CANCEL edit\r
-            // TODO: signal about cancellation\r
-            loseFocus();\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Quick hack to test widget feature..\r
-     * FIXME: Use existing methods or variables..\r
-     * \r
-     * @param input\r
-     */\r
-    public void input(String input) {\r
-        ActionEvent e = new ActionEvent(new DummyComponent(), ActionEvent.ACTION_PERFORMED, input);\r
-        performAction(e);\r
-    }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.scenegraph.swing;
+
+import java.awt.Color;
+import java.awt.Font;
+import java.awt.Point;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.geom.Point2D;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.List;
+
+import javax.swing.JComboBox;
+import javax.swing.JPopupMenu;
+import javax.swing.SwingUtilities;
+import javax.swing.event.PopupMenuEvent;
+import javax.swing.event.PopupMenuListener;
+
+import org.simantics.scenegraph.utils.DummyComponent;
+
+public class ComboBoxNode extends ComponentNode<JComboBox> implements ActionListener, FocusListener, PropertyChangeListener, KeyListener {
+    /**
+     * 
+     */
+    private static final long serialVersionUID = 7073028693751719102L;
+
+    protected boolean editable = true;
+    protected String value = "";
+    protected String tooltip = "";
+    protected double borderWidth = 0;
+
+    protected transient ActionListener actionListener = null;
+
+    protected Font font = null;
+    protected Color color = null;
+    protected List<String> items = null;
+    
+    @Override
+    public String toString() {
+        return super.toString() + "[editable=" + editable + ", value=" + value + "]";
+    }
+    
+    @Override
+    public void init() {
+        component = new JComboBox();
+        component.setLightWeightPopupEnabled(true);
+        component.setEditable(editable);
+        component.setEnabled(editable);
+        component.addActionListener(this);
+        component.addFocusListener(this);
+        component.addKeyListener(this);
+        component.addPopupMenuListener(new PopupMenuListener() {
+                       @Override
+                       public void popupMenuCanceled(PopupMenuEvent e) {
+                               // TODO Auto-generated method stub
+                               
+                       }
+
+                       @Override
+                       public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
+                               // TODO Auto-generated method stub
+                               
+                       }
+
+                       @Override
+                       public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
+                       JComboBox box = (JComboBox) e.getSource();
+                       Object comp = box.getUI().getAccessibleChild(box, 0);
+                       if (!(comp instanceof JPopupMenu)) {
+                         return;
+                       }
+                       /**
+                        * Relocate the popup under the combobox. This is necessary because scenegraph handles combobox rendering, 
+                        * thus the real location of the combobox is invalid.
+                        */
+                       JPopupMenu popup = (JPopupMenu) comp;
+                       Point2D p = ComboBoxNode.this.localToControl(new Point2D.Double());
+                       Point loc = new Point((int)p.getX(), (int)p.getY()+component.getHeight());
+                       SwingUtilities.convertPointToScreen(loc, box.getParent());
+                       popup.setLocation((int)loc.getX(), (int)loc.getY());
+                       }
+        });
+        super.init();
+    }
+
+    @SyncField("items")
+    public void setItems(List<String> items) {
+        this.items = items;
+        component.removeAllItems();
+        for(String str : items) {
+               component.addItem(str);
+        }
+    }
+
+    @SyncField("editable")
+    public void setEditable(boolean value) {
+        this.editable = value;
+
+        if(component != null) {
+            component.setEditable(value);
+            component.setEnabled(value);
+        }
+    }
+
+    @PropertySetter("Stroke Width")
+    @SyncField("borderWidth")
+    public void setBorderWidth(Float borderWidth) {
+        this.borderWidth = borderWidth;
+//        if(component != null) {
+//            ((TextField)component).setBorder(borderWidth);
+//        }
+    }
+
+    @SyncField("value")
+    public void setText(String value) {
+        this.value = value;
+        // RemoteViewer does not have component initialized
+        if (component != null) {
+            //System.out.println("MonitorNode.setText(" + value + ")");
+            component.setSelectedItem(value);
+            component.repaint();
+        }
+    }
+
+    @SyncField("tooltip")
+    public void setToolTipText(String tooltip) {
+        this.tooltip = tooltip;
+        if (component != null) {
+            component.setToolTipText(tooltip);
+        }
+    }
+
+    @PropertySetter("Font")
+    @SyncField("font")
+    public void setFont(Font font) {
+        this.font = font;
+        if (component != null) {
+            setComponentFont(font);
+        }
+    }
+
+    @PropertySetter("Color")
+    @SyncField("color")
+    public void setColor(Color color) {
+        this.color = color;
+        if (component != null) {
+            component.setForeground(color);
+        }
+    }
+
+    public String getText() {
+        return value;
+    }
+
+    public Font getFont() {
+        return font;
+    }
+
+    @Override
+    public void propertyChange(PropertyChangeEvent evt) {
+        if("value".equals(evt.getPropertyName()) && component != null) {
+               component.setSelectedItem(evt.getNewValue());
+            component.repaint();
+        } else if("editable".equals(evt.getPropertyName()) && component != null) {
+            component.setEditable((Boolean)evt.getNewValue());
+            component.setEnabled((Boolean)evt.getNewValue());
+        }
+    }
+
+
+    public void setActionListener(ActionListener actionListener) {
+        this.actionListener = actionListener;
+    }
+
+    @Override
+    public void actionPerformed(ActionEvent e) {
+        loseFocus();
+    }
+
+    void loseFocus() {
+        if (component != null)
+            if (component.isFocusOwner())
+                if (container.getParent() != null)
+                    container.getParent().requestFocusInWindow(); // Lose focus
+    }
+
+    @Override
+    public void focusGained(FocusEvent arg0) {
+    }
+
+    @Override
+    public void focusLost(FocusEvent arg0) {
+        if (component != null) {
+            ActionEvent e = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, (String) component.getSelectedItem());
+            performAction(e);
+        }
+    }
+
+    /**
+     * Wrapper method to send event to serverside
+     * 
+     * @param e
+     */
+    @ServerSide
+    public void performAction(ActionEvent e) {
+        if (actionListener != null) {
+            //System.out.println("MonitorNode.performAction(" + e + ")");
+            actionListener.actionPerformed(e);
+        }
+    }
+
+    @Override
+    public void keyPressed(KeyEvent e) {
+    }
+
+    @Override
+    public void keyTyped(KeyEvent e) {
+    }
+
+    @Override
+    public void keyReleased(KeyEvent e) {
+        if (e.getModifiers() == 0 && e.getKeyCode() == KeyEvent.VK_ESCAPE) {
+            // ESC without modifiers == CANCEL edit
+            // TODO: signal about cancellation
+            loseFocus();
+        }
+    }
+
+    /**
+     * Quick hack to test widget feature..
+     * FIXME: Use existing methods or variables..
+     * 
+     * @param input
+     */
+    public void input(String input) {
+        ActionEvent e = new ActionEvent(new DummyComponent(), ActionEvent.ACTION_PERFORMED, input);
+        performAction(e);
+    }
+}