From 0020bb22355532182b5ebd48ae6d583c8ecb238a Mon Sep 17 00:00:00 2001 From: lehtonen Date: Thu, 6 Oct 2011 00:24:41 +0000 Subject: [PATCH] Switching EventDelegator to NodeEventHandler. TextNode is still todo. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@22545 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/elements2/GraphPropertyNode.java | 135 +++++++++++------- 1 file changed, 80 insertions(+), 55 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/GraphPropertyNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/GraphPropertyNode.java index 62ace38c..a9802673 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/GraphPropertyNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/GraphPropertyNode.java @@ -11,85 +11,110 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.elements2; -import java.awt.event.MouseListener; -import java.awt.event.MouseMotionListener; import java.awt.geom.Rectangle2D; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.simantics.scenegraph.g2d.G2DNode; -import org.simantics.scenegraph.utils.NodeUtil; -abstract public class GraphPropertyNode extends G2DNode implements MouseListener, MouseMotionListener { +/** + * @author Antti Villberg + */ +public abstract class GraphPropertyNode extends G2DNode { - private static final long serialVersionUID = 245761992671850588L; + private static final long serialVersionUID = 245761992671850588L; - HashMap fields = new HashMap(); + private static Map, Map> fieldCache = new ConcurrentHashMap, Map>(); + + protected transient final Map fields; + + private PropertyChangeListener fieldListener = null; + + protected GraphPropertyNode() { + this.fields = getFields(getClass()); + } - private PropertyChangeListener fieldListener = null; - @Override public void init() { super.init(); - NodeUtil.getEventDelegator(this).addMouseListener(this); - NodeUtil.getEventDelegator(this).addMouseMotionListener(this); + addEventHandler(this); } - + @Override public void cleanup() { - NodeUtil.getEventDelegator(this).removeMouseListener(this); - NodeUtil.getEventDelegator(this).removeMouseMotionListener(this); + removeEventHandler(this); super.cleanup(); } - protected GraphPropertyNode() { - for(Field f : getClass().getFields()) { -// System.out.println("register field '" + f.getName() + "'"); - fields.put(f.getName(), f); - } - } - - public void setFieldListener(PropertyChangeListener listener) { - this.fieldListener = listener; - } - - public void propertyChange(String field, Object value) { - } - - public void setProperty(String field, Object value) { - Field f = fields.get(field); - if(f == null) { - System.err.println("GraphPropertyNode tried to set undefined property '" + field + "'"); - return; - } - try { - //System.out.println("setting field '" + field + "'"); - f.set(this, value); - propertyChange(field, value); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - - public void commitProperty(String field, Object value) { - if(fieldListener != null) { - fieldListener.propertyChange(new PropertyChangeEvent(this, field, null, value)); + public void setFieldListener(PropertyChangeListener listener) { + this.fieldListener = listener; + } + + public void propertyChange(String field, Object value) { + } + + public void setProperty(String field, Object value) { + Field f = fields.get(field); + if (f == null) { + System.err.println("GraphPropertyNode tried to set undefined property '" + field + "'"); + return; } - } + try { + //System.out.println("setting field '" + field + "'"); + f.set(this, value); + propertyChange(field, value); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } - boolean pressHit = false; - - public boolean hitTest(double x, double y, double tolerance) { - return false; + public void commitProperty(String field, Object value) { + if (fieldListener != null) { + fieldListener.propertyChange(new PropertyChangeEvent(this, field, null, value)); + } + } + + public boolean hitTest(double x, double y, double tolerance) { + return false; } - @Override + @Override public Rectangle2D getBoundsInLocal() { - return null; + return null; + } + + private static Map getFields(Class clazz) { + Map fields = fieldCache.get(clazz); + if (fields != null) + return fields; + fields = calculateFields(clazz); + fieldCache.put(clazz, fields); + return fields; } - + + private static Map calculateFields(Class clazz) { + Map result = new HashMap(); + for (Class cls = clazz; !GraphPropertyNode.class.equals(cls); cls = clazz.getSuperclass()) { + for (Field f : cls.getFields()) { + int mod = f.getModifiers(); + if (Modifier.isStatic(mod)) + continue; + if (Modifier.isTransient(mod)) + continue; + if (Modifier.isFinal(mod)) + continue; + if (!result.containsKey(f.getName())) + result.put(f.getName(), f); + } + } + return result; + } + } -- 2.47.1