]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/function/All.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / function / All.java
diff --git a/bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/function/All.java b/bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/function/All.java
new file mode 100644 (file)
index 0000000..a0f599e
--- /dev/null
@@ -0,0 +1,221 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * 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.modeling.template2d.ui.function;\r
+\r
+import java.util.Collections;\r
+import java.util.Map;\r
+import java.util.TreeMap;\r
+\r
+import org.simantics.browsing.ui.model.modifiers.NoModifierRule;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;\r
+import org.simantics.db.common.request.UnaryRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.SelectionHints;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.diagram.content.ElementContext;\r
+import org.simantics.diagram.function.PredefinedVariables;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
+import org.simantics.modeling.ModelingUtils;\r
+import org.simantics.modeling.template2d.ontology.Template2dResource;\r
+import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;\r
+import org.simantics.scenegraph.profile.request.RuntimeProfileActiveEntryResources;\r
+import org.simantics.scl.reflection.annotations.SCLValue;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+\r
+public class All {\r
+\r
+    @SCLValue(type = "a -> b -> c")\r
+    public static Object variableTransformation(Object _graph, Object input) throws DatabaseException {\r
+\r
+        ReadGraph graph = (ReadGraph) _graph;\r
+\r
+        Resource object = ISelectionUtils.getSinglePossibleKey(input, SelectionHints.KEY_MAIN, Resource.class);\r
+        if (object == null)\r
+            return null;\r
+\r
+        Resource runtime = ISelectionUtils.getSinglePossibleKey(input, SelectionHints.KEY_VARIABLE_RESOURCE,\r
+                Resource.class);\r
+        if (runtime == null)\r
+            return null;\r
+\r
+        Variable inputVariable = graph.syncRequest(new UnaryRead<Resource, Variable>(runtime) {\r
+\r
+            @Override\r
+            public Variable perform(ReadGraph graph) throws DatabaseException {\r
+\r
+                DiagramResource dr = DiagramResource.getInstance(graph);\r
+                String diagramVariable = graph.getPossibleRelatedValue(parameter, dr.RuntimeDiagram_HasVariable);\r
+                return Variables.getVariable(graph, diagramVariable);\r
+\r
+            }\r
+\r
+        });\r
+        if (inputVariable == null)\r
+            return null;\r
+\r
+        Resource component = ModelingUtils.getPossibleElementCorrespondendence(graph, object);\r
+        if (component == null)\r
+            return null;\r
+        return inputVariable.browsePossible(graph, component);\r
+\r
+    }\r
+\r
+    @SCLValue(type = "a -> b -> c")\r
+    public static Object resourceVariableTransformation(Object _graph, Object input) throws DatabaseException {\r
+\r
+        ReadGraph graph = (ReadGraph) _graph;\r
+\r
+        Resource object = ISelectionUtils.getSinglePossibleKey(input, SelectionHints.KEY_MAIN, Resource.class);\r
+        if (object == null)\r
+            return null;\r
+        \r
+        return Variables.getVariable(graph, object);\r
+\r
+//        Resource runtime = ISelectionUtils.getSinglePossibleKey(input, SelectionHints.KEY_VARIABLE_RESOURCE,\r
+//                Resource.class);\r
+//        if (runtime == null)\r
+//            return null;\r
+//\r
+//        Variable inputVariable = graph.syncRequest(new UnaryRead<Resource, Variable>(runtime) {\r
+//\r
+//            @Override\r
+//            public Variable perform(ReadGraph graph) throws DatabaseException {\r
+//\r
+//                DiagramResource dr = DiagramResource.getInstance(graph);\r
+//                String diagramVariable = graph.getPossibleRelatedValue(parameter, dr.RuntimeDiagram_HasVariable);\r
+//                return Variables.getVariable(graph, diagramVariable);\r
+//\r
+//            }\r
+//\r
+//        });\r
+//        if (inputVariable == null)\r
+//            return null;\r
+//\r
+//        Resource component = ModelingUtils.getPossibleElementCorrespondendence(graph, object);\r
+//        if (component == null)\r
+//            return null;\r
+//        return inputVariable.browsePossible(graph, component);\r
+\r
+    }\r
+\r
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")\r
+    public static String variableReference(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {\r
+\r
+        Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);\r
+\r
+//        System.out.println("CONTEXT: " + context.getURI(graph));\r
+//        Resource runtime = ScenegraphLoaderUtils.getRuntime(graph, context);\r
+//        System.out.println("runtime: " + NameUtils.getSafeName(graph, runtime));\r
+\r
+        String path = graph.getRelatedValue(converter, TEMPLATE2D.Profiles_VariableReference_path, Bindings.STRING);\r
+        if (path == null)\r
+            return null;\r
+\r
+        Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);\r
+        PredefinedVariables vars = PredefinedVariables.getInstance();\r
+        Variable property = vars.getVariable(graph, path, converter, selection);\r
+        if (property != null)\r
+            return property.getValue(graph).toString();\r
+\r
+        return null;\r
+    }\r
+\r
+    public static Resource getTemplate(ReadGraph graph, Resource runtimeDiagram) throws DatabaseException {\r
+        Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);\r
+        DiagramResource DIA = DiagramResource.getInstance(graph);\r
+        Resource diagram = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);\r
+        if (diagram != null) {\r
+            Resource template = graph.getPossibleObject(diagram, TEMPLATE2D.HasDrawingTemplate);\r
+            if (template != null)\r
+                return template;\r
+        }\r
+        return null;\r
+       }\r
+\r
+       @SCLValue(type = "ReadGraph -> Resource -> ElementContext -> a")\r
+    public static Object flagTransform(ReadGraph graph, Resource converter, ElementContext context) throws DatabaseException {\r
+               //Layer0 L0 = Layer0.getInstance(graph);\r
+               DiagramResource DIA = DiagramResource.getInstance(graph);\r
+        Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);\r
+\r
+               Resource flag = context.element;\r
+               Resource runtimeDiagram = context.runtime;\r
+\r
+               Resource diagram = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);\r
+               if (diagram == null)\r
+                       return org.simantics.diagram.function.All.flagTransform(graph, converter, context);\r
+\r
+               Resource template = getTemplate(graph, runtimeDiagram);\r
+               if (template == null)\r
+                       return org.simantics.diagram.function.All.flagTransform(graph, converter, context);\r
+\r
+               double gridSize = DiagramGraphUtil.getGridSize(graph, diagram, 0.0);\r
+\r
+               String tableName = graph.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableBinding, Bindings.STRING);\r
+               Integer rowIndex = graph.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableRowIndex, Bindings.INTEGER);\r
+               FlagInfo flagInfo = new FlagInfo();\r
+               flagInfo.flag = flag;\r
+               if (tableName != null && tableName.length() > 0)\r
+                       flagInfo.flagTableName = tableName;\r
+               flagInfo.flagTableRowIndex = rowIndex;\r
+               \r
+               Map<String, FlagTableInfo> name2table = graph.syncRequest(new DrawingTemplateInfo(template), TransientCacheListener.<TreeMap<String, FlagTableInfo>> instance());\r
+        if (name2table == null)\r
+               name2table = Collections.emptyMap();\r
+        \r
+        if (!name2table.containsKey(flagInfo.flagTableName))\r
+               return org.simantics.diagram.function.All.flagTransform(graph, converter, context);\r
+        \r
+        double[] mat = graph.getRelatedValue(flag, DIA.HasTransform, Bindings.DOUBLE_ARRAY);\r
+               TranslateFlag com = new TranslateFlag(flagInfo, name2table, mat, gridSize);\r
+               \r
+       boolean isIOTablesActive = false;\r
+        for (Resource entry : graph.syncRequest(new RuntimeProfileActiveEntryResources(runtimeDiagram))) {\r
+               //ProfileEntry profile = graph.adapt(entry, ProfileEntry.class);\r
+               if (TEMPLATE2D.Profiles_DrawingFlagTables.equals(entry)){\r
+                       isIOTablesActive = true;\r
+                       break;\r
+               }\r
+        }\r
+\r
+        if (isIOTablesActive)\r
+               return com.perform();\r
+        return org.simantics.diagram.function.All.flagTransform(graph, converter, context);\r
+    }\r
+\r
+    @SCLValue(type = "ReadGraph -> Resource -> a -> b")\r
+    public static Object variableReferencePath(ReadGraph graph, Resource resource, Object context) throws DatabaseException {\r
+        if (context instanceof Variable) {\r
+            // Input variable is (:VariableReference)#HasDisplayValue. Need to\r
+            // go to parent to find the variable to be edited.\r
+            //System.out.println("R: " + NameUtils.getSafeName(graph, resource));\r
+            //System.out.println("V: " + ((Variable) context).getURI(graph));\r
+            Variable document = ((Variable)context).browse(graph, ".");\r
+            //System.out.println("D: " + document.getURI(graph));\r
+            String path = document.getPossiblePropertyValue(graph, "path", Bindings.STRING);\r
+            return path;\r
+        }\r
+        return null;\r
+    }\r
+\r
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
+    public static Object variableReferenceModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
+        return NoModifierRule.NO_MODIFIER;\r
+    }\r
+\r
+}
\ No newline at end of file