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.utils.NodeUtil;
23 import org.simantics.utils.datastructures.hints.IHintContext.Key;
24 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
26 public class PlainElementPropertySetter implements PropertySetter {
28 private static final long serialVersionUID = -3165827599737160621L;
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;
34 public PlainElementPropertySetter(Key key) {
39 public void overrideProperties(IElement e, Properties properties) {
40 Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);
42 profile = new Properties();
43 e.setHint(KEY_PROFILE_PROPERTIES, profile);
46 profile.putAll(properties);
50 public void overrideProperty(IElement e, String name, Object value) {
51 //System.out.println(name+"="+value);
52 Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);
54 profile = new Properties();
55 e.setHint(KEY_PROFILE_PROPERTIES, profile);
57 profile.put(name, value);
60 private Node findSingle(Node node, Class<?> clazz) {
62 // System.err.println("findSingle " + node);
64 if(clazz.isInstance(node)) return node;
66 if(node instanceof G2DParentNode) {
67 G2DParentNode parent = (G2DParentNode)node;
69 for(IG2DNode child : parent.getNodes()) {
70 if(child instanceof Node) {
71 Node test = findSingle((Node)child, clazz);
73 if(found == null) found = test;
85 private Node findAnimatedNode(Node node) {
86 Node animated = findSingle(node, AnimatedNode.class);
87 if(animated != null) return animated;
92 public void syncPropertiesToNode(IElement e) {
93 Properties profile = e.getHint(KEY_PROFILE_PROPERTIES);
94 Properties element = e.getHint(ElementHints.KEY_ELEMENT_PROPERTIES);
96 Node node = e.getHint(this.key);
97 if(node == null) return; // FIXME
99 node = findAnimatedNode(node);
101 Properties concat = new Properties();
102 if(element != null) {
103 for(String key : element.keySet()) {
104 concat.put(key, element.get(key));
108 if(profile != null) {
109 for(String key : profile.keySet()) {
110 concat.put(key, profile.get(key));
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);
125 NodeUtil.setPropertyIfSupported(property_name, concat.get(property), child);
129 NodeUtil.setPropertyIfSupported(property, concat.get(property), node);