]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
d9a191a8bef0b65d00da893f7c404a8bc588d715
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.wizards.modules;
2
3 import java.util.Collection;
4
5 import org.simantics.Simantics;
6 import org.simantics.browsing.ui.common.node.AbstractNode;
7 import org.simantics.browsing.ui.common.node.DeleteException;
8 import org.simantics.browsing.ui.common.node.IDeletableNode;
9 import org.simantics.browsing.ui.common.node.IModifiableNode;
10 import org.simantics.browsing.ui.content.Labeler.Modifier;
11 import org.simantics.browsing.ui.graph.impl.LabelModifier;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.WriteGraph;
15 import org.simantics.db.common.request.ObjectsWithType;
16 import org.simantics.db.common.request.ReadRequest;
17 import org.simantics.db.common.request.WriteRequest;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
20 import org.simantics.db.exception.ServiceException;
21 import org.simantics.db.layer0.adapter.PasteHandler;
22 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
23 import org.simantics.db.procedure.Listener;
24 import org.simantics.db.request.Read;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.modeling.ModelingResources;
27 import org.simantics.simulation.ontology.SimulationResource;
28 import org.simantics.structural.stubs.StructuralResource2;
29
30 public class ModuleComponentTypeNode extends AbstractNode<Resource> implements IDeletableNode, IModifiableNode {
31
32         
33         Listener<String> configurationNameSynchronizer;
34         private boolean disposed = false;
35         private Resource configuration;
36         
37     public ModuleComponentTypeNode(Resource resource) {
38         super(resource);
39
40         Simantics.getSession().asyncRequest(new ReadRequest() {
41                         
42                         @Override
43                         public void run(ReadGraph graph) throws DatabaseException {
44                         StructuralResource2 sr2 = StructuralResource2.getInstance(graph);
45                         configuration = graph.getPossibleObject(data, sr2.IsDefinedBy);                         
46                         }
47                 });
48         
49         // Not the best solution for name sync
50         configurationNameSynchronizer = new Listener<String>() {
51
52                 @Override
53                         public void execute(final String result) {
54                                 Simantics.getSession().asyncRequest(new WriteRequest() {
55                                         
56                                         @Override
57                                         public void perform(WriteGraph graph) throws DatabaseException {
58                                                 if(configuration != null)
59                                                         graph.claimLiteral(configuration, Layer0.getInstance(graph).HasLabel, result);
60                                         }
61                                 });
62                         }
63
64                         @Override
65                         public void exception(Throwable t) {
66                                 t.printStackTrace();
67                         }
68
69                         @Override
70                         public boolean isDisposed() {
71                                 return disposed;
72                         }
73                 };
74                 
75         Simantics.getSession().asyncRequest(new Read<String>() {
76
77                         @Override
78                         public String perform(ReadGraph graph) throws DatabaseException {
79                                 Layer0 l0 = Layer0.getInstance(graph);
80                                 return graph.getRelatedValue(data, l0.HasName);
81                         }
82                 
83         }, configurationNameSynchronizer);
84         
85     }
86
87     @Override
88     public Modifier getModifier(String columnId) {
89         Modifier modifier = null;
90         try {
91             modifier = Simantics.getSession().syncRequest(new Read<Modifier>() {
92
93                 @Override
94                 public Modifier perform(ReadGraph graph) throws ManyObjectsForFunctionalRelationException, ServiceException {
95                     ModelingResources mr = ModelingResources.getInstance(graph);
96                     Layer0 l0 = Layer0.getInstance(graph);
97                     Resource type =  graph.getPossibleObject(data, mr.SymbolToComponentType);
98
99                     LabelModifier modifier = new LabelModifier(Simantics.getSession(), type, l0.HasName) {
100                         @Override
101                         public String isValid(String label) {
102                             if (label.isEmpty())
103                                 return "Empty name not allowed";
104                             return null;
105                         }
106                     };
107
108
109                     return modifier;
110                 }
111             });
112         } catch (DatabaseException e) {
113             e.printStackTrace();
114         }
115
116         return modifier;
117     }
118
119     @Override
120     public void delete() throws DeleteException {
121         disposed = true;
122         try {
123             Simantics.getSession().syncRequest(new WriteRequest() {
124
125                 @Override
126                 public void perform(WriteGraph graph) throws DatabaseException {
127                         Layer0 l0 = Layer0.getInstance(graph);
128                     StructuralResource2 st = StructuralResource2.getInstance(graph);
129                     ModelingResources mr = ModelingResources.getInstance(graph);
130
131                     Resource type =  graph.getPossibleObject(data, mr.SymbolToComponentType);
132                     Resource model = graph.getSingleObject(type, l0.PartOf);
133                     Resource modelConfiguration = graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration);
134                     if (!graph.syncRequest(new ObjectsWithType(modelConfiguration, l0.ConsistsOf, type)).isEmpty()) {
135                         System.out.println("The module is used at the model configuration");
136                         return;
137                     }
138                     Collection<Resource> moduleTypes = graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, st.ComponentType));
139                     for(Resource r : moduleTypes) {
140                         Resource configuration = graph.getSingleObject(r, st.IsDefinedBy);
141                         if(!graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, type)).isEmpty()) {
142                             System.out.println("The module is used at another module: " + graph.getRelatedValue(r, l0.HasName));
143                             return;
144                         }
145                     }
146                     graph.deny(model, l0.ConsistsOf, type);
147                 }
148             });
149         } catch (DatabaseException e) {
150             e.printStackTrace();
151         }
152     }
153     
154     @SuppressWarnings("rawtypes")
155     @Override
156     public Object getAdapter(Class adapter) {
157         if(PasteHandler.class == adapter && configuration != null) 
158             return new DefaultPasteHandler(configuration);
159         return super.getAdapter(adapter);
160     }
161 }