]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/model/Component.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / model / Component.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.ResourceArray;
25 import org.simantics.db.common.request.WriteRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.request.Read;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.modeling.ui.Activator;
30 import org.simantics.structural.stubs.StructuralResource2;
31
32 @Deprecated
33 public class Component extends PathNode implements IModifiable {
34
35     Resource type;
36
37     public Component(ReadGraph g, Resource resource) throws DatabaseException {
38         super(resource);
39         StructuralResource2 sr = StructuralResource2.getInstance(g);
40         type = g.getSingleType(resource, sr.Component);
41     }
42
43     INode type(ReadGraph g) throws DatabaseException {
44         return new ComponentType(g, type, resource);
45     }
46
47     @Override
48     public Collection<?> getChildren(ReadGraph g) throws DatabaseException {
49         Layer0 l0 = Layer0.getInstance(g);
50
51         StructuralResource2 sr = StructuralResource2.getInstance(g);
52         Resource composite = g.getPossibleObject(type, sr.IsDefinedBy);
53         ArrayList<Object> result = new ArrayList<Object>();
54         result.addAll(new Interface(type, resource, true).getChildren(g));
55         ResourceArray newPath = null;
56         if(path != null) {
57             newPath = path.appended(resource);
58         } else {
59             newPath = new ResourceArray(resource);
60         }
61         if(composite != null)
62             for(Resource component : g.getObjects(composite, l0.ConsistsOf)) {
63                 INode node = g.getPossibleAdapter(component, INode.class);
64                 if (node != null) {
65                     if(node instanceof IPathNode) {
66                         ((IPathNode)node).setPath(newPath);
67                     }
68                     result.add(node);
69                 }
70             }
71         return result;
72     }
73
74     @Override
75     public ImageDescriptor getImage(ReadGraph g) {
76         return Activator.COMPONENT_ICON;
77     }
78
79     @Override
80     public String getLabel(ReadGraph g) throws DatabaseException {
81         Layer0 l0 = Layer0.getInstance(g);
82         String name = (String)g.getPossibleRelatedValue(resource, l0.HasName);
83         if(name == null)
84             name = "";
85         return name + " : " + type(g).getLabel(g);
86     }
87
88     @Override
89     public boolean equals(Object obj) {
90         if(this == obj)
91             return true;
92         if(obj == null)
93             return false;
94         if(!getClass().equals(obj.getClass()))
95             return false;
96         Component other = (Component)obj;
97         return resource.equals(other.resource)
98         && type.equals(other.type);
99     }
100
101     @Override
102     public int hashCode() {
103         return (getClass().hashCode()*31 + type.hashCode())*31 + resource.hashCode();
104     }
105
106     @Override
107     public Modifier getModifier(final Session session, String columnId) {
108         return new Modifier() {
109
110             @Override
111             public String getValue() {
112                 Read<String> request =
113                     new Read<String>() {
114
115                     @Override
116                     public String perform(ReadGraph graph) throws DatabaseException {
117                         Layer0 b = Layer0.getInstance(graph);
118                         String name = graph.getPossibleRelatedValue(resource, b.HasName);
119                         return name != null ? name : "";
120                     }
121
122                 };
123                 try {
124                     return session.syncRequest(request);
125                 } catch (DatabaseException e) {
126                     e.printStackTrace();
127                     return "";
128                 }
129             }
130
131             @Override
132             public String isValid(String label) {
133                 return null;
134             }
135
136             @Override
137             public void modify(final String label) {
138                 session.asyncRequest(new WriteRequest() {
139                     @Override
140                     public void perform(WriteGraph graph) throws DatabaseException {
141                         Layer0 b = Layer0.getInstance(graph);
142                         graph.claimLiteral(resource, b.HasName, label);
143                     }
144                 });
145             }
146
147         };
148     }
149 }