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 org.eclipse.jface.resource.ImageDescriptor;
15 import org.simantics.browsing.ui.common.node.IModifiable;
16 import org.simantics.browsing.ui.content.Labeler.Modifier;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.Statement;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.common.utils.NameUtils;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.modeling.ModelingResources;
28 import org.simantics.modeling.ui.Activator;
29 import org.simantics.structural.stubs.StructuralResource2;
32 public class Parameter extends Node implements IModifiable {
36 Resource connectionType;
37 Resource connectionDirection;
40 public Resource getTerminalRelation(ReadGraph graph, Resource relation) throws DatabaseException {
41 StructuralResource2 sr = StructuralResource2.getInstance(graph);
42 for(Resource bound : graph.getObjects(relation, sr.IsBoundBy)) {
43 if(graph.isInstanceOf(bound, sr.Connection)) {
44 for(Statement terminalInverse : graph.getStatements(bound, sr.IsConnectedTo)) {
45 if(graph.isInstanceOf(terminalInverse.getObject(), sr.Component)) {
46 return graph.getInverse(terminalInverse.getPredicate());
54 public Resource getConnectionType(ReadGraph graph, Resource connectionPoint) throws DatabaseException {
55 StructuralResource2 sr = StructuralResource2.getInstance(graph);
56 ModelingResources mr = ModelingResources.getInstance(graph);
57 for(Resource bound : graph.getObjects(relation, sr.IsBoundBy)) {
58 if(graph.isInstanceOf(bound, sr.Connection)) {
59 Resource diagramConn = graph.getPossibleObject(bound, mr.ConnectionToDiagramConnection);
60 if(diagramConn != null) {
61 return graph.getPossibleObject(diagramConn, sr.HasConnectionType);
68 public Parameter(ReadGraph g, Resource resource, Resource instance, boolean isLeaf) throws DatabaseException {
70 this.instance = instance;
71 StructuralResource2 sr = StructuralResource2.getInstance(g);
72 this.relation = g.getSingleObject(resource, sr.Binds);
73 // this.connectionType = g.getPossibleObject(resource, sr.HasConnectionType);
74 this.connectionDirection = g.getPossibleObject(resource, sr.HasConnectionDirection);
76 this.connectionType = getConnectionType(g, relation);
80 public String getLabel(ReadGraph g) throws DatabaseException {
81 Layer0 b = Layer0.getInstance(g);
82 String name = g.getPossibleRelatedValue(relation, b.HasName);
84 return "Unnamed parameter";
85 if(connectionType != null) {
86 name = name + " : " + NameUtils.getSafeName(g, connectionType);
87 if(connectionDirection != null) {
88 StructuralResource2 sr = StructuralResource2.getInstance(g);
90 (sr.InputDirection.equals(connectionDirection) ? "in" :
91 sr.OutputDirection.equals(connectionDirection) ? "out" :
92 NameUtils.getSafeName(g, connectionDirection));
99 public ImageDescriptor getImage(ReadGraph g) throws DatabaseException {
100 /*StructuralResource2 sr = StructuralResource2.getInstance(g);
101 if (g.isInstanceOf(resource, sr.LiteralVariable))
102 return Activator.VARIABLE_ICON;*/
103 return Activator.OPEN_CONNECTION_ICON;
107 public boolean equals(Object obj) {
112 if(!getClass().equals(obj.getClass()))
114 Parameter other = (Parameter)obj;
115 return resource.equals(other.resource)
116 && relation.equals(other.relation)
117 && (instance == null ? other.instance==null : instance.equals(other.instance))
118 && isLeaf == other.isLeaf
119 && (connectionType == null ? other.connectionType == null : connectionType.equals(other.connectionType));
123 public int hashCode() {
124 return ((getClass().hashCode()*31 + relation.hashCode()) * 31 +
125 (instance == null ? 0 : instance.hashCode()))*31 + resource.hashCode();
129 public Modifier getModifier(final Session session, String columnId) {
130 return new Modifier() {
133 public String getValue() {
135 Layer0 b = Layer0.getInstance(session);
136 return session.syncRequest(new RelatedValueImplied<String>(relation, b.HasName));
137 } catch (DatabaseException e) {
144 public String isValid(String label) {
149 public void modify(final String label) {
150 session.asyncRequest(new WriteRequest() {
152 public void perform(WriteGraph graph) throws DatabaseException {
153 Layer0 b = Layer0.getInstance(graph);
154 graph.claimLiteral(relation, b.HasName, label);