]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
3e5600aeb1f5395c20a713f7119ae23cb9383f0e
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.wizards.functions;\r
2 \r
3 import java.io.File;\r
4 \r
5 import org.eclipse.core.runtime.IStatus;\r
6 import org.eclipse.core.runtime.Path;\r
7 import org.eclipse.core.runtime.Status;\r
8 import org.eclipse.jface.layout.GridDataFactory;\r
9 import org.eclipse.jface.layout.PixelConverter;\r
10 import org.eclipse.jface.viewers.ISelection;\r
11 import org.eclipse.jface.viewers.ISelectionProvider;\r
12 import org.eclipse.jface.viewers.IStructuredSelection;\r
13 import org.eclipse.jface.wizard.WizardPage;\r
14 import org.eclipse.swt.SWT;\r
15 import org.eclipse.swt.events.ModifyEvent;\r
16 import org.eclipse.swt.events.ModifyListener;\r
17 import org.eclipse.swt.events.SelectionAdapter;\r
18 import org.eclipse.swt.events.SelectionEvent;\r
19 import org.eclipse.swt.events.SelectionListener;\r
20 import org.eclipse.swt.layout.GridData;\r
21 import org.eclipse.swt.layout.GridLayout;\r
22 import org.eclipse.swt.widgets.Button;\r
23 import org.eclipse.swt.widgets.Composite;\r
24 import org.eclipse.swt.widgets.FileDialog;\r
25 import org.eclipse.swt.widgets.Label;\r
26 import org.eclipse.swt.widgets.Shell;\r
27 import org.eclipse.swt.widgets.Text;\r
28 import org.eclipse.swt.widgets.Tree;\r
29 import org.simantics.browsing.ui.swt.AdaptableHintContext;\r
30 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;\r
31 import org.simantics.db.ReadGraph;\r
32 import org.simantics.db.Resource;\r
33 import org.simantics.db.exception.DatabaseException;\r
34 import org.simantics.db.request.Read;\r
35 import org.simantics.sysdyn.SysdynResource;\r
36 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;\r
37 import org.simantics.ui.SimanticsUI;\r
38 import org.simantics.utils.datastructures.ArrayMap;\r
39 \r
40 public class WizardFunctionsImportPage extends WizardPage{\r
41 \r
42         // dialog store id constants\r
43         private Text filePathField;\r
44 \r
45         // Keep track of the archive that we browsed to last time\r
46         // the wizard was invoked.\r
47         private static String previouslyBrowsedFile = "";\r
48 \r
49         private Button browseDirectoriesButton;\r
50 \r
51         private Shell shell;\r
52 \r
53         //private IStructuredSelection currentSelection;\r
54 \r
55         private Resource selectedModel;\r
56 \r
57         GraphExplorerComposite functionLibraryExplorer;\r
58         \r
59         private boolean selectionMade = false;\r
60         \r
61         /**\r
62          * Creates a new project creation wizard page.\r
63          * \r
64          */\r
65         public WizardFunctionsImportPage() {\r
66                 this("wizardFunctionsImportPage", null, null); //$NON-NLS-1$\r
67         }\r
68 \r
69         /**\r
70          * Create a new instance of the receiver.\r
71          * \r
72          * @param pageName\r
73          */\r
74         public WizardFunctionsImportPage(String pageName) {\r
75                 this(pageName,null, null);\r
76         }\r
77 \r
78         /**\r
79          * More (many more) parameters.\r
80          * \r
81          * @param pageName\r
82          * @param initialPath\r
83          * @param currentSelection\r
84          * @since 3.5\r
85          */\r
86         public WizardFunctionsImportPage(String pageName,String initialPath,\r
87                         IStructuredSelection currentSelection) {\r
88                 super(pageName);\r
89                 setPageComplete(false);\r
90                 //this.currentSelection = currentSelection;\r
91                 setTitle("Import Function Library");\r
92                 setDescription("Choose the Function Library file and the import location, then press Finish.");\r
93         }\r
94 \r
95         public void createControl(Composite parent) {\r
96 \r
97                 initializeDialogUnits(parent);\r
98 \r
99                 Composite workArea = new Composite(parent, SWT.NONE);\r
100                 setControl(workArea);\r
101 \r
102                 workArea.setLayout(new GridLayout());\r
103                 workArea.setLayoutData(new GridData(GridData.FILL_BOTH\r
104                                 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));\r
105 \r
106                 createProjectsRoot(workArea);\r
107 \r
108                 createTree(workArea);\r
109                 \r
110                 \r
111         }\r
112 \r
113         private void createProjectsRoot(Composite workArea) {\r
114 \r
115                 //set label for field\r
116                 Label title = new Label(workArea, SWT.NONE);\r
117                 title.setText("Select Function Library source:");\r
118 \r
119                 Composite projectGroup = new Composite(workArea, SWT.NONE);\r
120                 GridLayout layout = new GridLayout();\r
121                 layout.numColumns = 2;\r
122                 layout.makeColumnsEqualWidth = false;\r
123                 layout.marginWidth = 0;\r
124 \r
125                 projectGroup.setLayout(layout);\r
126                 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));\r
127 \r
128                 // module location entry field\r
129                 this.filePathField = new Text(projectGroup, SWT.BORDER);\r
130 \r
131                 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);\r
132                 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);\r
133                 filePathField.setLayoutData(directoryPathData);\r
134                 \r
135 \r
136                 filePathField.addModifyListener(new ModifyListener(){\r
137                         @Override\r
138                         public void modifyText(ModifyEvent e) {\r
139                                 previouslyBrowsedFile = filePathField.getText();        \r
140                         }\r
141                 });\r
142                 if (previouslyBrowsedFile != null){\r
143                         filePathField.setText(previouslyBrowsedFile);\r
144                         validatePage();\r
145                 }\r
146 \r
147                 // browse button\r
148                 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);\r
149                 browseDirectoriesButton.setText("Browse");\r
150                 setButtonLayoutData(browseDirectoriesButton);\r
151 \r
152                 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {\r
153                         /*\r
154                          * (non-Javadoc)\r
155                          * \r
156                          * @see org.eclipse.swt.events.SelectionAdapter#widgetS\r
157                          * elected(org.eclipse.swt.events.SelectionEvent)\r
158                          */\r
159                         public void widgetSelected(SelectionEvent e) {\r
160                                 handleLocationDirectoryButtonPressed();\r
161                         }\r
162                 });\r
163 \r
164         }\r
165 \r
166         private void createTree(Composite workArea){\r
167                 \r
168                 //set label for tree\r
169                 Label title = new Label(workArea, SWT.NONE);\r
170                 title.setText("Select import location:");\r
171 \r
172                 try {\r
173                         Resource input = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
174 \r
175                                 @Override\r
176                                 public Resource perform(ReadGraph graph)\r
177                                                 throws DatabaseException {\r
178                                         Resource model = SimanticsUI.getProject().get();\r
179                                         return model;\r
180                                 }\r
181 \r
182                         });\r
183 \r
184                         functionLibraryExplorer = new GraphExplorerComposite(ArrayMap.keys(\r
185                                         "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);\r
186 \r
187                         functionLibraryExplorer\r
188                         .setBrowseContexts(SysdynResource.URIs.FunctionTree);\r
189 \r
190                         functionLibraryExplorer.finish();\r
191 \r
192                         functionLibraryExplorer.setInput(null, input);\r
193 \r
194                         GridDataFactory.fillDefaults().grab(true, true).applyTo(\r
195                                         functionLibraryExplorer);\r
196                         \r
197                         ((Tree)functionLibraryExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {\r
198                                 \r
199                                 @Override\r
200                                 public void widgetSelected(SelectionEvent e) {\r
201                                         selectionMade = true;\r
202                                         validatePage();\r
203                                 }\r
204                                 @Override\r
205                                 public void widgetDefaultSelected(SelectionEvent e) {\r
206                                         selectionMade = true;\r
207                                         validatePage();\r
208                                 }\r
209                         });\r
210 \r
211                 } catch (DatabaseException e) {\r
212                         e.printStackTrace();\r
213                 }\r
214 \r
215         }\r
216 \r
217         //Set filePathField active\r
218         public void setVisible(boolean visible) {\r
219                 super.setVisible(visible);\r
220                 this.filePathField.setFocus();\r
221         }\r
222 \r
223         //Open dialog for choosing the file\r
224         protected void handleLocationDirectoryButtonPressed() {\r
225 \r
226                 shell = filePathField.getShell();\r
227 \r
228                 FileDialog dialog = new FileDialog(shell, SWT.OPEN);\r
229 \r
230                 String[] ext = {"*.tg"};\r
231                 dialog.setFilterExtensions(ext);\r
232 \r
233                 dialog.setText("Import Function Library");\r
234 \r
235                 String dirName = filePathField.getText().trim();\r
236 \r
237                 File path = new File(dirName);\r
238                 if (path.exists()) {\r
239                         dialog.setFilterPath(new Path(dirName).toOSString());   \r
240                 }\r
241 \r
242                 String selectedFile = dialog.open();\r
243                 if (selectedFile != null) {\r
244                         filePathField.setText(selectedFile);\r
245                         validatePage();\r
246                 }       \r
247         }\r
248 \r
249         //Get selection from the tree\r
250         @SuppressWarnings("unchecked")\r
251         public static <T> T getExplorerResource(GraphExplorerComposite explorer,\r
252                         Class<T> clazz) {\r
253                 \r
254                 if(explorer == null)\r
255                         return null;\r
256                 ISelection selection = ((ISelectionProvider) explorer\r
257                                 .getAdapter(ISelectionProvider.class)).getSelection();\r
258                 if (selection == null)\r
259                         return null;\r
260                 IStructuredSelection iss = (IStructuredSelection) selection;\r
261                 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();\r
262                 if (inc == null)\r
263                         return null;\r
264                 final T resource = (T) inc.getAdapter(clazz);\r
265 \r
266                 return resource;\r
267         }\r
268 \r
269         //Create project after finish is pressed.\r
270         public boolean createProjects() {\r
271                 \r
272                 selectedModel= getExplorerResource(functionLibraryExplorer, Resource.class);\r
273                 if(selectedModel == null){\r
274                         setErrorMessage("Error when retrieving resource");\r
275                         return false;\r
276                 }\r
277                         \r
278                 String selected = previouslyBrowsedFile;\r
279                 if(selected == null){\r
280                         setErrorMessage("No file selected");\r
281                         return false;\r
282                 }\r
283                 \r
284                 IStatus status = ImportUtilsUI.importFunctionLibrary(selectedModel, selected, null);\r
285 \r
286                 /*\r
287                 TransferableGraph1 tg = null;\r
288                 try {\r
289                         tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
290                 } catch (RuntimeBindingConstructionException e) {\r
291                         e.printStackTrace();\r
292                         return false;\r
293                 } catch (IOException e) {\r
294                         setErrorMessage("The imported file is not of type: Function Library");\r
295                         return false;\r
296                 } \r
297                 if(tg == null){\r
298                         setErrorMessage("The imported file is not of type: Function Library");\r
299                         return false;\r
300                 }\r
301 \r
302 \r
303                 try {\r
304                         Boolean hasSharedOntologies;\r
305                         hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
306 \r
307                                 @Override\r
308                                 public Boolean perform(ReadGraph graph) throws DatabaseException {\r
309                                         try {\r
310                                                 graph.getResource("http://SharedOntologies");\r
311                                         } catch (ResourceNotFoundException e) {\r
312                                                 return false;\r
313                                         }               \r
314                                         return true;\r
315                                 }\r
316                         });\r
317 \r
318                         if(!hasSharedOntologies) {\r
319                                 SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
320 \r
321                                         @Override\r
322                                         public void perform(WriteGraph graph) throws DatabaseException {\r
323                                                 Layer0 l0 = Layer0.getInstance(graph);\r
324                                                 GraphUtils.create2(graph, l0.Library, \r
325                                                                 l0.HasName, "SharedOntologies",\r
326                                                                 l0.PartOf, graph.getResource("http:/"));\r
327                                         }\r
328                                 });\r
329 \r
330                         }\r
331                 } catch (DatabaseException e) {\r
332                         e.printStackTrace();\r
333                         return false;\r
334                 }\r
335 \r
336 \r
337                 SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(selectedModel);\r
338                 try {\r
339                         DefaultPasteHandler.defaultExecute(tg, selectedModel, ia);\r
340                 } catch (Exception e) {\r
341                         e.printStackTrace();\r
342                 }\r
343 \r
344                 final Resource root = ia.getRoot();\r
345 \r
346                 try {\r
347                         SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
348 \r
349                                 @Override\r
350                                 public void perform(WriteGraph graph) throws DatabaseException {\r
351                                         Layer0 l0 = Layer0.getInstance(graph);\r
352                                         if(graph.isInstanceOf(root, SysdynResource.getInstance(graph).SharedFunctionOntology)) {\r
353                                                 Resource library = graph.getResource("http://SharedOntologies");\r
354                                                 if(!graph.hasStatement(library, l0.ConsistsOf, root)) {\r
355                                                         graph.claim(library, l0.ConsistsOf, root);\r
356                                                 }\r
357 \r
358                                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
359                                                 Resource model = selectedModel;\r
360                                                 while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology))\r
361                                                         model = graph.getSingleObject(model, l0.PartOf);\r
362                                                 if(graph.isInstanceOf(model, sr.SysdynModel)) {\r
363                                                         graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, root);\r
364                                                 }\r
365 \r
366                                         } else if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) {\r
367                                                 Resource instanceOf = graph.getPossibleObject(root,l0.InstanceOf);\r
368                                                 String type = "...";\r
369                                                 if(instanceOf != null)\r
370                                                         type = NameUtils.getSafeName(graph, instanceOf);\r
371                                                 else {\r
372                                                         Resource inheritedFrom = graph.getPossibleObject(root, l0.Inherits);\r
373                                                         if(inheritedFrom != null)\r
374                                                                 type = NameUtils.getSafeName(graph, inheritedFrom);\r
375                                                 } \r
376                                                 graph.deny(root, l0.PartOf);\r
377                                                 error = type;\r
378                                         }\r
379                                 }\r
380                         });\r
381                 } catch (DatabaseException e) {\r
382                         e.printStackTrace();\r
383                 }\r
384                 if (!error.isEmpty()){\r
385                         setErrorMessage("The imported file is not of type: Function Library (" + error +")");\r
386                         error = "";\r
387                         return false;\r
388                 }\r
389                  */\r
390                 if(status == null || !status.equals(Status.OK_STATUS)) {\r
391                     setErrorMessage(status.getMessage());\r
392                     return false;\r
393                 }\r
394                 return true;\r
395         }\r
396         /*\r
397 \r
398         private class SysdynFunctionLibraryImportAdvisor extends DefaultPasteImportAdvisor {\r
399 \r
400                 public SysdynFunctionLibraryImportAdvisor(Resource library) {\r
401                         super(library);\r
402                 }\r
403 \r
404                 @Override\r
405                 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {\r
406                         if(root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {\r
407                                 try {\r
408                                         library = graph.getResource("http://SharedOntologies");\r
409                                 } catch (ResourceNotFoundException e) {\r
410                                         e.printStackTrace();\r
411                                 }\r
412                         }\r
413                 }\r
414 \r
415                 @Override\r
416                 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {\r
417                         Layer0 l0 = graph.getService(Layer0.class);\r
418                         this.root = graph.newResource();\r
419                         graph.claim(library, l0.ConsistsOf, l0.PartOf, this.root);\r
420                         String name = root.name;\r
421                         String newName = nameMappings.get(name);\r
422                         graph.addLiteral(this.root, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);\r
423                         return this.root;\r
424 \r
425                 }\r
426 \r
427         }\r
428         */\r
429         \r
430         void validatePage() {\r
431 \r
432                 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){\r
433                         setPageComplete(false);\r
434                         return;\r
435                 }\r
436                 setErrorMessage(null);\r
437                 setPageComplete(true);\r
438 \r
439         }\r
440 \r
441 }