--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.scenegraph.profile.common;\r
+\r
+import org.simantics.scenegraph.INode;\r
+import org.simantics.scenegraph.NodeException;\r
+import org.simantics.scenegraph.ParentNode;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.profile.EvaluationContext;\r
+import org.simantics.scenegraph.profile.Observer;\r
+import org.simantics.scenegraph.profile.Style;\r
+import org.simantics.scenegraph.utils.InitValueSupport;\r
+import org.simantics.scenegraph.utils.NodeUtil;\r
+\r
+/**\r
+ * Utilities for handling scene graph nodes in diagram profile styles.\r
+ * \r
+ * @see Style\r
+ */\r
+public class ProfileVariables {\r
+\r
+ public static void init(INode node, Observer observer) {\r
+ if(node instanceof InitValueSupport) {\r
+ ((InitValueSupport)node).initValues();\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * @param node\r
+ * @param id\r
+ * @return\r
+ */\r
+ @SuppressWarnings("unchecked")\r
+ public static <N extends INode> N browseChild(INode node, String id) {\r
+\r
+ // There was an empty string meaning that input should be returned\r
+ if (id.isEmpty())\r
+ return (N)node;\r
+\r
+ String[] parts = id.split("\\.");\r
+ for (String part : parts) {\r
+ if ("*".equals(part)) {\r
+ node = NodeUtil.getFirstChild(node);\r
+ } else {\r
+ node = NodeUtil.getChildById(node, part);\r
+ }\r
+ if (node == null)\r
+ return null;\r
+ }\r
+\r
+ return (N)node;\r
+\r
+ }\r
+\r
+ /*\r
+ * Sets named property for named node starting from top level. The path identifies a node from named child hierarchy (separated by '.').\r
+ */\r
+ public static void claimNodeProperty(INode node, String property, Object value, EvaluationContext evaluationContext) {\r
+ \r
+ if(node == null) {\r
+ evaluationContext.update();\r
+ return;\r
+ }\r
+ \r
+// for(String property : concat.keySet()) {\r
+ //System.out.println("sync: "+property+" | "+concat.get(property));\r
+ // TODO: property names containing dots "." should be splitted and treated as hierarchical properties \r
+ if(property.contains(".")) {\r
+ String t[] = property.split("\\.");\r
+ if(t.length == 2) { // FIXME: add support for deeper hierarchy\r
+ String child_name = t[0];\r
+ String property_name = t[1];\r
+ if(node instanceof G2DParentNode) {\r
+ INode child = NodeUtil.findChildById((G2DParentNode)node, child_name);\r
+ if(child != null)\r
+ NodeUtil.setPropertyIfSupported(property_name, value, child);\r
+ }\r
+ }\r
+ } else {\r
+ NodeUtil.setPropertyIfSupported(property, value, node);\r
+ }\r
+// }\r
+ \r
+ }\r
+\r
+ /*\r
+ * Ensures that the element contains a named child node in given path with\r
+ * given class. All nodes in the path are expected to exist.\r
+ */\r
+ public static <N extends INode> N claimChild(INode node, String path, String name, Class<N> clazz, Observer observer) {\r
+\r
+ INode child = browseChild(node, path);\r
+ if(child == null) {\r
+ observer.exception(new NullPointerException("Scenegraph child node was not found: " + path));\r
+ observer.update();\r
+ return null;\r
+ }\r
+\r
+ INode existing = NodeUtil.getChildById(child, name);\r
+ if (existing == null) {\r
+ if (child instanceof ParentNode<?>) {\r
+ existing = ((ParentNode<?>) child).addNode(name, clazz);\r
+ } else {\r
+ observer.exception(new NodeException("Cannot claim child node for non-parent-node " + child));\r
+ return null;\r
+ }\r
+ } else if (!clazz.isInstance(existing)) {\r
+ observer.exception(new ClassCastException("Class of existing child node (" + existing.getClass() + ") with path=" + path + " and name=" + name + " is invalid, requested " + clazz));\r
+ return null;\r
+ }\r
+ return clazz.cast(existing);\r
+ }\r
+\r
+ /**\r
+ * @param node\r
+ * @param path\r
+ * @param name\r
+ */\r
+ public static void denyChild(INode node, String path, String name) {\r
+ INode child = browseChild(node, path);\r
+ if (child == null)\r
+ return;\r
+\r
+ if (child instanceof ParentNode<?>) {\r
+ ((ParentNode<?>) child).removeNode(name);\r
+ }\r
+ }\r
+\r
+ public static void denyChild(INode node, String name) {\r
+ denyChild(node, "", name);\r
+ }\r
+\r
+ public static void denyChildren(INode node, String prefix) {\r
+ if (node instanceof ParentNode<?>) {\r
+ ParentNode<?> parent = (ParentNode<?>) node;\r
+\r
+ for (String childId : NodeUtil.filterDirectChildIds(parent, prefix))\r
+ parent.removeNode(childId);\r
+ }\r
+ }\r
+\r
+}\r