]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/PlainElementPropertySetter.java
G2DParentNode handles "undefined" child bounds separately
[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.utils.NodeUtil;
23 import org.simantics.utils.datastructures.hints.IHintContext.Key;
24 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
25
26 public class PlainElementPropertySetter implements PropertySetter {
27
28     private static final long serialVersionUID = -3165827599737160621L;
29
30     public static final PlainElementPropertySetter INSTANCE = new PlainElementPropertySetter(ElementHints.KEY_SG_NODE);
31     public static final Key KEY_PROFILE_PROPERTIES = new KeyOf(Properties.class, "ELEMENT_PROFILE_PROPERTIES");
32     private final Key key;
33
34     public PlainElementPropertySetter(Key key) {
35         this.key = key;
36     }
37
38     @Override
39     public void overrideProperties(IElement e, Properties properties) {
40         Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);
41         if(profile == null) {
42             profile = new Properties();
43             e.setHint(KEY_PROFILE_PROPERTIES, profile);
44         }
45         profile.clear();
46         profile.putAll(properties);
47     }
48
49     @Override
50     public void overrideProperty(IElement e, String name, Object value) {
51         //System.out.println(name+"="+value);
52         Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);
53         if(profile == null) {
54             profile = new Properties();
55             e.setHint(KEY_PROFILE_PROPERTIES, profile);
56         }
57         profile.put(name, value);
58     }
59
60     private Node findSingle(Node node, Class<?> clazz) {
61
62 //              System.err.println("findSingle " + node);
63
64         if(clazz.isInstance(node)) return node;
65
66         if(node instanceof G2DParentNode) {
67             G2DParentNode parent = (G2DParentNode)node;
68             Node found = null;
69             for(IG2DNode child : parent.getNodes()) {
70                 if(child instanceof Node) {
71                     Node test = findSingle((Node)child, clazz);
72                     if(test != null) {
73                         if(found == null) found = test;
74                         else return null;
75                     }
76                 }
77             }
78             return found;
79         } else {
80             return null;
81         }
82
83     }
84
85     private Node findAnimatedNode(Node node) {
86         Node animated = findSingle(node, AnimatedNode.class);
87         if(animated != null) return animated;
88         else return node;
89     }
90
91     @Override
92     public void syncPropertiesToNode(IElement e) {
93         Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);
94         Properties element = e.getHint(ElementHints.KEY_ELEMENT_PROPERTIES);
95
96         Node node = e.getHint(this.key);
97         if(node == null) return; // FIXME
98
99         node = findAnimatedNode(node);
100
101         Properties concat = new Properties();
102         if(element != null) {
103             for(String key : element.keySet()) {
104                 concat.put(key, element.get(key));
105             }
106         }
107
108         if(profile != null) {
109             for(String key : profile.keySet()) {
110                 concat.put(key, profile.get(key));
111             }
112         }
113
114         for(String property : concat.keySet()) {
115             //System.out.println("sync: "+property+" | "+concat.get(property));
116             // TODO: property names containing dots "." should be splitted and treated as hierarchical properties 
117             if(property.contains(".")) {
118                 String t[] = property.split("\\.");
119                 if(t.length == 2) { // FIXME: add support for deeper hierarchy
120                     String child_name = t[0];
121                     String property_name = t[1];
122                     if(node instanceof G2DParentNode) {
123                         INode child = NodeUtil.findChildById((G2DParentNode)node, child_name);
124                         if(child != null)
125                             NodeUtil.setPropertyIfSupported(property_name, concat.get(property), child);
126                     }
127                 }
128             } else {
129                 NodeUtil.setPropertyIfSupported(property, concat.get(property), node);
130             }
131         }
132     }
133 }