X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scenegraph.swing%2Fsrc%2Forg%2Fsimantics%2Fscenegraph%2Fswing%2FListBoxNode.java;h=6bfd7092fffb0569cccf0999e9461d60a077f1d5;hb=b2f1da4c219c646ba3fce0498bf757d64c6dca6d;hp=78767e195630bdadf43cda6bcbdac2e0bc5cff4f;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/ListBoxNode.java b/bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/ListBoxNode.java index 78767e195..6bfd7092f 100644 --- a/bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/ListBoxNode.java +++ b/bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/ListBoxNode.java @@ -1,219 +1,219 @@ -/******************************************************************************* - * 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.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.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; -import java.util.List; - -import javax.swing.DefaultListModel; -import javax.swing.JList; -import javax.swing.JScrollPane; - -import org.simantics.scenegraph.utils.DummyComponent; - -public class ListBoxNode extends ComponentNode implements FocusListener, PropertyChangeListener, KeyListener { - /** - * - */ - private static final long serialVersionUID = 7073028693751719102L; - - protected boolean editable = true; - protected Object value = ""; - protected String tooltip = ""; - protected double borderWidth = 0; - - protected transient ActionListener actionListener = null; - - protected Font font = null; - protected Color color = null; - protected List items = null; - - protected transient JList list = null; - - @Override - public String toString() { - return super.toString() + "[editable=" + editable + ", value=" + value + "]"; - } - - @Override - public void init() { - list = new JList(new DefaultListModel()); - component = new JScrollPane(list); - list.setEnabled(editable); - component.setEnabled(editable); -// component.addChangeListener(this); - list.addFocusListener(this); - list.addKeyListener(this); - list.setLocation(0, 0); - - super.init(); - } - - @SyncField("items") - public void setItems(List items) { - this.items = items; - DefaultListModel model = (DefaultListModel)list.getModel(); - model.clear(); - for(String item : items) { - model.addElement(item); - } - if(items.size() > 0) - list.setSelectedValue(items.get(0), true); - } - - @SyncField("editable") - public void setEditable(boolean value) { - this.editable = value; - - if(component != null) { - 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 setValue(Object value) { - this.value = value; - // RemoteViewer does not have component initialized - if (component != null && value != null) { - //System.out.println("MonitorNode.setText(" + value + ")"); - list.setSelectedValue(value, true); - 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 Object getValue() { - return value; - } - - public Font getFont() { - return font; - } - - @Override - public void propertyChange(PropertyChangeEvent evt) { - if("value".equals(evt.getPropertyName()) && component != null) { - list.setSelectedValue(evt.getNewValue(), true); - component.repaint(); - } else if("editable".equals(evt.getPropertyName()) && component != null) { - component.setEnabled((Boolean)evt.getNewValue()); - } - } - - - public void setActionListener(ActionListener actionListener) { - this.actionListener = actionListener; - } - - 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) list.getSelectedValue()); - 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); - } -} +/******************************************************************************* + * 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.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.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.List; + +import javax.swing.DefaultListModel; +import javax.swing.JList; +import javax.swing.JScrollPane; + +import org.simantics.scenegraph.utils.DummyComponent; + +public class ListBoxNode extends ComponentNode implements FocusListener, PropertyChangeListener, KeyListener { + /** + * + */ + private static final long serialVersionUID = 7073028693751719102L; + + protected boolean editable = true; + protected Object value = ""; + protected String tooltip = ""; + protected double borderWidth = 0; + + protected transient ActionListener actionListener = null; + + protected Font font = null; + protected Color color = null; + protected List items = null; + + protected transient JList list = null; + + @Override + public String toString() { + return super.toString() + "[editable=" + editable + ", value=" + value + "]"; + } + + @Override + public void init() { + list = new JList(new DefaultListModel()); + component = new JScrollPane(list); + list.setEnabled(editable); + component.setEnabled(editable); +// component.addChangeListener(this); + list.addFocusListener(this); + list.addKeyListener(this); + list.setLocation(0, 0); + + super.init(); + } + + @SyncField("items") + public void setItems(List items) { + this.items = items; + DefaultListModel model = (DefaultListModel)list.getModel(); + model.clear(); + for(String item : items) { + model.addElement(item); + } + if(items.size() > 0) + list.setSelectedValue(items.get(0), true); + } + + @SyncField("editable") + public void setEditable(boolean value) { + this.editable = value; + + if(component != null) { + 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 setValue(Object value) { + this.value = value; + // RemoteViewer does not have component initialized + if (component != null && value != null) { + //System.out.println("MonitorNode.setText(" + value + ")"); + list.setSelectedValue(value, true); + 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 Object getValue() { + return value; + } + + public Font getFont() { + return font; + } + + @Override + public void propertyChange(PropertyChangeEvent evt) { + if("value".equals(evt.getPropertyName()) && component != null) { + list.setSelectedValue(evt.getNewValue(), true); + component.repaint(); + } else if("editable".equals(evt.getPropertyName()) && component != null) { + component.setEnabled((Boolean)evt.getNewValue()); + } + } + + + public void setActionListener(ActionListener actionListener) { + this.actionListener = actionListener; + } + + 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) list.getSelectedValue()); + 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); + } +}