--- /dev/null
+/*******************************************************************************\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.g2d.elementclass;\r
+\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.ElementHints.Properties;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.PropertySetter;\r
+import org.simantics.scenegraph.INode;\r
+import org.simantics.scenegraph.Node;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.IG2DNode;\r
+import org.simantics.scenegraph.g2d.nodes.AnimatedSVGNode;\r
+import org.simantics.scenegraph.utils.NodeUtil;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+\r
+public class PlainElementPropertySetter implements PropertySetter {\r
+\r
+ private static final long serialVersionUID = -3165827599737160621L;\r
+\r
+ public static final PlainElementPropertySetter INSTANCE = new PlainElementPropertySetter(ElementHints.KEY_SG_NODE);\r
+ public static final Key KEY_PROFILE_PROPERTIES = new KeyOf(Properties.class, "ELEMENT_PROFILE_PROPERTIES");\r
+ private final Key key;\r
+\r
+ public PlainElementPropertySetter(Key key) {\r
+ this.key = key;\r
+ }\r
+\r
+ @Override\r
+ public void overrideProperties(IElement e, Properties properties) {\r
+ Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);\r
+ if(profile == null) {\r
+ profile = new Properties();\r
+ e.setHint(KEY_PROFILE_PROPERTIES, profile);\r
+ }\r
+ profile.clear();\r
+ profile.putAll(properties);\r
+ }\r
+\r
+ @Override\r
+ public void overrideProperty(IElement e, String name, Object value) {\r
+ //System.out.println(name+"="+value);\r
+ Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);\r
+ if(profile == null) {\r
+ profile = new Properties();\r
+ e.setHint(KEY_PROFILE_PROPERTIES, profile);\r
+ }\r
+ profile.put(name, value);\r
+ }\r
+\r
+ private Node findSingle(Node node, Class<?> clazz) {\r
+\r
+// System.err.println("findSingle " + node);\r
+\r
+ if(clazz.isInstance(node)) return node;\r
+\r
+ if(node instanceof G2DParentNode) {\r
+ G2DParentNode parent = (G2DParentNode)node;\r
+ Node found = null;\r
+ for(IG2DNode child : parent.getNodes()) {\r
+ if(child instanceof Node) {\r
+ Node test = findSingle((Node)child, clazz);\r
+ if(test != null) {\r
+ if(found == null) found = test;\r
+ else return null;\r
+ }\r
+ }\r
+ }\r
+ return found;\r
+ } else {\r
+ return null;\r
+ }\r
+\r
+ }\r
+\r
+ private Node findAnimatedNode(Node node) {\r
+ Node animated = findSingle(node, AnimatedSVGNode.class);\r
+ if(animated != null) return animated;\r
+ else return node;\r
+ }\r
+\r
+ @Override\r
+ public void syncPropertiesToNode(IElement e) {\r
+ Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);\r
+ Properties element = e.getHint(ElementHints.KEY_ELEMENT_PROPERTIES);\r
+\r
+ Node node = e.getHint(this.key);\r
+ if(node == null) return; // FIXME\r
+\r
+ node = findAnimatedNode(node);\r
+\r
+ Properties concat = new Properties();\r
+ if(element != null) {\r
+ for(String key : element.keySet()) {\r
+ concat.put(key, element.get(key));\r
+ }\r
+ }\r
+\r
+ if(profile != null) {\r
+ for(String key : profile.keySet()) {\r
+ concat.put(key, profile.get(key));\r
+ }\r
+ }\r
+\r
+ for(String property : concat.keySet()) {\r
+ //System.out.println("sync: "+property+" | "+concat.get(property));\r
+ // TODO: property names containing dots "." should be splitted and treated as hierarchical properties \r
+ if(property.contains(".")) {\r
+ String t[] = property.split("\\.");\r
+ if(t.length == 2) { // FIXME: add support for deeper hierarchy\r
+ String child_name = t[0];\r
+ String property_name = t[1];\r
+ if(node instanceof G2DParentNode) {\r
+ INode child = NodeUtil.findChildById((G2DParentNode)node, child_name);\r
+ if(child != null)\r
+ NodeUtil.setPropertyIfSupported(property_name, concat.get(property), child);\r
+ }\r
+ }\r
+ } else {\r
+ NodeUtil.setPropertyIfSupported(property, concat.get(property), node);\r
+ }\r
+ }\r
+ }\r
+}\r