]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ExpressionStyle.java
Combination of Simantics-platform related changes and fixes for district
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / ExpressionStyle.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.diagram.adapter;
13
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Map;
17
18 import org.simantics.databoard.Bindings;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.utils.OrderedSetUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.variable.Variable;
24 import org.simantics.diagram.profile.StyleBase;
25 import org.simantics.diagram.stubs.DiagramResource;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.modeling.ModelingResources;
28 import org.simantics.operation.Layer0X;
29 import org.simantics.scenegraph.INode;
30 import org.simantics.scenegraph.profile.EvaluationContext;
31 import org.simantics.scenegraph.profile.common.ProfileVariables;
32 import org.simantics.structural.stubs.StructuralResource2;
33
34 /**
35  * @author Antti Villberg
36  */
37 public class ExpressionStyle extends StyleBase<Map<String, Object>> {
38
39         final Resource style;
40
41         public ExpressionStyle(Resource style) {
42                 this.style = style;
43         }
44
45         @Override
46         public Map<String, Object> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable activeVariable) throws DatabaseException {
47
48                 //System.err.println("calculateExpressionStyle " + activeVariable.getURI(graph));
49
50                 Layer0 L0 = Layer0.getInstance(graph);
51                 Layer0X L0X = Layer0X.getInstance(graph);
52                 DiagramResource DIA = DiagramResource.getInstance(graph);
53                 ModelingResources MOD = ModelingResources.getInstance(graph);
54                 StructuralResource2 SR = StructuralResource2.getInstance(graph);
55                 
56                 String key = graph.getPossibleRelatedValue(style, DIA.HasVariableName, Bindings.STRING);
57                 String expression = graph.getPossibleRelatedValue(style, L0X.HasExpression, Bindings.STRING);
58                 //System.out.println(key + ": " + expression);
59
60                 Resource module = graph.getPossibleObject(element, MOD.ElementToComponent);
61                 if (module == null)
62                         return null;
63
64                 Resource svg = null;
65                 Resource elementType = graph.getPossibleType(element, L0.Entity);
66                 if(elementType == null) return null;
67                 Resource definition = graph.getPossibleObject(elementType, SR.IsDefinedBy);
68                 if(definition == null) return null;
69                 List<Resource> items = OrderedSetUtils.toList(graph, definition);
70                 if(items == null) return null;
71                 for(Resource item : items) {
72                         if(graph.isInstanceOf(item, DIA.AnimatedSVGElement)) {
73                                 svg = item;
74                                 break;
75                         }
76                 }
77                 if(svg == null) return null;
78
79                 String moduleName = graph.getPossibleRelatedValue(module, L0.HasName, Bindings.STRING);
80                 if (moduleName == null)
81                         return null;
82
83                 Variable moduleVariable = activeVariable.getChild(graph, moduleName);
84
85                 //System.err.println("mv " + moduleVariable.getURI(graph));
86
87                 Object value = moduleVariable.getPossiblePropertyValue(graph, expression);
88                 Map<String, Object> values = Collections.singletonMap(key, value);
89                 
90 //              for(Resource desc : graph.getObjects(svg, DIA.HasConfigurationVariable)) {
91 //                      String name = graph.getRelatedValue(desc, L0.HasName);
92 //                      System.err.println("name=" + name);
93 //                      String formula = graph.getRelatedValue(desc, L0X.HasExpression);
94 //                      Object value = moduleVariable.getPossiblePropertyValue(graph, formula);
95 //                      System.err.println(name + " => " + formula + " => " + value);
96 //                      values.put(name, value);
97 //              }
98
99                 return values;
100
101         }
102
103         @Override
104         public void applyStyleForNode(EvaluationContext evaluationContext, INode node, Map<String, Object> values) {
105             //System.out.println(this + ": apply values " + values);
106             if (values == null)
107                 return;
108
109             ProfileVariables.claimNodeProperty(node, "valuezz", values, evaluationContext);
110             
111 //          Variables.set(element, "valuezz", values, observer);
112 ////        for(Map.Entry<String, Object> entry : values.entrySet()) {
113 ////            Variables.set(element, entry.getKey(), entry.getValue(), observer);
114 ////        }
115 //
116 //          PropertySetter setter = element.getElementClass().getAtMostOneItemOfClass(PropertySetter.class);
117 //          if (setter != null)
118 //              setter.syncPropertiesToNode(element);
119         }
120
121         @Override
122         protected void cleanupStyleForNode(EvaluationContext observer, INode node) {
123             ProfileVariables.init(node, observer);
124         }
125
126         @Override
127         public String toString() {
128                 return "Expression";
129         }
130
131 }