1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.g2d.elementclass;
14 import org.simantics.g2d.element.ElementHints;
15 import org.simantics.g2d.element.ElementHints.Properties;
16 import org.simantics.g2d.element.IElement;
17 import org.simantics.g2d.element.handler.PropertySetter;
18 import org.simantics.scenegraph.INode;
19 import org.simantics.scenegraph.Node;
20 import org.simantics.scenegraph.g2d.G2DParentNode;
21 import org.simantics.scenegraph.g2d.IG2DNode;
22 import org.simantics.scenegraph.g2d.nodes.AnimatedSVGNode;
23 import org.simantics.scenegraph.utils.NodeUtil;
24 import org.simantics.utils.datastructures.hints.IHintContext.Key;
25 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
27 public class PlainElementPropertySetter implements PropertySetter {
29 private static final long serialVersionUID = -3165827599737160621L;
31 public static final PlainElementPropertySetter INSTANCE = new PlainElementPropertySetter(ElementHints.KEY_SG_NODE);
32 public static final Key KEY_PROFILE_PROPERTIES = new KeyOf(Properties.class, "ELEMENT_PROFILE_PROPERTIES");
33 private final Key key;
35 public PlainElementPropertySetter(Key key) {
40 public void overrideProperties(IElement e, Properties properties) {
41 Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);
43 profile = new Properties();
44 e.setHint(KEY_PROFILE_PROPERTIES, profile);
47 profile.putAll(properties);
51 public void overrideProperty(IElement e, String name, Object value) {
52 //System.out.println(name+"="+value);
53 Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);
55 profile = new Properties();
56 e.setHint(KEY_PROFILE_PROPERTIES, profile);
58 profile.put(name, value);
61 private Node findSingle(Node node, Class<?> clazz) {
63 // System.err.println("findSingle " + node);
65 if(clazz.isInstance(node)) return node;
67 if(node instanceof G2DParentNode) {
68 G2DParentNode parent = (G2DParentNode)node;
70 for(IG2DNode child : parent.getNodes()) {
71 if(child instanceof Node) {
72 Node test = findSingle((Node)child, clazz);
74 if(found == null) found = test;
86 private Node findAnimatedNode(Node node) {
87 Node animated = findSingle(node, AnimatedSVGNode.class);
88 if(animated != null) return animated;
93 public void syncPropertiesToNode(IElement e) {
94 Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);
95 Properties element = e.getHint(ElementHints.KEY_ELEMENT_PROPERTIES);
97 Node node = e.getHint(this.key);
98 if(node == null) return; // FIXME
100 node = findAnimatedNode(node);
102 Properties concat = new Properties();
103 if(element != null) {
104 for(String key : element.keySet()) {
105 concat.put(key, element.get(key));
109 if(profile != null) {
110 for(String key : profile.keySet()) {
111 concat.put(key, profile.get(key));
115 for(String property : concat.keySet()) {
116 //System.out.println("sync: "+property+" | "+concat.get(property));
117 // TODO: property names containing dots "." should be splitted and treated as hierarchical properties
118 if(property.contains(".")) {
119 String t[] = property.split("\\.");
120 if(t.length == 2) { // FIXME: add support for deeper hierarchy
121 String child_name = t[0];
122 String property_name = t[1];
123 if(node instanceof G2DParentNode) {
124 INode child = NodeUtil.findChildById((G2DParentNode)node, child_name);
126 NodeUtil.setPropertyIfSupported(property_name, concat.get(property), child);
130 NodeUtil.setPropertyIfSupported(property, concat.get(property), node);