]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.modelBrowser.model;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 \r
17 import org.eclipse.jface.resource.ImageDescriptor;\r
18 import org.simantics.browsing.ui.common.node.IModifiable;\r
19 import org.simantics.browsing.ui.content.Labeler.Modifier;\r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.Session;\r
23 import org.simantics.db.WriteGraph;\r
24 import org.simantics.db.common.request.WriteRequest;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.db.request.Read;\r
27 import org.simantics.layer0.Layer0;\r
28 import org.simantics.modeling.ModelingResources;\r
29 import org.simantics.modeling.ui.Activator;\r
30 import org.simantics.structural.stubs.StructuralResource2;\r
31 \r
32 @Deprecated\r
33 public class ComponentType extends Node implements IModifiable {\r
34 \r
35     final static boolean showFolders = true;\r
36 \r
37     Resource content;\r
38     Resource instance;\r
39 \r
40     public ComponentType(ReadGraph g, Resource resource) throws DatabaseException {\r
41         this(g, resource, null);\r
42     }\r
43 \r
44     public ComponentType(ReadGraph g, Resource resource, Resource instance) throws DatabaseException {\r
45         super(resource);\r
46         StructuralResource2 sr = StructuralResource2.getInstance(g);\r
47         this.content = g.getPossibleObject(resource, sr.IsDefinedBy);\r
48         this.instance = instance;\r
49     }\r
50 \r
51     @Override\r
52     public Collection<?> getChildren(ReadGraph g) throws DatabaseException {\r
53         Collection<INode> ret = new ArrayList<INode>(3);\r
54         ret.add(new Interface(resource, instance, false));\r
55         if(content != null) {\r
56             ret.add(new Decorator(g.adapt(content, INode.class)) {\r
57                 @Override\r
58                 public String getLabel(ReadGraph g) {\r
59                     return "Content";\r
60                 }\r
61             });\r
62         }\r
63         for(Resource symbol : g.getObjects(resource, ModelingResources.getInstance(g).ComponentTypeToSymbol))\r
64             ret.add(new Symbol(symbol));\r
65         if(!showFolders) {\r
66             ArrayList<Object> flatten = new ArrayList<Object>();\r
67             for(INode n : ret)\r
68                 flatten.addAll(n.getChildren(g));\r
69             return flatten;\r
70         }\r
71         return ret;\r
72     }\r
73 \r
74     @Override\r
75     public ImageDescriptor getImage(ReadGraph g) {\r
76         return Activator.COMPONENT_TYPE_ICON;\r
77     }\r
78 \r
79     @Override\r
80     public String getLabel(ReadGraph g) throws DatabaseException {\r
81         Layer0 b = Layer0.getInstance(g);\r
82         return g.getPossibleRelatedValue(resource, b.HasName);\r
83     }\r
84 \r
85     @Override\r
86     public boolean equals(Object obj) {\r
87         if(this == obj)\r
88             return true;\r
89         if(obj == null)\r
90             return false;\r
91         if(!getClass().equals(obj.getClass()))\r
92             return false;\r
93         ComponentType other = (ComponentType)obj;\r
94         return resource.equals(other.resource)\r
95         && content.equals(other.content)\r
96         && (instance == null ? other.instance == null : instance.equals(other.instance));\r
97     }\r
98 \r
99     @Override\r
100     public int hashCode() {\r
101         return ((getClass().hashCode()*31 + (instance == null ? 0 : instance.hashCode()))*31\r
102                 +  content.hashCode())*31 + resource.hashCode();\r
103     }\r
104 \r
105     @Override\r
106     public Modifier getModifier(final Session session, String columnId) {\r
107         return new Modifier() {\r
108 \r
109             @Override\r
110             public String getValue() {\r
111                 Read<String> request =\r
112                     new Read<String>() {\r
113 \r
114                     @Override\r
115                     public String perform(ReadGraph graph) throws DatabaseException {\r
116                         Layer0 b = Layer0.getInstance(graph);\r
117                         String name = graph.getPossibleRelatedValue(resource, b.HasName);\r
118                         return name != null ? name : "";\r
119                     }\r
120 \r
121                 };\r
122                 try {\r
123                     return session.syncRequest(request);\r
124                 } catch (DatabaseException e) {\r
125                     e.printStackTrace();\r
126                     return "";\r
127                 }\r
128             }\r
129 \r
130             @Override\r
131             public String isValid(String label) {\r
132                 return null;\r
133             }\r
134 \r
135             @Override\r
136             public void modify(final String label) {\r
137                 session.asyncRequest(new WriteRequest() {\r
138                     @Override\r
139                     public void perform(WriteGraph graph) throws DatabaseException {\r
140                         Layer0 b = Layer0.getInstance(graph);\r
141                         graph.claimLiteral(resource, b.HasName, label);\r
142                     }\r
143                 });\r
144             }\r
145 \r
146         };\r
147     }\r
148 }\r