1 package org.simantics.sysdyn.ui.wizards.modules;
\r
3 import java.util.Collection;
\r
5 import org.simantics.browsing.ui.common.node.AbstractNode;
\r
6 import org.simantics.browsing.ui.common.node.DeleteException;
\r
7 import org.simantics.browsing.ui.common.node.IDeletableNode;
\r
8 import org.simantics.browsing.ui.common.node.IModifiableNode;
\r
9 import org.simantics.browsing.ui.content.Labeler.Modifier;
\r
10 import org.simantics.browsing.ui.graph.impl.LabelModifier;
\r
11 import org.simantics.db.ReadGraph;
\r
12 import org.simantics.db.Resource;
\r
13 import org.simantics.db.WriteGraph;
\r
14 import org.simantics.db.common.request.ObjectsWithType;
\r
15 import org.simantics.db.common.request.ReadRequest;
\r
16 import org.simantics.db.common.request.WriteRequest;
\r
17 import org.simantics.db.exception.DatabaseException;
\r
18 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
\r
19 import org.simantics.db.exception.ServiceException;
\r
20 import org.simantics.db.layer0.adapter.PasteHandler;
\r
21 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
\r
22 import org.simantics.db.procedure.Listener;
\r
23 import org.simantics.db.request.Read;
\r
24 import org.simantics.layer0.Layer0;
\r
25 import org.simantics.modeling.ModelingResources;
\r
26 import org.simantics.simulation.ontology.SimulationResource;
\r
27 import org.simantics.structural.stubs.StructuralResource2;
\r
28 import org.simantics.ui.SimanticsUI;
\r
30 public class ModuleComponentTypeNode extends AbstractNode<Resource> implements IDeletableNode, IModifiableNode {
\r
33 Listener<String> configurationNameSynchronizer;
\r
34 private boolean disposed = false;
\r
35 private Resource configuration;
\r
37 public ModuleComponentTypeNode(Resource resource) {
\r
40 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
43 public void run(ReadGraph graph) throws DatabaseException {
\r
44 StructuralResource2 sr2 = StructuralResource2.getInstance(graph);
\r
45 configuration = graph.getPossibleObject(data, sr2.IsDefinedBy);
\r
49 // Not the best solution for name sync
\r
50 configurationNameSynchronizer = new Listener<String>() {
\r
53 public void execute(final String result) {
\r
54 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
57 public void perform(WriteGraph graph) throws DatabaseException {
\r
58 if(configuration != null)
\r
59 graph.claimLiteral(configuration, Layer0.getInstance(graph).HasLabel, result);
\r
65 public void exception(Throwable t) {
\r
66 t.printStackTrace();
\r
70 public boolean isDisposed() {
\r
75 SimanticsUI.getSession().asyncRequest(new Read<String>() {
\r
78 public String perform(ReadGraph graph) throws DatabaseException {
\r
79 Layer0 l0 = Layer0.getInstance(graph);
\r
80 return graph.getRelatedValue(data, l0.HasName);
\r
83 }, configurationNameSynchronizer);
\r
88 public Modifier getModifier(String columnId) {
\r
89 Modifier modifier = null;
\r
91 modifier = SimanticsUI.getSession().syncRequest(new Read<Modifier>() {
\r
94 public Modifier perform(ReadGraph graph) throws ManyObjectsForFunctionalRelationException, ServiceException {
\r
95 ModelingResources mr = ModelingResources.getInstance(graph);
\r
96 Layer0 l0 = Layer0.getInstance(graph);
\r
97 Resource type = graph.getPossibleObject(data, mr.SymbolToComponentType);
\r
99 LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), type, l0.HasName) {
\r
101 public String isValid(String label) {
\r
102 if (label.isEmpty())
\r
103 return "Empty name not allowed";
\r
112 } catch (DatabaseException e) {
\r
113 e.printStackTrace();
\r
120 public void delete() throws DeleteException {
\r
123 SimanticsUI.getSession().syncRequest(new WriteRequest() {
\r
126 public void perform(WriteGraph graph) throws DatabaseException {
\r
127 Layer0 l0 = Layer0.getInstance(graph);
\r
128 StructuralResource2 st = StructuralResource2.getInstance(graph);
\r
129 ModelingResources mr = ModelingResources.getInstance(graph);
\r
131 Resource type = graph.getPossibleObject(data, mr.SymbolToComponentType);
\r
132 Resource model = graph.getSingleObject(type, l0.PartOf);
\r
133 Resource modelConfiguration = graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration);
\r
134 if (!graph.syncRequest(new ObjectsWithType(modelConfiguration, l0.ConsistsOf, type)).isEmpty()) {
\r
135 System.out.println("The module is used at the model configuration");
\r
138 Collection<Resource> moduleTypes = graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, st.ComponentType));
\r
139 for(Resource r : moduleTypes) {
\r
140 Resource configuration = graph.getSingleObject(r, st.IsDefinedBy);
\r
141 if(!graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, type)).isEmpty()) {
\r
142 System.out.println("The module is used at another module: " + graph.getRelatedValue(r, l0.HasName));
\r
146 graph.deny(model, l0.ConsistsOf, type);
\r
149 } catch (DatabaseException e) {
\r
150 e.printStackTrace();
\r
154 @SuppressWarnings("rawtypes")
\r
156 public Object getAdapter(Class adapter) {
\r
157 if(PasteHandler.class == adapter && configuration != null)
\r
158 return new DefaultPasteHandler(configuration);
\r
159 return super.getAdapter(adapter);
\r