]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/ComponentType.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / model / ComponentType.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/ComponentType.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/ComponentType.java
new file mode 100644 (file)
index 0000000..21869f1
--- /dev/null
@@ -0,0 +1,148 @@
+/*******************************************************************************\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 java.util.ArrayList;\r
+import java.util.Collection;\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.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\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 ComponentType extends Node implements IModifiable {\r
+\r
+    final static boolean showFolders = true;\r
+\r
+    Resource content;\r
+    Resource instance;\r
+\r
+    public ComponentType(ReadGraph g, Resource resource) throws DatabaseException {\r
+        this(g, resource, null);\r
+    }\r
+\r
+    public ComponentType(ReadGraph g, Resource resource, Resource instance) throws DatabaseException {\r
+        super(resource);\r
+        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
+        this.content = g.getPossibleObject(resource, sr.IsDefinedBy);\r
+        this.instance = instance;\r
+    }\r
+\r
+    @Override\r
+    public Collection<?> getChildren(ReadGraph g) throws DatabaseException {\r
+        Collection<INode> ret = new ArrayList<INode>(3);\r
+        ret.add(new Interface(resource, instance, false));\r
+        if(content != null) {\r
+            ret.add(new Decorator(g.adapt(content, INode.class)) {\r
+                @Override\r
+                public String getLabel(ReadGraph g) {\r
+                    return "Content";\r
+                }\r
+            });\r
+        }\r
+        for(Resource symbol : g.getObjects(resource, ModelingResources.getInstance(g).ComponentTypeToSymbol))\r
+            ret.add(new Symbol(symbol));\r
+        if(!showFolders) {\r
+            ArrayList<Object> flatten = new ArrayList<Object>();\r
+            for(INode n : ret)\r
+                flatten.addAll(n.getChildren(g));\r
+            return flatten;\r
+        }\r
+        return ret;\r
+    }\r
+\r
+    @Override\r
+    public ImageDescriptor getImage(ReadGraph g) {\r
+        return Activator.COMPONENT_TYPE_ICON;\r
+    }\r
+\r
+    @Override\r
+    public String getLabel(ReadGraph g) throws DatabaseException {\r
+       Layer0 b = Layer0.getInstance(g);\r
+       return g.getPossibleRelatedValue(resource, b.HasName);\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
+        ComponentType other = (ComponentType)obj;\r
+        return resource.equals(other.resource)\r
+        && content.equals(other.content)\r
+        && (instance == null ? other.instance == null : instance.equals(other.instance));\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return ((getClass().hashCode()*31 + (instance == null ? 0 : instance.hashCode()))*31\r
+                +  content.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
+                Read<String> request =\r
+                    new Read<String>() {\r
+\r
+                    @Override\r
+                    public String perform(ReadGraph graph) throws DatabaseException {\r
+                       Layer0 b = Layer0.getInstance(graph);\r
+                        String name = graph.getPossibleRelatedValue(resource, b.HasName);\r
+                        return name != null ? name : "";\r
+                    }\r
+\r
+                };\r
+                try {\r
+                    return session.syncRequest(request);\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(resource, b.HasName, label);\r
+                    }\r
+                });\r
+            }\r
+\r
+        };\r
+    }\r
+}\r