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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.elementclass;
\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
27 public class PlainElementPropertySetter implements PropertySetter {
\r
29 private static final long serialVersionUID = -3165827599737160621L;
\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
35 public PlainElementPropertySetter(Key key) {
\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
47 profile.putAll(properties);
\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
58 profile.put(name, value);
\r
61 private Node findSingle(Node node, Class<?> clazz) {
\r
63 // System.err.println("findSingle " + node);
\r
65 if(clazz.isInstance(node)) return node;
\r
67 if(node instanceof G2DParentNode) {
\r
68 G2DParentNode parent = (G2DParentNode)node;
\r
70 for(IG2DNode child : parent.getNodes()) {
\r
71 if(child instanceof Node) {
\r
72 Node test = findSingle((Node)child, clazz);
\r
74 if(found == null) found = test;
\r
86 private Node findAnimatedNode(Node node) {
\r
87 Node animated = findSingle(node, AnimatedSVGNode.class);
\r
88 if(animated != null) return animated;
\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
97 Node node = e.getHint(this.key);
\r
98 if(node == null) return; // FIXME
\r
100 node = findAnimatedNode(node);
\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
109 if(profile != null) {
\r
110 for(String key : profile.keySet()) {
\r
111 concat.put(key, profile.get(key));
\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
126 NodeUtil.setPropertyIfSupported(property_name, concat.get(property), child);
\r
130 NodeUtil.setPropertyIfSupported(property, concat.get(property), node);
\r