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.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;
33 public class ComponentType extends Node implements IModifiable {
35 final static boolean showFolders = true;
40 public ComponentType(ReadGraph g, Resource resource) throws DatabaseException {
41 this(g, resource, null);
44 public ComponentType(ReadGraph g, Resource resource, Resource instance) throws DatabaseException {
46 StructuralResource2 sr = StructuralResource2.getInstance(g);
47 this.content = g.getPossibleObject(resource, sr.IsDefinedBy);
48 this.instance = instance;
52 public Collection<?> getChildren(ReadGraph g) throws DatabaseException {
53 Collection<INode> ret = new ArrayList<INode>(3);
54 ret.add(new Interface(resource, instance, false));
56 ret.add(new Decorator(g.adapt(content, INode.class)) {
58 public String getLabel(ReadGraph g) {
63 for(Resource symbol : g.getObjects(resource, ModelingResources.getInstance(g).ComponentTypeToSymbol))
64 ret.add(new Symbol(symbol));
66 ArrayList<Object> flatten = new ArrayList<Object>();
68 flatten.addAll(n.getChildren(g));
75 public ImageDescriptor getImage(ReadGraph g) {
76 return Activator.COMPONENT_TYPE_ICON;
80 public String getLabel(ReadGraph g) throws DatabaseException {
81 Layer0 b = Layer0.getInstance(g);
82 return g.getPossibleRelatedValue(resource, b.HasName);
86 public boolean equals(Object obj) {
91 if(!getClass().equals(obj.getClass()))
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));
100 public int hashCode() {
101 return ((getClass().hashCode()*31 + (instance == null ? 0 : instance.hashCode()))*31
102 + content.hashCode())*31 + resource.hashCode();
106 public Modifier getModifier(final Session session, String columnId) {
107 return new Modifier() {
110 public String getValue() {
111 Read<String> request =
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 : "";
123 return session.syncRequest(request);
124 } catch (DatabaseException e) {
131 public String isValid(String label) {
136 public void modify(final String label) {
137 session.asyncRequest(new WriteRequest() {
139 public void perform(WriteGraph graph) throws DatabaseException {
140 Layer0 b = Layer0.getInstance(graph);
141 graph.claimLiteral(resource, b.HasName, label);