]> gerrit.simantics Code Review - simantics/platform.git/blob - modelBrowser/model/Parameter.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / modelBrowser / model / Parameter.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 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;
30
31 @Deprecated
32 public class Parameter extends Node implements IModifiable {
33
34     Resource instance;
35     Resource relation;
36     Resource connectionType;
37     Resource connectionDirection;
38     boolean isLeaf;
39
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());
47                     }
48                 }
49             }
50         }
51         return null;
52     }
53
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);
62                 }
63             }
64         }
65         return null;
66     }
67
68     public Parameter(ReadGraph g, Resource resource, Resource instance, boolean isLeaf) throws DatabaseException {
69         super(resource);
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);
75         this.isLeaf = isLeaf;
76         this.connectionType = getConnectionType(g, relation);
77     }
78
79     @Override
80     public String getLabel(ReadGraph g) throws DatabaseException {
81         Layer0 b = Layer0.getInstance(g);
82         String name = g.getPossibleRelatedValue(relation, b.HasName);
83         if(name == null)
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);
89                 name = name + ", " +
90                 (sr.InputDirection.equals(connectionDirection) ? "in" :
91                     sr.OutputDirection.equals(connectionDirection) ? "out" :
92                         NameUtils.getSafeName(g, connectionDirection));
93             }
94         }
95         return name;
96     }
97
98     @Override
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;
104     }
105
106     @Override
107     public boolean equals(Object obj) {
108         if(this == obj)
109             return true;
110         if(obj == null)
111             return false;
112         if(!getClass().equals(obj.getClass()))
113             return false;
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));
120     }
121
122     @Override
123     public int hashCode() {
124         return ((getClass().hashCode()*31 + relation.hashCode()) * 31  +
125                 (instance == null ? 0 : instance.hashCode()))*31 + resource.hashCode();
126     }    
127
128     @Override
129     public Modifier getModifier(final Session session, String columnId) {
130         return new Modifier() {
131
132             @Override
133             public String getValue() {
134                 try {
135                         Layer0 b = Layer0.getInstance(session);
136                     return session.syncRequest(new RelatedValueImplied<String>(relation, b.HasName));
137                 } catch (DatabaseException e) {
138                     e.printStackTrace();
139                     return "";
140                 }
141             }
142
143             @Override
144             public String isValid(String label) {
145                 return null;
146             }
147
148             @Override
149             public void modify(final String label) {
150                 session.asyncRequest(new WriteRequest() {
151                     @Override
152                     public void perform(WriteGraph graph) throws DatabaseException {
153                         Layer0 b = Layer0.getInstance(graph);
154                         graph.claimLiteral(relation, b.HasName, label);
155                     }
156
157                 });
158             }
159
160         };
161     }
162 }