]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/PlainElementPropertySetter.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / PlainElementPropertySetter.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.elementclass;
13
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;
26
27 public class PlainElementPropertySetter implements PropertySetter {
28
29     private static final long serialVersionUID = -3165827599737160621L;
30
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;
34
35     public PlainElementPropertySetter(Key key) {
36         this.key = key;
37     }
38
39     @Override
40     public void overrideProperties(IElement e, Properties properties) {
41         Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);
42         if(profile == null) {
43             profile = new Properties();
44             e.setHint(KEY_PROFILE_PROPERTIES, profile);
45         }
46         profile.clear();
47         profile.putAll(properties);
48     }
49
50     @Override
51     public void overrideProperty(IElement e, String name, Object value) {
52         //System.out.println(name+"="+value);
53         Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);
54         if(profile == null) {
55             profile = new Properties();
56             e.setHint(KEY_PROFILE_PROPERTIES, profile);
57         }
58         profile.put(name, value);
59     }
60
61     private Node findSingle(Node node, Class<?> clazz) {
62
63 //              System.err.println("findSingle " + node);
64
65         if(clazz.isInstance(node)) return node;
66
67         if(node instanceof G2DParentNode) {
68             G2DParentNode parent = (G2DParentNode)node;
69             Node found = null;
70             for(IG2DNode child : parent.getNodes()) {
71                 if(child instanceof Node) {
72                     Node test = findSingle((Node)child, clazz);
73                     if(test != null) {
74                         if(found == null) found = test;
75                         else return null;
76                     }
77                 }
78             }
79             return found;
80         } else {
81             return null;
82         }
83
84     }
85
86     private Node findAnimatedNode(Node node) {
87         Node animated = findSingle(node, AnimatedSVGNode.class);
88         if(animated != null) return animated;
89         else return node;
90     }
91
92     @Override
93     public void syncPropertiesToNode(IElement e) {
94         Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);
95         Properties element = e.getHint(ElementHints.KEY_ELEMENT_PROPERTIES);
96
97         Node node = e.getHint(this.key);
98         if(node == null) return; // FIXME
99
100         node = findAnimatedNode(node);
101
102         Properties concat = new Properties();
103         if(element != null) {
104             for(String key : element.keySet()) {
105                 concat.put(key, element.get(key));
106             }
107         }
108
109         if(profile != null) {
110             for(String key : profile.keySet()) {
111                 concat.put(key, profile.get(key));
112             }
113         }
114
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);
125                         if(child != null)
126                             NodeUtil.setPropertyIfSupported(property_name, concat.get(property), child);
127                     }
128                 }
129             } else {
130                 NodeUtil.setPropertyIfSupported(property, concat.get(property), node);
131             }
132         }
133     }
134 }