]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/Component.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / model / Component.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/Component.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/Component.java
new file mode 100644 (file)
index 0000000..eca555e
--- /dev/null
@@ -0,0 +1,149 @@
+/*******************************************************************************\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.ResourceArray;\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.ui.Activator;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+@Deprecated\r
+public class Component extends PathNode implements IModifiable {\r
+\r
+    Resource type;\r
+\r
+    public Component(ReadGraph g, Resource resource) throws DatabaseException {\r
+        super(resource);\r
+        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
+        type = g.getSingleType(resource, sr.Component);\r
+    }\r
+\r
+    INode type(ReadGraph g) throws DatabaseException {\r
+        return new ComponentType(g, type, resource);\r
+    }\r
+\r
+    @Override\r
+    public Collection<?> getChildren(ReadGraph g) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(g);\r
+\r
+        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
+        Resource composite = g.getPossibleObject(type, sr.IsDefinedBy);\r
+        ArrayList<Object> result = new ArrayList<Object>();\r
+        result.addAll(new Interface(type, resource, true).getChildren(g));\r
+        ResourceArray newPath = null;\r
+        if(path != null) {\r
+            newPath = path.appended(resource);\r
+        } else {\r
+            newPath = new ResourceArray(resource);\r
+        }\r
+        if(composite != null)\r
+            for(Resource component : g.getObjects(composite, l0.ConsistsOf)) {\r
+                INode node = g.getPossibleAdapter(component, INode.class);\r
+                if (node != null) {\r
+                    if(node instanceof IPathNode) {\r
+                        ((IPathNode)node).setPath(newPath);\r
+                    }\r
+                    result.add(node);\r
+                }\r
+            }\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public ImageDescriptor getImage(ReadGraph g) {\r
+        return Activator.COMPONENT_ICON;\r
+    }\r
+\r
+    @Override\r
+    public String getLabel(ReadGraph g) throws DatabaseException {\r
+        Layer0 l0 = Layer0.getInstance(g);\r
+        String name = (String)g.getPossibleRelatedValue(resource, l0.HasName);\r
+        if(name == null)\r
+            name = "";\r
+        return name + " : " + type(g).getLabel(g);\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
+        Component other = (Component)obj;\r
+        return resource.equals(other.resource)\r
+        && type.equals(other.type);\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return (getClass().hashCode()*31 + type.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