]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
3625dd9d770aadcff597cd59a1266f3c45b0bff5
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.wizards.modules;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.util.ArrayList;\r
6 import java.util.Collection;\r
7 \r
8 import org.eclipse.core.runtime.Path;\r
9 import org.eclipse.jface.layout.GridDataFactory;\r
10 import org.eclipse.jface.layout.PixelConverter;\r
11 import org.eclipse.jface.viewers.ISelection;\r
12 import org.eclipse.jface.viewers.ISelectionProvider;\r
13 import org.eclipse.jface.viewers.IStructuredSelection;\r
14 import org.eclipse.jface.wizard.WizardPage;\r
15 import org.eclipse.swt.SWT;\r
16 import org.eclipse.swt.events.ModifyEvent;\r
17 import org.eclipse.swt.events.ModifyListener;\r
18 import org.eclipse.swt.events.SelectionAdapter;\r
19 import org.eclipse.swt.events.SelectionEvent;\r
20 import org.eclipse.swt.events.SelectionListener;\r
21 import org.eclipse.swt.layout.GridData;\r
22 import org.eclipse.swt.layout.GridLayout;\r
23 import org.eclipse.swt.widgets.Button;\r
24 import org.eclipse.swt.widgets.Composite;\r
25 import org.eclipse.swt.widgets.FileDialog;\r
26 import org.eclipse.swt.widgets.Label;\r
27 import org.eclipse.swt.widgets.Shell;\r
28 import org.eclipse.swt.widgets.Text;\r
29 import org.eclipse.swt.widgets.Tree;\r
30 import org.simantics.browsing.ui.swt.AdaptableHintContext;\r
31 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;\r
32 import org.simantics.databoard.Bindings;\r
33 import org.simantics.databoard.Files;\r
34 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
35 import org.simantics.db.ReadGraph;\r
36 import org.simantics.db.Resource;\r
37 import org.simantics.db.WriteGraph;\r
38 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
39 import org.simantics.db.common.request.ObjectsWithType;\r
40 import org.simantics.db.common.request.ReadRequest;\r
41 import org.simantics.db.common.request.WriteRequest;\r
42 import org.simantics.db.common.utils.NameUtils;\r
43 import org.simantics.db.exception.DatabaseException;\r
44 import org.simantics.db.layer0.util.TransferableGraphRequest2;\r
45 import org.simantics.db.request.Read;\r
46 import org.simantics.graph.representation.TransferableGraph1;\r
47 import org.simantics.layer0.Layer0;\r
48 import org.simantics.modeling.ModelingResources;\r
49 import org.simantics.structural.stubs.StructuralResource2;\r
50 import org.simantics.sysdyn.SysdynResource;\r
51 import org.simantics.ui.SimanticsUI;\r
52 import org.simantics.utils.datastructures.ArrayMap;\r
53 import org.simantics.utils.datastructures.Pair;\r
54 \r
55 public class WizardModulesExportPage extends WizardPage {\r
56         \r
57         // dialog store id constants\r
58         private Text filePathField;\r
59         \r
60         // Keep track of the archive that we browsed to last time\r
61         // the wizard was invoked.\r
62         private static String previouslyBrowsedFile = "";\r
63 \r
64         private Button browseDirectoriesButton;\r
65         \r
66         private Resource selectedModule;\r
67         \r
68         GraphExplorerComposite modelExplorer;\r
69 \r
70         private boolean selectionMade = false;\r
71         \r
72         /**\r
73          * Creates a new project creation wizard page.\r
74          * \r
75          */\r
76         public WizardModulesExportPage() {\r
77                 this("wizardModulesExportPage", null, null); //$NON-NLS-1$\r
78         }\r
79 \r
80         /**\r
81          * Create a new instance of the receiver.\r
82          * \r
83          * @param pageName\r
84          */\r
85         public WizardModulesExportPage(String pageName) {\r
86                 this(pageName,null, null);\r
87         }\r
88                         \r
89         /**\r
90          * More (many more) parameters.\r
91          * \r
92          * @param pageName\r
93          * @param initialPath\r
94          * @param currentSelection\r
95          * @since 3.5\r
96          */\r
97         public WizardModulesExportPage(String pageName,String initialPath,\r
98                         IStructuredSelection currentSelection) {\r
99                 super(pageName);\r
100                 //this.currentSelection = currentSelection;\r
101                 setPageComplete(false);\r
102                 setTitle("Export Module");\r
103                 setDescription("Choose the Module and the export location, then press Finish.");\r
104         }\r
105         \r
106         public void createControl(Composite parent) {\r
107                 \r
108                 initializeDialogUnits(parent);\r
109 \r
110                 Composite workArea = new Composite(parent, SWT.NONE);\r
111                 setControl(workArea);\r
112 \r
113                 workArea.setLayout(new GridLayout());\r
114                 workArea.setLayoutData(new GridData(GridData.FILL_BOTH\r
115                                 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));\r
116 \r
117                 createProjectsRoot(workArea);\r
118                 createTree(workArea);\r
119         }\r
120         \r
121         private void createProjectsRoot(Composite workArea) {\r
122                 \r
123                 // set label for field\r
124                 Label title = new Label(workArea, SWT.NONE);\r
125                 title.setText("Select the export location for Module:");\r
126                 \r
127                 Composite projectGroup = new Composite(workArea, SWT.NONE);\r
128                 GridLayout layout = new GridLayout();\r
129                 layout.numColumns = 2;\r
130                 layout.makeColumnsEqualWidth = false;\r
131                 layout.marginWidth = 0;\r
132 \r
133                 projectGroup.setLayout(layout);\r
134                 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));\r
135                 \r
136                 // module location entry field\r
137                 this.filePathField = new Text(projectGroup, SWT.BORDER);\r
138 \r
139                 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);\r
140                 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);\r
141                 filePathField.setLayoutData(directoryPathData);\r
142                 \r
143                 filePathField.addModifyListener(new ModifyListener(){\r
144             @Override\r
145             public void modifyText(ModifyEvent e) {\r
146                 previouslyBrowsedFile = filePathField.getText();        \r
147             }\r
148                 });\r
149                 if (previouslyBrowsedFile != null){\r
150                         filePathField.setText(previouslyBrowsedFile);\r
151                         validatePage();\r
152                 }\r
153                 \r
154                 // browse button\r
155                 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);\r
156                 browseDirectoriesButton.setText("Browse");\r
157                 setButtonLayoutData(browseDirectoriesButton);\r
158                 \r
159                 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {\r
160                         /*\r
161                          * (non-Javadoc)\r
162                          * \r
163                          * @see org.eclipse.swt.events.SelectionAdapter#widgetS\r
164                          * elected(org.eclipse.swt.events.SelectionEvent)\r
165                          */\r
166                         public void widgetSelected(SelectionEvent e) {\r
167                                 handleLocationDirectoryButtonPressed();\r
168                         }\r
169                 });\r
170                 \r
171         }\r
172 \r
173         private void createTree(Composite workArea){\r
174                 \r
175                 //set label for tree\r
176                 Label title = new Label(workArea, SWT.NONE);\r
177                 title.setText("Select Module to export:");\r
178 \r
179                 try {\r
180                         Resource input = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
181 \r
182                                 @Override\r
183                                 public Resource perform(ReadGraph graph)\r
184                                                 throws DatabaseException {\r
185                                         Resource model = SimanticsUI.getProject().get();\r
186                                         return model;\r
187                                 }\r
188 \r
189                         });\r
190 \r
191                         modelExplorer = new GraphExplorerComposite(ArrayMap.keys(\r
192                                         "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);\r
193 \r
194                         modelExplorer\r
195                         .setBrowseContexts(SysdynResource.URIs.ExportModuleTree);\r
196 \r
197                         modelExplorer.finish();\r
198                         \r
199                         modelExplorer.setInput(null, input);\r
200 \r
201                         GridDataFactory.fillDefaults().grab(true, true).applyTo(\r
202                                         modelExplorer);\r
203                         \r
204                         ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {\r
205                                 \r
206                                 @Override\r
207                                 public void widgetSelected(SelectionEvent e) {\r
208                                         setMessage(null);\r
209                                         selectionMade = true;\r
210                                         validatePage();\r
211                                 }\r
212                                 @Override\r
213                                 public void widgetDefaultSelected(SelectionEvent e) {\r
214                                         setMessage(null);\r
215                                         selectionMade = true;\r
216                                         validatePage();\r
217                                 }\r
218                         });\r
219 \r
220                 } catch (DatabaseException e) {\r
221                         e.printStackTrace();\r
222                 }\r
223 \r
224         }\r
225     \r
226         //Set filePathField active\r
227         public void setVisible(boolean visible) {\r
228                 super.setVisible(visible);\r
229                 this.filePathField.setFocus();\r
230         }\r
231         \r
232         //Open dialog for choosing the file\r
233         protected void handleLocationDirectoryButtonPressed() {\r
234                 \r
235                 final Shell shell = filePathField.getShell();\r
236                 \r
237                 FileDialog dialog = new FileDialog(shell, SWT.SAVE);\r
238                 \r
239                 String[] ext = {"*.sysdynModule"};\r
240                 dialog.setFilterExtensions(ext);\r
241                 \r
242                 dialog.setText("Export Module");\r
243 \r
244                 String dirName = filePathField.getText().trim();\r
245                 \r
246                 File path = new File(dirName);\r
247                 if (path.exists()) {\r
248                         dialog.setFilterPath(new Path(dirName).toOSString());   \r
249                 }\r
250                 \r
251                 String selectedFile = dialog.open();\r
252                 if (selectedFile != null) {\r
253                         filePathField.setText(selectedFile);\r
254                         validatePage();\r
255                 }               \r
256 \r
257         }\r
258         \r
259         //Get selection from the tree\r
260         @SuppressWarnings("unchecked")\r
261         public static <T> T getExplorerResource(GraphExplorerComposite explorer,\r
262                         Class<T> clazz) {\r
263                 \r
264                 if(explorer == null)\r
265                         return null;\r
266                 ISelection selection = ((ISelectionProvider) explorer\r
267                                 .getAdapter(ISelectionProvider.class)).getSelection();\r
268                 if (selection == null)\r
269                         return null;\r
270                 IStructuredSelection iss = (IStructuredSelection) selection;\r
271                 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();\r
272                 if (inc == null)\r
273                         return null;\r
274                 final T resource = (T) inc.getAdapter(clazz);\r
275 \r
276                 return resource;\r
277         }\r
278         \r
279         //Create export file when finish is pressed\r
280         public boolean createProjects() {\r
281                 \r
282                 final String selected = previouslyBrowsedFile;\r
283                 if(selected == null) return false;\r
284 \r
285                 selectedModule= getExplorerResource(modelExplorer, Resource.class);\r
286                 if(selectedModule == null)\r
287                         return false;\r
288                 \r
289                 String name = null;\r
290                 try {\r
291                         name = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
292 \r
293                                 @Override\r
294                                 public String perform(ReadGraph graph) throws DatabaseException {\r
295                                         StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
296                                         SysdynResource sr = SysdynResource.getInstance(graph);\r
297                                         Layer0 l0 = Layer0.getInstance(graph);\r
298                                         \r
299                                         Resource component = selectedModule;\r
300                                         if (component == null || !graph.hasStatement(component, Layer0.getInstance(graph).PartOf))\r
301                                                 return null;\r
302                                         \r
303                                         Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy);\r
304                                         if (configuration == null)\r
305                                                 return null;\r
306                                         \r
307                                         ArrayList<String> dependencies = null;\r
308                                         for(Resource r : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Module))) {\r
309                                                 if(dependencies == null)\r
310                                                         dependencies = new ArrayList<String>();\r
311                                                 String name = NameUtils.getSafeName(graph, r);\r
312                                                 String instanceOf = NameUtils.getSafeName(graph, graph.getSingleObject(r, l0.InstanceOf));\r
313                                                 dependencies.add(name + " : " + instanceOf);\r
314                                         }\r
315                                         if(dependencies != null && !dependencies.isEmpty())\r
316                                                 throw new ContainsDependenciesException(dependencies);\r
317                                         \r
318                                         String name = graph.getPossibleRelatedValue(component, l0.HasName, Bindings.STRING);\r
319                                         return name;\r
320                                         \r
321                                 }\r
322                                 \r
323                         });\r
324                 } \r
325                 catch (DatabaseException e1) {\r
326                         e1.printStackTrace();\r
327                 }\r
328                 if(name == null) return false;\r
329                 \r
330                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
331                         \r
332                         @Override\r
333                         public void run(ReadGraph graph) throws DatabaseException {\r
334                                 Layer0 l0 = Layer0.getInstance(graph);\r
335                                 \r
336                                 final Resource component = selectedModule;\r
337                                 //final Resource component = graph.getPossibleObject(modulesymbol, mr.SymbolToComponentType);\r
338                                 if (component == null || !graph.hasStatement(component, Layer0.getInstance(graph).PartOf))\r
339                                         return;\r
340                                 String name = graph.syncRequest(new PossibleRelatedValue<String>(component, l0.HasName, Bindings.STRING ));\r
341                                 final ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();\r
342                                 roots.add(Pair.make(component, name));\r
343 \r
344                                 graph.asyncRequest(new WriteRequest() {\r
345 \r
346                                         @Override\r
347                                         public void perform(WriteGraph graph) throws DatabaseException {\r
348                                                 Layer0 l0 = Layer0.getInstance(graph);\r
349                                                 StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
350                                                 ModelingResources mr = ModelingResources.getInstance(graph);\r
351                                                 final Resource modulesymbol = graph.getPossibleObject(component, mr.ComponentTypeToSymbol);\r
352                                                 Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy);\r
353                                                 if (!graph.hasStatement(configuration, l0.PartOf, component)&& \r
354                                                                 !graph.hasStatement(modulesymbol, l0.PartOf, component)) {\r
355                                                         // Make sure that configuration and symbol are included.\r
356                                                         // In old versions, they were attached to model, not to module.\r
357                                                         Resource previousPartof = graph.getSingleObject(configuration, l0.PartOf);\r
358 \r
359                                                         graph.deny(configuration, l0.PartOf);\r
360                                                         graph.deny(modulesymbol, l0.PartOf);\r
361                                                         graph.claim(configuration, l0.PartOf, l0.ConsistsOf, component);\r
362                                                         graph.claim(modulesymbol, l0.PartOf, l0.ConsistsOf, component);\r
363 \r
364                                                         export(graph, selected, roots, component);\r
365 \r
366                                                         graph.deny(configuration, l0.PartOf);\r
367                                                         graph.deny(modulesymbol, l0.PartOf);\r
368                                                         graph.claim(configuration, l0.PartOf, l0.ConsistsOf, previousPartof);\r
369                                                         graph.claim(modulesymbol, l0.PartOf, l0.ConsistsOf, previousPartof);\r
370                                                 } else {\r
371                                                         // Normal export\r
372                                                         export(graph, selected, roots, component);\r
373                                                 }\r
374                                         }\r
375                                 });\r
376 \r
377                         }\r
378                 });\r
379 \r
380                 return true;\r
381         }\r
382         \r
383         private void export(WriteGraph graph, String path, ArrayList<Pair<Resource, String>> roots, Resource component) {\r
384                 \r
385                 // FIXME: Enumeration replacement handling like this is not suitable.\r
386                 try {\r
387                         Layer0 l0 = Layer0.getInstance(graph);\r
388                         SysdynResource sr = SysdynResource.getInstance(graph);\r
389                         StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
390 \r
391                         Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy);\r
392                         ArrayList<Pair<Resource, Resource>> replacements = new ArrayList<Pair<Resource, Resource>>();\r
393 \r
394                         for(Resource enumeration : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Enumeration))) {\r
395                                 if(graph.hasStatement(enumeration, sr.Redeclaration_replacedEnumeration_Inverse)) {\r
396                                         for(Resource replacement : graph.getObjects(enumeration, sr.Redeclaration_replacedEnumeration_Inverse)) {\r
397                                                 replacements.add(new Pair<Resource, Resource>(enumeration, replacement));\r
398                                         }\r
399                                 }\r
400                         }\r
401                         \r
402                         for(Pair<Resource,Resource> replacement : replacements)\r
403                                 graph.deny(replacement.first, sr.Redeclaration_replacedEnumeration_Inverse, replacement.second);\r
404                         \r
405                         TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, component));\r
406                         Files.createFile(new File(path), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
407                         \r
408                         for(Pair<Resource,Resource> replacement : replacements)\r
409                                 graph.claim(replacement.first, sr.Redeclaration_replacedEnumeration_Inverse, replacement.second);\r
410 \r
411                 } catch (RuntimeBindingConstructionException e) {\r
412                         e.printStackTrace();\r
413                 } catch (IOException e) {\r
414                         e.printStackTrace();\r
415                 } catch (DatabaseException e) {\r
416                         e.printStackTrace();\r
417                 }\r
418         }\r
419         \r
420         class ContainsDependenciesException extends DatabaseException {\r
421                 private static final long serialVersionUID = -1533706136673146020L;\r
422                 \r
423                 private Collection<String> dependencies;\r
424                 \r
425                 ContainsDependenciesException(Collection<String> dependencies) {\r
426                         this.dependencies = dependencies;\r
427                 }\r
428                 \r
429                 public Collection<String> getDependencies() {\r
430                         return this.dependencies;\r
431                 }\r
432                 \r
433         }\r
434         \r
435         void validatePage() {\r
436                 \r
437                 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){\r
438                         setPageComplete(false);\r
439                         return;\r
440                 }\r
441                 \r
442                 if (modelExplorer != null){\r
443                         final Resource selectedResource = getExplorerResource(modelExplorer, Resource.class);\r
444 \r
445                         String root = null;\r
446                         try {\r
447                                 root = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
448 \r
449                                         @Override\r
450                                         public String perform(ReadGraph graph) throws DatabaseException {\r
451                                                 Layer0 l0 = Layer0.getInstance(graph);\r
452                                                 Resource model = graph.getPossibleObject(selectedResource, l0.PartOf);\r
453                                                 String rootName = NameUtils.getSafeName(graph, model);\r
454 \r
455                                                 return rootName;\r
456                                         }\r
457 \r
458                                 });\r
459                                 if (root != null && root.equalsIgnoreCase("Development Project")){\r
460                                         setPageComplete(false);\r
461                                         setMessage("Select Module under the Model.");\r
462                                         return;\r
463                                 }\r
464                         } catch (DatabaseException e) {\r
465                                 e.printStackTrace();\r
466                         }\r
467                 }\r
468                 \r
469                 setPageComplete(true);\r
470                 \r
471         }\r
472         \r
473 \r
474 \r
475 }