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