1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.g2d.nodes.provisional;
14 import java.awt.geom.Rectangle2D;
15 import java.beans.PropertyChangeEvent;
16 import java.beans.PropertyChangeListener;
17 import java.lang.reflect.Field;
18 import java.lang.reflect.Modifier;
19 import java.util.HashMap;
21 import java.util.concurrent.ConcurrentHashMap;
23 import org.simantics.scenegraph.g2d.G2DNode;
26 * @author Antti Villberg
28 public abstract class GraphPropertyNode extends G2DNode {
30 private static final long serialVersionUID = 245761992671850588L;
32 private static Map<Class<? extends GraphPropertyNode>, Map<String, Field>> fieldCache = new ConcurrentHashMap<Class<? extends GraphPropertyNode>, Map<String, Field>>();
34 protected transient final Map<String, Field> fields;
36 private PropertyChangeListener fieldListener = null;
38 protected GraphPropertyNode() {
39 this.fields = getFields(getClass());
45 addEventHandler(this);
49 public void cleanup() {
50 removeEventHandler(this);
54 public void setFieldListener(PropertyChangeListener listener) {
55 this.fieldListener = listener;
58 public void propertyChange(String field, Object value) {
61 public void setProperty(String field, Object value) {
62 Field f = fields.get(field);
64 System.err.println("GraphPropertyNode tried to set undefined property '" + field + "'");
68 //System.out.println("setting field '" + field + "'");
70 propertyChange(field, value);
71 } catch (IllegalArgumentException e) {
73 } catch (IllegalAccessException e) {
78 public void commitProperty(String field, Object value) {
79 if (fieldListener != null) {
80 fieldListener.propertyChange(new PropertyChangeEvent(this, field, null, value));
84 public boolean hitTest(double x, double y, double tolerance) {
89 public Rectangle2D getBoundsInLocal() {
93 private static Map<String, Field> getFields(Class<? extends GraphPropertyNode> clazz) {
94 Map<String, Field> fields = fieldCache.get(clazz);
97 fields = calculateFields(clazz);
98 fieldCache.put(clazz, fields);
102 private static Map<String, Field> calculateFields(Class<? extends GraphPropertyNode> clazz) {
103 Map<String, Field> result = new HashMap<String, Field>();
104 for (Class<?> cls = clazz; cls != null && !GraphPropertyNode.class.equals(cls); cls = clazz.getSuperclass()) {
105 for (Field f : cls.getFields()) {
106 int mod = f.getModifiers();
107 if (Modifier.isStatic(mod))
109 if (Modifier.isTransient(mod))
111 if (Modifier.isFinal(mod))
113 if (!result.containsKey(f.getName()))
114 result.put(f.getName(), f);