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