]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/PlainElementPropertySetter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / PlainElementPropertySetter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.elementclass;\r
13 \r
14 import org.simantics.g2d.element.ElementHints;\r
15 import org.simantics.g2d.element.ElementHints.Properties;\r
16 import org.simantics.g2d.element.IElement;\r
17 import org.simantics.g2d.element.handler.PropertySetter;\r
18 import org.simantics.scenegraph.INode;\r
19 import org.simantics.scenegraph.Node;\r
20 import org.simantics.scenegraph.g2d.G2DParentNode;\r
21 import org.simantics.scenegraph.g2d.IG2DNode;\r
22 import org.simantics.scenegraph.g2d.nodes.AnimatedSVGNode;\r
23 import org.simantics.scenegraph.utils.NodeUtil;\r
24 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
25 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
26 \r
27 public class PlainElementPropertySetter implements PropertySetter {\r
28 \r
29     private static final long serialVersionUID = -3165827599737160621L;\r
30 \r
31     public static final PlainElementPropertySetter INSTANCE = new PlainElementPropertySetter(ElementHints.KEY_SG_NODE);\r
32     public static final Key KEY_PROFILE_PROPERTIES = new KeyOf(Properties.class, "ELEMENT_PROFILE_PROPERTIES");\r
33     private final Key key;\r
34 \r
35     public PlainElementPropertySetter(Key key) {\r
36         this.key = key;\r
37     }\r
38 \r
39     @Override\r
40     public void overrideProperties(IElement e, Properties properties) {\r
41         Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);\r
42         if(profile == null) {\r
43             profile = new Properties();\r
44             e.setHint(KEY_PROFILE_PROPERTIES, profile);\r
45         }\r
46         profile.clear();\r
47         profile.putAll(properties);\r
48     }\r
49 \r
50     @Override\r
51     public void overrideProperty(IElement e, String name, Object value) {\r
52         //System.out.println(name+"="+value);\r
53         Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);\r
54         if(profile == null) {\r
55             profile = new Properties();\r
56             e.setHint(KEY_PROFILE_PROPERTIES, profile);\r
57         }\r
58         profile.put(name, value);\r
59     }\r
60 \r
61     private Node findSingle(Node node, Class<?> clazz) {\r
62 \r
63 //              System.err.println("findSingle " + node);\r
64 \r
65         if(clazz.isInstance(node)) return node;\r
66 \r
67         if(node instanceof G2DParentNode) {\r
68             G2DParentNode parent = (G2DParentNode)node;\r
69             Node found = null;\r
70             for(IG2DNode child : parent.getNodes()) {\r
71                 if(child instanceof Node) {\r
72                     Node test = findSingle((Node)child, clazz);\r
73                     if(test != null) {\r
74                         if(found == null) found = test;\r
75                         else return null;\r
76                     }\r
77                 }\r
78             }\r
79             return found;\r
80         } else {\r
81             return null;\r
82         }\r
83 \r
84     }\r
85 \r
86     private Node findAnimatedNode(Node node) {\r
87         Node animated = findSingle(node, AnimatedSVGNode.class);\r
88         if(animated != null) return animated;\r
89         else return node;\r
90     }\r
91 \r
92     @Override\r
93     public void syncPropertiesToNode(IElement e) {\r
94         Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);\r
95         Properties element = e.getHint(ElementHints.KEY_ELEMENT_PROPERTIES);\r
96 \r
97         Node node = e.getHint(this.key);\r
98         if(node == null) return; // FIXME\r
99 \r
100         node = findAnimatedNode(node);\r
101 \r
102         Properties concat = new Properties();\r
103         if(element != null) {\r
104             for(String key : element.keySet()) {\r
105                 concat.put(key, element.get(key));\r
106             }\r
107         }\r
108 \r
109         if(profile != null) {\r
110             for(String key : profile.keySet()) {\r
111                 concat.put(key, profile.get(key));\r
112             }\r
113         }\r
114 \r
115         for(String property : concat.keySet()) {\r
116             //System.out.println("sync: "+property+" | "+concat.get(property));\r
117             // TODO: property names containing dots "." should be splitted and treated as hierarchical properties \r
118             if(property.contains(".")) {\r
119                 String t[] = property.split("\\.");\r
120                 if(t.length == 2) { // FIXME: add support for deeper hierarchy\r
121                     String child_name = t[0];\r
122                     String property_name = t[1];\r
123                     if(node instanceof G2DParentNode) {\r
124                         INode child = NodeUtil.findChildById((G2DParentNode)node, child_name);\r
125                         if(child != null)\r
126                             NodeUtil.setPropertyIfSupported(property_name, concat.get(property), child);\r
127                     }\r
128                 }\r
129             } else {\r
130                 NodeUtil.setPropertyIfSupported(property, concat.get(property), node);\r
131             }\r
132         }\r
133     }\r
134 }\r