*******************************************************************************/\r
package org.simantics.sysdyn.ui.elements2;\r
\r
-import java.awt.event.MouseListener;\r
-import java.awt.event.MouseMotionListener;\r
import java.awt.geom.Rectangle2D;\r
import java.beans.PropertyChangeEvent;\r
import java.beans.PropertyChangeListener;\r
import java.lang.reflect.Field;\r
+import java.lang.reflect.Modifier;\r
import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.concurrent.ConcurrentHashMap;\r
\r
import org.simantics.scenegraph.g2d.G2DNode;\r
-import org.simantics.scenegraph.utils.NodeUtil;\r
\r
-abstract public class GraphPropertyNode extends G2DNode implements MouseListener, MouseMotionListener {\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+public abstract class GraphPropertyNode extends G2DNode {\r
\r
- private static final long serialVersionUID = 245761992671850588L;\r
+ private static final long serialVersionUID = 245761992671850588L;\r
\r
- HashMap<String, Field> fields = new HashMap<String, Field>();\r
+ private static Map<Class<? extends GraphPropertyNode>, Map<String, Field>> fieldCache = new ConcurrentHashMap<Class<? extends GraphPropertyNode>, Map<String, Field>>();\r
+\r
+ protected transient final Map<String, Field> fields;\r
+\r
+ private PropertyChangeListener fieldListener = null;\r
+\r
+ protected GraphPropertyNode() {\r
+ this.fields = getFields(getClass());\r
+ }\r
\r
- private PropertyChangeListener fieldListener = null;\r
- \r
@Override\r
public void init() {\r
super.init();\r
- NodeUtil.getEventDelegator(this).addMouseListener(this);\r
- NodeUtil.getEventDelegator(this).addMouseMotionListener(this);\r
+ addEventHandler(this);\r
}\r
- \r
+\r
@Override\r
public void cleanup() {\r
- NodeUtil.getEventDelegator(this).removeMouseListener(this);\r
- NodeUtil.getEventDelegator(this).removeMouseMotionListener(this);\r
+ removeEventHandler(this);\r
super.cleanup();\r
}\r
\r
- protected GraphPropertyNode() {\r
- for(Field f : getClass().getFields()) {\r
-// System.out.println("register field '" + f.getName() + "'");\r
- fields.put(f.getName(), f);\r
- }\r
- }\r
- \r
- public void setFieldListener(PropertyChangeListener listener) {\r
- this.fieldListener = listener;\r
- }\r
- \r
- public void propertyChange(String field, Object value) {\r
- }\r
- \r
- public void setProperty(String field, Object value) {\r
- Field f = fields.get(field);\r
- if(f == null) {\r
- System.err.println("GraphPropertyNode tried to set undefined property '" + field + "'");\r
- return;\r
- }\r
- try {\r
- //System.out.println("setting field '" + field + "'");\r
- f.set(this, value);\r
- propertyChange(field, value);\r
- } catch (IllegalArgumentException e) {\r
- e.printStackTrace();\r
- } catch (IllegalAccessException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- \r
- public void commitProperty(String field, Object value) {\r
- if(fieldListener != null) {\r
- fieldListener.propertyChange(new PropertyChangeEvent(this, field, null, value));\r
+ public void setFieldListener(PropertyChangeListener listener) {\r
+ this.fieldListener = listener;\r
+ }\r
+\r
+ public void propertyChange(String field, Object value) {\r
+ }\r
+\r
+ public void setProperty(String field, Object value) {\r
+ Field f = fields.get(field);\r
+ if (f == null) {\r
+ System.err.println("GraphPropertyNode tried to set undefined property '" + field + "'");\r
+ return;\r
}\r
- }\r
+ try {\r
+ //System.out.println("setting field '" + field + "'");\r
+ f.set(this, value);\r
+ propertyChange(field, value);\r
+ } catch (IllegalArgumentException e) {\r
+ e.printStackTrace();\r
+ } catch (IllegalAccessException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
\r
- boolean pressHit = false;\r
- \r
- public boolean hitTest(double x, double y, double tolerance) {\r
- return false;\r
+ public void commitProperty(String field, Object value) {\r
+ if (fieldListener != null) {\r
+ fieldListener.propertyChange(new PropertyChangeEvent(this, field, null, value));\r
+ }\r
+ }\r
+\r
+ public boolean hitTest(double x, double y, double tolerance) {\r
+ return false;\r
}\r
\r
- @Override\r
+ @Override\r
public Rectangle2D getBoundsInLocal() {\r
- return null;\r
+ return null;\r
+ }\r
+\r
+ private static Map<String, Field> getFields(Class<? extends GraphPropertyNode> clazz) {\r
+ Map<String, Field> fields = fieldCache.get(clazz);\r
+ if (fields != null)\r
+ return fields;\r
+ fields = calculateFields(clazz);\r
+ fieldCache.put(clazz, fields);\r
+ return fields;\r
}\r
- \r
+\r
+ private static Map<String, Field> calculateFields(Class<? extends GraphPropertyNode> clazz) {\r
+ Map<String, Field> result = new HashMap<String, Field>();\r
+ for (Class<?> cls = clazz; !GraphPropertyNode.class.equals(cls); cls = clazz.getSuperclass()) {\r
+ for (Field f : cls.getFields()) {\r
+ int mod = f.getModifiers();\r
+ if (Modifier.isStatic(mod))\r
+ continue;\r
+ if (Modifier.isTransient(mod))\r
+ continue;\r
+ if (Modifier.isFinal(mod))\r
+ continue;\r
+ if (!result.containsKey(f.getName()))\r
+ result.put(f.getName(), f);\r
+ }\r
+ }\r
+ return result;\r
+ }\r
+\r
}\r