--- /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.diagram.adapter;\r
+\r
+import java.util.Collections;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.OrderedSetUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.diagram.profile.StyleBase;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.scenegraph.INode;\r
+import org.simantics.scenegraph.profile.EvaluationContext;\r
+import org.simantics.scenegraph.profile.common.ProfileVariables;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+public class ExpressionStyle extends StyleBase<Map<String, Object>> {\r
+\r
+ final Resource style;\r
+\r
+ public ExpressionStyle(Resource style) {\r
+ this.style = style;\r
+ }\r
+\r
+ @Override\r
+ public Map<String, Object> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable activeVariable) throws DatabaseException {\r
+\r
+ //System.err.println("calculateExpressionStyle " + activeVariable.getURI(graph));\r
+\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ Layer0X L0X = Layer0X.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ ModelingResources MOD = ModelingResources.getInstance(graph);\r
+ StructuralResource2 SR = StructuralResource2.getInstance(graph);\r
+ \r
+ String key = graph.getPossibleRelatedValue(style, DIA.HasVariableName, Bindings.STRING);\r
+ String expression = graph.getPossibleRelatedValue(style, L0X.HasExpression, Bindings.STRING);\r
+ //System.out.println(key + ": " + expression);\r
+\r
+ Resource module = graph.getPossibleObject(element, MOD.ElementToComponent);\r
+ if (module == null)\r
+ return null;\r
+\r
+ Resource svg = null;\r
+ Resource elementType = graph.getPossibleType(element, L0.Entity);\r
+ if(elementType == null) return null;\r
+ Resource definition = graph.getPossibleObject(elementType, SR.IsDefinedBy);\r
+ if(definition == null) return null;\r
+ List<Resource> items = OrderedSetUtils.toList(graph, definition);\r
+ if(items == null) return null;\r
+ for(Resource item : items) {\r
+ if(graph.isInstanceOf(item, DIA.AnimatedSVGElement)) {\r
+ svg = item;\r
+ break;\r
+ }\r
+ }\r
+ if(svg == null) return null;\r
+\r
+ String moduleName = graph.getPossibleRelatedValue(module, L0.HasName, Bindings.STRING);\r
+ if (moduleName == null)\r
+ return null;\r
+\r
+ Variable moduleVariable = activeVariable.getChild(graph, moduleName);\r
+\r
+ //System.err.println("mv " + moduleVariable.getURI(graph));\r
+\r
+ Object value = moduleVariable.getPossiblePropertyValue(graph, expression);\r
+ Map<String, Object> values = Collections.singletonMap(key, value);\r
+ \r
+// for(Resource desc : graph.getObjects(svg, DIA.HasConfigurationVariable)) {\r
+// String name = graph.getRelatedValue(desc, L0.HasName);\r
+// System.err.println("name=" + name);\r
+// String formula = graph.getRelatedValue(desc, L0X.HasExpression);\r
+// Object value = moduleVariable.getPossiblePropertyValue(graph, formula);\r
+// System.err.println(name + " => " + formula + " => " + value);\r
+// values.put(name, value);\r
+// }\r
+\r
+ return values;\r
+\r
+ }\r
+\r
+ @Override\r
+ public void applyStyleForNode(EvaluationContext evaluationContext, INode node, Map<String, Object> values) {\r
+ //System.out.println(this + ": apply values " + values);\r
+ if (values == null)\r
+ return;\r
+\r
+ ProfileVariables.claimNodeProperty(node, "valuezz", values, evaluationContext);\r
+ \r
+// Variables.set(element, "valuezz", values, observer);\r
+//// for(Map.Entry<String, Object> entry : values.entrySet()) {\r
+//// Variables.set(element, entry.getKey(), entry.getValue(), observer);\r
+//// }\r
+//\r
+// PropertySetter setter = element.getElementClass().getAtMostOneItemOfClass(PropertySetter.class);\r
+// if (setter != null)\r
+// setter.syncPropertiesToNode(element);\r
+ }\r
+\r
+ @Override\r
+ protected void cleanupStyleForNode(EvaluationContext observer, INode node) {\r
+ ProfileVariables.init(node, observer);\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return "Expression";\r
+ }\r
+\r
+}\r