1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.modelBrowser.model;
14 import java.util.ArrayList;
15 import java.util.Collection;
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;
33 public class Component extends PathNode implements IModifiable {
37 public Component(ReadGraph g, Resource resource) throws DatabaseException {
39 StructuralResource2 sr = StructuralResource2.getInstance(g);
40 type = g.getSingleType(resource, sr.Component);
43 INode type(ReadGraph g) throws DatabaseException {
44 return new ComponentType(g, type, resource);
48 public Collection<?> getChildren(ReadGraph g) throws DatabaseException {
49 Layer0 l0 = Layer0.getInstance(g);
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;
57 newPath = path.appended(resource);
59 newPath = new ResourceArray(resource);
62 for(Resource component : g.getObjects(composite, l0.ConsistsOf)) {
63 INode node = g.getPossibleAdapter(component, INode.class);
65 if(node instanceof IPathNode) {
66 ((IPathNode)node).setPath(newPath);
75 public ImageDescriptor getImage(ReadGraph g) {
76 return Activator.COMPONENT_ICON;
80 public String getLabel(ReadGraph g) throws DatabaseException {
81 Layer0 l0 = Layer0.getInstance(g);
82 String name = (String)g.getPossibleRelatedValue(resource, l0.HasName);
85 return name + " : " + type(g).getLabel(g);
89 public boolean equals(Object obj) {
94 if(!getClass().equals(obj.getClass()))
96 Component other = (Component)obj;
97 return resource.equals(other.resource)
98 && type.equals(other.type);
102 public int hashCode() {
103 return (getClass().hashCode()*31 + type.hashCode())*31 + resource.hashCode();
107 public Modifier getModifier(final Session session, String columnId) {
108 return new Modifier() {
111 public String getValue() {
112 Read<String> request =
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 : "";
124 return session.syncRequest(request);
125 } catch (DatabaseException e) {
132 public String isValid(String label) {
137 public void modify(final String label) {
138 session.asyncRequest(new WriteRequest() {
140 public void perform(WriteGraph graph) throws DatabaseException {
141 Layer0 b = Layer0.getInstance(graph);
142 graph.claimLiteral(resource, b.HasName, label);