]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/common/ProfileVariables.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph.profile / src / org / simantics / scenegraph / profile / common / ProfileVariables.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.scenegraph.profile.common;\r
13 \r
14 import org.simantics.scenegraph.INode;\r
15 import org.simantics.scenegraph.NodeException;\r
16 import org.simantics.scenegraph.ParentNode;\r
17 import org.simantics.scenegraph.g2d.G2DParentNode;\r
18 import org.simantics.scenegraph.profile.EvaluationContext;\r
19 import org.simantics.scenegraph.profile.Observer;\r
20 import org.simantics.scenegraph.profile.Style;\r
21 import org.simantics.scenegraph.utils.InitValueSupport;\r
22 import org.simantics.scenegraph.utils.NodeUtil;\r
23 \r
24 /**\r
25  * Utilities for handling scene graph nodes in diagram profile styles.\r
26  * \r
27  * @see Style\r
28  */\r
29 public class ProfileVariables {\r
30 \r
31     public static void init(INode node, Observer observer) {\r
32         if(node instanceof InitValueSupport) {\r
33             ((InitValueSupport)node).initValues();\r
34         }\r
35     }\r
36     \r
37     /**\r
38      * @param node\r
39      * @param id\r
40      * @return\r
41      */\r
42     @SuppressWarnings("unchecked")\r
43     public static <N extends INode> N browseChild(INode node, String id) {\r
44 \r
45         // There was an empty string meaning that input should be returned\r
46         if (id.isEmpty())\r
47             return (N)node;\r
48 \r
49         String[] parts = id.split("\\.");\r
50         for (String part : parts) {\r
51             if ("*".equals(part)) {\r
52                 node = NodeUtil.getFirstChild(node);\r
53             } else {\r
54                 node = NodeUtil.getChildById(node, part);\r
55             }\r
56             if (node == null)\r
57                 return null;\r
58         }\r
59 \r
60         return (N)node;\r
61 \r
62     }\r
63 \r
64     /*\r
65      * Sets named property for named node starting from top level. The path identifies a node from named child hierarchy (separated by '.').\r
66      */\r
67     public static void claimNodeProperty(INode node, String property, Object value, EvaluationContext evaluationContext) {\r
68         \r
69         if(node == null) {\r
70             evaluationContext.update();\r
71             return;\r
72         }\r
73         \r
74 //        for(String property : concat.keySet()) {\r
75             //System.out.println("sync: "+property+" | "+concat.get(property));\r
76             // TODO: property names containing dots "." should be splitted and treated as hierarchical properties \r
77             if(property.contains(".")) {\r
78                 String t[] = property.split("\\.");\r
79                 if(t.length == 2) { // FIXME: add support for deeper hierarchy\r
80                     String child_name = t[0];\r
81                     String property_name = t[1];\r
82                     if(node instanceof G2DParentNode) {\r
83                         INode child = NodeUtil.findChildById((G2DParentNode)node, child_name);\r
84                         if(child != null)\r
85                             NodeUtil.setPropertyIfSupported(property_name, value, child);\r
86                     }\r
87                 }\r
88             } else {\r
89                 NodeUtil.setPropertyIfSupported(property, value, node);\r
90             }\r
91 //        }\r
92         \r
93     }\r
94 \r
95     /*\r
96      * Ensures that the element contains a named child node in given path with\r
97      * given class. All nodes in the path are expected to exist.\r
98      */\r
99     public static <N extends INode> N claimChild(INode node, String path, String name, Class<N> clazz, Observer observer) {\r
100 \r
101         INode child = browseChild(node, path);\r
102         if(child == null) {\r
103             observer.exception(new NullPointerException("Scenegraph child node was not found: " + path));\r
104             observer.update();\r
105             return null;\r
106         }\r
107 \r
108         INode existing = NodeUtil.getChildById(child, name);\r
109         if (existing == null) {\r
110             if (child instanceof ParentNode<?>) {\r
111                 existing = ((ParentNode<?>) child).addNode(name, clazz);\r
112             } else {\r
113                 observer.exception(new NodeException("Cannot claim child node for non-parent-node " + child));\r
114                 return null;\r
115             }\r
116         } else if (!clazz.isInstance(existing)) {\r
117             observer.exception(new ClassCastException("Class of existing child node (" + existing.getClass() + ") with path=" + path + " and name=" + name + " is invalid, requested " + clazz));\r
118             return null;\r
119         }\r
120         return clazz.cast(existing);\r
121     }\r
122 \r
123     /**\r
124      * @param node\r
125      * @param path\r
126      * @param name\r
127      */\r
128     public static void denyChild(INode node, String path, String name) {\r
129         INode child = browseChild(node, path);\r
130         if (child == null)\r
131             return;\r
132 \r
133         if (child instanceof ParentNode<?>) {\r
134             ((ParentNode<?>) child).removeNode(name);\r
135         }\r
136     }\r
137 \r
138     public static void denyChild(INode node, String name) {\r
139         denyChild(node, "", name);\r
140     }\r
141 \r
142     public static void denyChildren(INode node, String prefix) {\r
143         if (node instanceof ParentNode<?>) {\r
144             ParentNode<?> parent = (ParentNode<?>) node;\r
145 \r
146             for (String childId : NodeUtil.filterDirectChildIds(parent, prefix))\r
147                 parent.removeNode(childId);\r
148         }\r
149     }\r
150 \r
151 }\r