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