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