]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
22e62b2f03b1169b787ef95cf79ab24a6f3c26cb
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.browser.nodes;\r
13 \r
14 import java.util.Collection;\r
15 \r
16 import org.eclipse.jface.dialogs.MessageDialog;\r
17 import org.eclipse.swt.SWT;\r
18 import org.eclipse.swt.widgets.Shell;\r
19 import org.eclipse.ui.PlatformUI;\r
20 import org.simantics.browsing.ui.common.node.AbstractNode;\r
21 import org.simantics.browsing.ui.common.node.DeleteException;\r
22 import org.simantics.browsing.ui.common.node.IDeletableNode;\r
23 import org.simantics.browsing.ui.common.node.IModifiableNode;\r
24 import org.simantics.browsing.ui.content.Labeler.Modifier;\r
25 import org.simantics.browsing.ui.graph.impl.LabelModifier;\r
26 import org.simantics.db.ReadGraph;\r
27 import org.simantics.db.Resource;\r
28 import org.simantics.db.WriteGraph;\r
29 import org.simantics.db.common.request.ObjectsWithType;\r
30 import org.simantics.db.common.request.ReadRequest;\r
31 import org.simantics.db.common.request.WriteRequest;\r
32 import org.simantics.db.exception.DatabaseException;\r
33 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
34 import org.simantics.db.exception.ServiceException;\r
35 import org.simantics.db.layer0.adapter.PasteHandler;\r
36 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
37 import org.simantics.db.layer0.util.RemoverUtil;\r
38 import org.simantics.db.procedure.Listener;\r
39 import org.simantics.db.request.Read;\r
40 import org.simantics.issues.ontology.IssueResource;\r
41 import org.simantics.layer0.Layer0;\r
42 import org.simantics.modeling.ModelingResources;\r
43 import org.simantics.simulation.ontology.SimulationResource;\r
44 import org.simantics.structural.stubs.StructuralResource2;\r
45 import org.simantics.sysdyn.ui.utils.ModuleTypeNameValidator;\r
46 import org.simantics.ui.SimanticsUI;\r
47 \r
48 public class ModuleTypeNode extends AbstractNode<Resource> implements IDeletableNode, IModifiableNode {\r
49 \r
50         \r
51         Listener<String> configurationNameSynchronizer;\r
52         private boolean disposed = false;\r
53         private Resource configuration;\r
54         \r
55     public ModuleTypeNode(Resource resource) {\r
56         super(resource);\r
57 \r
58         SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
59                         \r
60                         @Override\r
61                         public void run(ReadGraph graph) throws DatabaseException {\r
62                         ModelingResources mr = ModelingResources.getInstance(graph);\r
63                         StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
64                         Resource type =  graph.getPossibleObject(data, mr.SymbolToComponentType);\r
65                         configuration = graph.getPossibleObject(type, sr2.IsDefinedBy);                         \r
66                         }\r
67                 });\r
68         \r
69         // Not the best solution for name sync\r
70         configurationNameSynchronizer = new Listener<String>() {\r
71 \r
72                 @Override\r
73                         public void execute(final String result) {\r
74                     if(result == null)\r
75                         return; \r
76                     \r
77                                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
78                                         \r
79                                         @Override\r
80                                         public void perform(WriteGraph graph) throws DatabaseException {\r
81                                                 if(configuration != null)\r
82                                                         graph.claimLiteral(configuration, Layer0.getInstance(graph).HasLabel, result);\r
83                                         }\r
84                                 });\r
85                         }\r
86 \r
87                         @Override\r
88                         public void exception(Throwable t) {\r
89                                 t.printStackTrace();\r
90                         }\r
91 \r
92                         @Override\r
93                         public boolean isDisposed() {\r
94                                 return disposed;\r
95                         }\r
96                 };\r
97                 \r
98         SimanticsUI.getSession().asyncRequest(new Read<String>() {\r
99 \r
100                         @Override\r
101                         public String perform(ReadGraph graph) throws DatabaseException {\r
102                                 Layer0 l0 = Layer0.getInstance(graph);\r
103                 ModelingResources mr = ModelingResources.getInstance(graph);\r
104                 Resource type =  graph.getPossibleObject(data, mr.SymbolToComponentType);\r
105                                 return (String) (type != null ? graph.getRelatedValue(type, l0.HasName) : null);\r
106                         }\r
107                 \r
108         }, configurationNameSynchronizer);\r
109         \r
110     }\r
111 \r
112     @Override\r
113     public Modifier getModifier(String columnId) {\r
114         Modifier modifier = null;\r
115         try {\r
116             modifier = SimanticsUI.getSession().syncRequest(new Read<Modifier>() {\r
117 \r
118                 @Override\r
119                 public Modifier perform(ReadGraph graph) throws ManyObjectsForFunctionalRelationException, ServiceException {\r
120                     ModelingResources mr = ModelingResources.getInstance(graph);\r
121                     Layer0 l0 = Layer0.getInstance(graph);\r
122                     Resource type =  graph.getPossibleObject(data, mr.SymbolToComponentType);\r
123 \r
124                     LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), type, l0.HasName) {\r
125                         @Override\r
126                         public String isValid(String label) {\r
127                             if (!new ModuleTypeNameValidator().isValid(data, label))\r
128                                 return "Not valid";\r
129                             else\r
130                                 return null;\r
131                         }\r
132                     };\r
133 \r
134 \r
135                     return modifier;\r
136                 }\r
137             });\r
138         } catch (DatabaseException e) {\r
139             e.printStackTrace();\r
140         }\r
141 \r
142         return modifier;\r
143     }\r
144 \r
145     @Override\r
146     public void delete() throws DeleteException {\r
147         disposed = true;\r
148         try {\r
149             SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
150 \r
151                 @Override\r
152                 public void perform(WriteGraph graph) throws DatabaseException{\r
153                         Layer0 l0 = Layer0.getInstance(graph);\r
154                     StructuralResource2 st = StructuralResource2.getInstance(graph);\r
155                     ModelingResources mr = ModelingResources.getInstance(graph);\r
156 \r
157                     Resource type =  graph.getPossibleObject(data, mr.SymbolToComponentType);\r
158                     Resource model = graph.getSingleObject(type, l0.PartOf);\r
159                     Resource modelConfiguration = graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration);\r
160                     if (!graph.syncRequest(new ObjectsWithType(modelConfiguration, l0.ConsistsOf, type)).isEmpty()) {\r
161                         throw new ModuleDeleteException("The module is used at the model configuration");\r
162                     }\r
163                     Collection<Resource> moduleTypes = graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, st.ComponentType));\r
164                     for(Resource r : moduleTypes) {\r
165                         Resource configuration = graph.getSingleObject(r, st.IsDefinedBy);\r
166                         if(!graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, type)).isEmpty()) {\r
167                             throw new ModuleDeleteException("The module is used at another module: " + graph.getRelatedValue(r, l0.HasName));\r
168                         }\r
169                     }\r
170                     \r
171                     IssueResource ISSUE = IssueResource.getInstance(graph);\r
172                     // Remove issues\r
173                     for(Resource issueSource : graph.syncRequest(new ObjectsWithType(type, l0.ConsistsOf, ISSUE.Sources_DependencyTracker))) {\r
174                         for(Resource issue : graph.syncRequest(new ObjectsWithType(issueSource, ISSUE.IssueSource_Manages, ISSUE.Issue))) {\r
175                             RemoverUtil.remove(graph, issue);\r
176                         }\r
177                     }\r
178                     RemoverUtil.remove(graph, type);\r
179                 }\r
180             });\r
181         } catch (ModuleDeleteException e) {\r
182             Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
183             MessageDialog dialog = new MessageDialog(shell, "Unable to delete", null, e.message, SWT.ERROR,\r
184                     new String[] { "OK" }, 0);\r
185             dialog.create();\r
186             dialog.open();\r
187         } catch (DatabaseException e) {\r
188             e.printStackTrace();\r
189         }\r
190     }\r
191     \r
192     private class ModuleDeleteException extends DatabaseException {\r
193         private static final long serialVersionUID = 4076002781765246919L;\r
194         String message;\r
195         \r
196         public ModuleDeleteException(String message) {\r
197             this.message = message;\r
198         }\r
199     }\r
200     \r
201     @SuppressWarnings("rawtypes")\r
202     @Override\r
203     public Object getAdapter(Class adapter) {\r
204         if(PasteHandler.class == adapter && configuration != null) \r
205             return new DefaultPasteHandler(configuration);\r
206         return super.getAdapter(adapter);\r
207     }\r
208 }\r