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.scenegraph.profile.common;
\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
25 * Utilities for handling scene graph nodes in diagram profile styles.
\r
29 public class ProfileVariables {
\r
31 public static void init(INode node, Observer observer) {
\r
32 if(node instanceof InitValueSupport) {
\r
33 ((InitValueSupport)node).initValues();
\r
42 @SuppressWarnings("unchecked")
\r
43 public static <N extends INode> N browseChild(INode node, String id) {
\r
45 // There was an empty string meaning that input should be returned
\r
49 String[] parts = id.split("\\.");
\r
50 for (String part : parts) {
\r
51 if ("*".equals(part)) {
\r
52 node = NodeUtil.getFirstChild(node);
\r
54 node = NodeUtil.getChildById(node, part);
\r
65 * Sets named property for named node starting from top level. The path identifies a node from named child hierarchy (separated by '.').
\r
67 public static void claimNodeProperty(INode node, String property, Object value, EvaluationContext evaluationContext) {
\r
70 evaluationContext.update();
\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
85 NodeUtil.setPropertyIfSupported(property_name, value, child);
\r
89 NodeUtil.setPropertyIfSupported(property, value, node);
\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
99 public static <N extends INode> N claimChild(INode node, String path, String name, Class<N> clazz, Observer observer) {
\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
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
113 observer.exception(new NodeException("Cannot claim child node for non-parent-node " + child));
\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
120 return clazz.cast(existing);
\r
128 public static void denyChild(INode node, String path, String name) {
\r
129 INode child = browseChild(node, path);
\r
133 if (child instanceof ParentNode<?>) {
\r
134 ((ParentNode<?>) child).removeNode(name);
\r
138 public static void denyChild(INode node, String name) {
\r
139 denyChild(node, "", name);
\r
142 public static void denyChildren(INode node, String prefix) {
\r
143 if (node instanceof ParentNode<?>) {
\r
144 ParentNode<?> parent = (ParentNode<?>) node;
\r
146 for (String childId : NodeUtil.filterDirectChildIds(parent, prefix))
\r
147 parent.removeNode(childId);
\r