]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/function/All.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / function / All.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * 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.modeling.template2d.ui.function;
13
14 import java.util.Collections;
15 import java.util.Map;
16 import java.util.TreeMap;
17
18 import org.simantics.browsing.ui.model.modifiers.NoModifierRule;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
23 import org.simantics.db.common.request.UnaryRead;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.SelectionHints;
26 import org.simantics.db.layer0.variable.Variable;
27 import org.simantics.db.layer0.variable.Variables;
28 import org.simantics.diagram.content.ElementContext;
29 import org.simantics.diagram.function.PredefinedVariables;
30 import org.simantics.diagram.stubs.DiagramResource;
31 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
32 import org.simantics.modeling.ModelingUtils;
33 import org.simantics.modeling.template2d.ontology.Template2dResource;
34 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;
35 import org.simantics.scenegraph.profile.request.RuntimeProfileActiveEntryResources;
36 import org.simantics.scl.reflection.annotations.SCLValue;
37 import org.simantics.utils.ui.ISelectionUtils;
38
39
40 public class All {
41
42     @SCLValue(type = "a -> b -> c")
43     public static Object variableTransformation(Object _graph, Object input) throws DatabaseException {
44
45         ReadGraph graph = (ReadGraph) _graph;
46
47         Resource object = ISelectionUtils.getSinglePossibleKey(input, SelectionHints.KEY_MAIN, Resource.class);
48         if (object == null)
49             return null;
50
51         Resource runtime = ISelectionUtils.getSinglePossibleKey(input, SelectionHints.KEY_VARIABLE_RESOURCE,
52                 Resource.class);
53         if (runtime == null)
54             return null;
55
56         Variable inputVariable = graph.syncRequest(new UnaryRead<Resource, Variable>(runtime) {
57
58             @Override
59             public Variable perform(ReadGraph graph) throws DatabaseException {
60
61                 DiagramResource dr = DiagramResource.getInstance(graph);
62                 String diagramVariable = graph.getPossibleRelatedValue(parameter, dr.RuntimeDiagram_HasVariable);
63                 return Variables.getVariable(graph, diagramVariable);
64
65             }
66
67         });
68         if (inputVariable == null)
69             return null;
70
71         Resource component = ModelingUtils.getPossibleElementCorrespondendence(graph, object);
72         if (component == null)
73             return null;
74         return inputVariable.browsePossible(graph, component);
75
76     }
77
78     @SCLValue(type = "a -> b -> c")
79     public static Object resourceVariableTransformation(Object _graph, Object input) throws DatabaseException {
80
81         ReadGraph graph = (ReadGraph) _graph;
82
83         Resource object = ISelectionUtils.getSinglePossibleKey(input, SelectionHints.KEY_MAIN, Resource.class);
84         if (object == null)
85             return null;
86         
87         return Variables.getVariable(graph, object);
88
89 //        Resource runtime = ISelectionUtils.getSinglePossibleKey(input, SelectionHints.KEY_VARIABLE_RESOURCE,
90 //                Resource.class);
91 //        if (runtime == null)
92 //            return null;
93 //
94 //        Variable inputVariable = graph.syncRequest(new UnaryRead<Resource, Variable>(runtime) {
95 //
96 //            @Override
97 //            public Variable perform(ReadGraph graph) throws DatabaseException {
98 //
99 //                DiagramResource dr = DiagramResource.getInstance(graph);
100 //                String diagramVariable = graph.getPossibleRelatedValue(parameter, dr.RuntimeDiagram_HasVariable);
101 //                return Variables.getVariable(graph, diagramVariable);
102 //
103 //            }
104 //
105 //        });
106 //        if (inputVariable == null)
107 //            return null;
108 //
109 //        Resource component = ModelingUtils.getPossibleElementCorrespondendence(graph, object);
110 //        if (component == null)
111 //            return null;
112 //        return inputVariable.browsePossible(graph, component);
113
114     }
115
116     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
117     public static String variableReference(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
118
119         Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
120
121 //        System.out.println("CONTEXT: " + context.getURI(graph));
122 //        Resource runtime = ScenegraphLoaderUtils.getRuntime(graph, context);
123 //        System.out.println("runtime: " + NameUtils.getSafeName(graph, runtime));
124
125         String path = graph.getRelatedValue(converter, TEMPLATE2D.Profiles_VariableReference_path, Bindings.STRING);
126         if (path == null)
127             return null;
128
129         Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
130         PredefinedVariables vars = PredefinedVariables.getInstance();
131         Variable property = vars.getVariable(graph, path, converter, selection);
132         if (property != null)
133             return property.getValue(graph).toString();
134
135         return null;
136     }
137
138     public static Resource getTemplate(ReadGraph graph, Resource runtimeDiagram) throws DatabaseException {
139         Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
140         DiagramResource DIA = DiagramResource.getInstance(graph);
141         Resource diagram = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
142         if (diagram != null) {
143             Resource template = graph.getPossibleObject(diagram, TEMPLATE2D.HasDrawingTemplate);
144             if (template != null)
145                 return template;
146         }
147         return null;
148         }
149
150         @SCLValue(type = "ReadGraph -> Resource -> ElementContext -> a")
151     public static Object flagTransform(ReadGraph graph, Resource converter, ElementContext context) throws DatabaseException {
152                 //Layer0 L0 = Layer0.getInstance(graph);
153                 DiagramResource DIA = DiagramResource.getInstance(graph);
154         Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
155
156                 Resource flag = context.element;
157                 Resource runtimeDiagram = context.runtime;
158
159                 Resource diagram = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
160                 if (diagram == null)
161                         return org.simantics.diagram.function.All.flagTransform(graph, converter, context);
162
163                 Resource template = getTemplate(graph, runtimeDiagram);
164                 if (template == null)
165                         return org.simantics.diagram.function.All.flagTransform(graph, converter, context);
166
167                 double gridSize = DiagramGraphUtil.getGridSize(graph, diagram, 0.0);
168
169                 String tableName = graph.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableBinding, Bindings.STRING);
170                 Integer rowIndex = graph.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableRowIndex, Bindings.INTEGER);
171                 FlagInfo flagInfo = new FlagInfo();
172                 flagInfo.flag = flag;
173                 if (tableName != null && tableName.length() > 0)
174                         flagInfo.flagTableName = tableName;
175                 flagInfo.flagTableRowIndex = rowIndex;
176                 
177                 Map<String, FlagTableInfo> name2table = graph.syncRequest(new DrawingTemplateInfo(template), TransientCacheListener.<TreeMap<String, FlagTableInfo>> instance());
178         if (name2table == null)
179                 name2table = Collections.emptyMap();
180         
181         if (!name2table.containsKey(flagInfo.flagTableName))
182                 return org.simantics.diagram.function.All.flagTransform(graph, converter, context);
183         
184         double[] mat = graph.getRelatedValue(flag, DIA.HasTransform, Bindings.DOUBLE_ARRAY);
185                 TranslateFlag com = new TranslateFlag(flagInfo, name2table, mat, gridSize);
186                 
187         boolean isIOTablesActive = false;
188         for (Resource entry : graph.syncRequest(new RuntimeProfileActiveEntryResources(runtimeDiagram))) {
189                 //ProfileEntry profile = graph.adapt(entry, ProfileEntry.class);
190                 if (TEMPLATE2D.Profiles_DrawingFlagTables.equals(entry)){
191                         isIOTablesActive = true;
192                         break;
193                 }
194         }
195
196         if (isIOTablesActive)
197                 return com.perform();
198         return org.simantics.diagram.function.All.flagTransform(graph, converter, context);
199     }
200
201     @SCLValue(type = "ReadGraph -> Resource -> a -> b")
202     public static Object variableReferencePath(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
203         if (context instanceof Variable) {
204             // Input variable is (:VariableReference)#HasDisplayValue. Need to
205             // go to parent to find the variable to be edited.
206             //System.out.println("R: " + NameUtils.getSafeName(graph, resource));
207             //System.out.println("V: " + ((Variable) context).getURI(graph));
208             Variable document = ((Variable)context).browse(graph, ".");
209             //System.out.println("D: " + document.getURI(graph));
210             String path = document.getPossiblePropertyValue(graph, "path", Bindings.STRING);
211             return path;
212         }
213         return null;
214     }
215
216     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
217     public static Object variableReferenceModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
218         return NoModifierRule.NO_MODIFIER;
219     }
220
221 }