]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
7b501811b68a0f6d8eeadf124dd6283fa1c4e083
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.wizards.modules;\r
2 \r
3 import java.util.Collection;\r
4 \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
29 \r
30 public class ModuleComponentTypeNode extends AbstractNode<Resource> implements IDeletableNode, IModifiableNode {\r
31 \r
32         \r
33         Listener<String> configurationNameSynchronizer;\r
34         private boolean disposed = false;\r
35         private Resource configuration;\r
36         \r
37     public ModuleComponentTypeNode(Resource resource) {\r
38         super(resource);\r
39 \r
40         SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
41                         \r
42                         @Override\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
46                         }\r
47                 });\r
48         \r
49         // Not the best solution for name sync\r
50         configurationNameSynchronizer = new Listener<String>() {\r
51 \r
52                 @Override\r
53                         public void execute(final String result) {\r
54                                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
55                                         \r
56                                         @Override\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
60                                         }\r
61                                 });\r
62                         }\r
63 \r
64                         @Override\r
65                         public void exception(Throwable t) {\r
66                                 t.printStackTrace();\r
67                         }\r
68 \r
69                         @Override\r
70                         public boolean isDisposed() {\r
71                                 return disposed;\r
72                         }\r
73                 };\r
74                 \r
75         SimanticsUI.getSession().asyncRequest(new Read<String>() {\r
76 \r
77                         @Override\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
81                         }\r
82                 \r
83         }, configurationNameSynchronizer);\r
84         \r
85     }\r
86 \r
87     @Override\r
88     public Modifier getModifier(String columnId) {\r
89         Modifier modifier = null;\r
90         try {\r
91             modifier = SimanticsUI.getSession().syncRequest(new Read<Modifier>() {\r
92 \r
93                 @Override\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
98 \r
99                     LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), type, l0.HasName) {\r
100                         @Override\r
101                         public String isValid(String label) {\r
102                             if (label.isEmpty())\r
103                                 return "Empty name not allowed";\r
104                             return null;\r
105                         }\r
106                     };\r
107 \r
108 \r
109                     return modifier;\r
110                 }\r
111             });\r
112         } catch (DatabaseException e) {\r
113             e.printStackTrace();\r
114         }\r
115 \r
116         return modifier;\r
117     }\r
118 \r
119     @Override\r
120     public void delete() throws DeleteException {\r
121         disposed = true;\r
122         try {\r
123             SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
124 \r
125                 @Override\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
130 \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
136                         return;\r
137                     }\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
143                             return;\r
144                         }\r
145                     }\r
146                     graph.deny(model, l0.ConsistsOf, type);\r
147                 }\r
148             });\r
149         } catch (DatabaseException e) {\r
150             e.printStackTrace();\r
151         }\r
152     }\r
153     \r
154     @SuppressWarnings("rawtypes")\r
155     @Override\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
160     }\r
161 }