]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/Parameter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / model / Parameter.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/Parameter.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/Parameter.java
new file mode 100644 (file)
index 0000000..0f1f1b4
--- /dev/null
@@ -0,0 +1,162 @@
+/*******************************************************************************\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.modeling.ui.modelBrowser.model;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.simantics.browsing.ui.common.node.IModifiable;\r
+import org.simantics.browsing.ui.content.Labeler.Modifier;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.primitiverequest.RelatedValueImplied;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.ui.Activator;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+@Deprecated\r
+public class Parameter extends Node implements IModifiable {\r
+\r
+    Resource instance;\r
+    Resource relation;\r
+    Resource connectionType;\r
+    Resource connectionDirection;\r
+    boolean isLeaf;\r
+\r
+    public Resource getTerminalRelation(ReadGraph graph, Resource relation) throws DatabaseException {\r
+        StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
+        for(Resource bound : graph.getObjects(relation, sr.IsBoundBy)) {\r
+            if(graph.isInstanceOf(bound, sr.Connection)) {\r
+                for(Statement terminalInverse : graph.getStatements(bound, sr.IsConnectedTo)) {\r
+                    if(graph.isInstanceOf(terminalInverse.getObject(), sr.Component)) {\r
+                        return graph.getInverse(terminalInverse.getPredicate());\r
+                    }\r
+                }\r
+            }\r
+        }\r
+        return null;\r
+    }\r
+\r
+    public Resource getConnectionType(ReadGraph graph, Resource connectionPoint) throws DatabaseException {\r
+        StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
+        ModelingResources mr = ModelingResources.getInstance(graph);\r
+        for(Resource bound : graph.getObjects(relation, sr.IsBoundBy)) {\r
+            if(graph.isInstanceOf(bound, sr.Connection)) {\r
+                Resource diagramConn = graph.getPossibleObject(bound, mr.ConnectionToDiagramConnection);\r
+                if(diagramConn != null) {\r
+                    return graph.getPossibleObject(diagramConn, sr.HasConnectionType);\r
+                }\r
+            }\r
+        }\r
+        return null;\r
+    }\r
+\r
+    public Parameter(ReadGraph g, Resource resource, Resource instance, boolean isLeaf) throws DatabaseException {\r
+        super(resource);\r
+        this.instance = instance;\r
+        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
+        this.relation = g.getSingleObject(resource, sr.Binds);\r
+//             this.connectionType = g.getPossibleObject(resource, sr.HasConnectionType);\r
+        this.connectionDirection = g.getPossibleObject(resource, sr.HasConnectionDirection);\r
+        this.isLeaf = isLeaf;\r
+        this.connectionType = getConnectionType(g, relation);\r
+    }\r
+\r
+    @Override\r
+    public String getLabel(ReadGraph g) throws DatabaseException {\r
+       Layer0 b = Layer0.getInstance(g);\r
+        String name = g.getPossibleRelatedValue(relation, b.HasName);\r
+        if(name == null)\r
+            return "Unnamed parameter";\r
+        if(connectionType != null) {\r
+            name = name + " : " + NameUtils.getSafeName(g, connectionType);\r
+            if(connectionDirection != null) {\r
+                StructuralResource2 sr = StructuralResource2.getInstance(g);\r
+                name = name + ", " +\r
+                (sr.InputDirection.equals(connectionDirection) ? "in" :\r
+                    sr.OutputDirection.equals(connectionDirection) ? "out" :\r
+                        NameUtils.getSafeName(g, connectionDirection));\r
+            }\r
+        }\r
+        return name;\r
+    }\r
+\r
+    @Override\r
+    public ImageDescriptor getImage(ReadGraph g) throws DatabaseException {\r
+        /*StructuralResource2 sr = StructuralResource2.getInstance(g);\r
+        if (g.isInstanceOf(resource, sr.LiteralVariable))\r
+            return Activator.VARIABLE_ICON;*/\r
+        return Activator.OPEN_CONNECTION_ICON;\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object obj) {\r
+        if(this == obj)\r
+            return true;\r
+        if(obj == null)\r
+            return false;\r
+        if(!getClass().equals(obj.getClass()))\r
+            return false;\r
+        Parameter other = (Parameter)obj;\r
+        return resource.equals(other.resource)\r
+        && relation.equals(other.relation)\r
+        && (instance == null ? other.instance==null : instance.equals(other.instance))\r
+        && isLeaf == other.isLeaf\r
+        && (connectionType == null ? other.connectionType == null : connectionType.equals(other.connectionType));\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return ((getClass().hashCode()*31 + relation.hashCode()) * 31  +\r
+                (instance == null ? 0 : instance.hashCode()))*31 + resource.hashCode();\r
+    }    \r
+\r
+    @Override\r
+    public Modifier getModifier(final Session session, String columnId) {\r
+        return new Modifier() {\r
+\r
+            @Override\r
+            public String getValue() {\r
+                try {\r
+                       Layer0 b = Layer0.getInstance(session);\r
+                    return session.syncRequest(new RelatedValueImplied<String>(relation, b.HasName));\r
+                } catch (DatabaseException e) {\r
+                    e.printStackTrace();\r
+                    return "";\r
+                }\r
+            }\r
+\r
+            @Override\r
+            public String isValid(String label) {\r
+                return null;\r
+            }\r
+\r
+            @Override\r
+            public void modify(final String label) {\r
+                session.asyncRequest(new WriteRequest() {\r
+                    @Override\r
+                    public void perform(WriteGraph graph) throws DatabaseException {\r
+                       Layer0 b = Layer0.getInstance(graph);\r
+                        graph.claimLiteral(relation, b.HasName, label);\r
+                    }\r
+\r
+                });\r
+            }\r
+\r
+        };\r
+    }\r
+}\r