]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
659767910c84867983b0f40aae8f2b27fb4907e6
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.wizards.modules;\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 \r
41 public class WizardModulesImportPage extends WizardPage{\r
42         \r
43         public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH";\r
44         \r
45         // dialog store id constants\r
46         \r
47         private Text filePathField;\r
48         \r
49         // Keep track of the archive that we browsed to last time\r
50         // the wizard was invoked.\r
51         private static String previouslyBrowsedFile = "";\r
52 \r
53         private Button browseDirectoriesButton;\r
54 \r
55         private Shell shell;\r
56         \r
57         //private IStructuredSelection currentSelection;\r
58         \r
59         private Resource selectedModel;\r
60         \r
61         GraphExplorerComposite modelExplorer;\r
62         \r
63         private boolean selectionMade = false;\r
64         \r
65         /**\r
66          * Creates a new project creation wizard page.\r
67          * \r
68          */\r
69         public WizardModulesImportPage() {\r
70                 this("wizardModulesImportPage", null, null); //$NON-NLS-1$\r
71         }\r
72 \r
73         /**\r
74          * Create a new instance of the receiver.\r
75          * \r
76          * @param pageName\r
77          */\r
78         public WizardModulesImportPage(String pageName) {\r
79                 this(pageName,null, null);\r
80         }\r
81                         \r
82         /**\r
83          * More (many more) parameters.\r
84          * \r
85          * @param pageName\r
86          * @param initialPath\r
87          * @param currentSelection\r
88          * @since 3.5\r
89          */\r
90         public WizardModulesImportPage(String pageName,String initialPath,\r
91                         IStructuredSelection currentSelection) {\r
92                 super(pageName);\r
93                 setPageComplete(false);\r
94                 //this.currentSelection = currentSelection;\r
95                 setTitle("Import Module");\r
96                 setDescription("Choose the Module file and the import location, then press Finish.");\r
97         }\r
98         \r
99         public void createControl(Composite parent) {\r
100         \r
101                 initializeDialogUnits(parent);\r
102 \r
103                 Composite workArea = new Composite(parent, SWT.NONE);\r
104                 setControl(workArea);\r
105 \r
106                 workArea.setLayout(new GridLayout());\r
107                 workArea.setLayoutData(new GridData(GridData.FILL_BOTH\r
108                                 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));\r
109 \r
110                 createProjectsRoot(workArea);\r
111                 \r
112                 createTree(workArea);\r
113         }\r
114         \r
115         private void createProjectsRoot(Composite workArea) {\r
116                 \r
117                 // set label for field\r
118                 Label title = new Label(workArea, SWT.NONE);\r
119                 title.setText("Select Module source:");\r
120                 \r
121                 Composite projectGroup = new Composite(workArea, SWT.NONE);\r
122                 GridLayout layout = new GridLayout();\r
123                 layout.numColumns = 2;\r
124                 layout.makeColumnsEqualWidth = false;\r
125                 layout.marginWidth = 0;\r
126                 \r
127                 projectGroup.setLayout(layout);\r
128                 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));\r
129                 \r
130                 // module location entry field\r
131                 this.filePathField = new Text(projectGroup, SWT.BORDER);\r
132 \r
133                 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);\r
134                 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);\r
135                 filePathField.setLayoutData(directoryPathData);\r
136                 \r
137                 filePathField.addModifyListener(new ModifyListener(){\r
138             @Override\r
139             public void modifyText(ModifyEvent e) {\r
140                 previouslyBrowsedFile = filePathField.getText();        \r
141             }\r
142                 });\r
143                 if (previouslyBrowsedFile != null){\r
144                         filePathField.setText(previouslyBrowsedFile);\r
145                         validatePage();\r
146                 }\r
147                 \r
148                 // browse button\r
149                 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);\r
150                 browseDirectoriesButton.setText("Browse");\r
151                 setButtonLayoutData(browseDirectoriesButton);\r
152                 \r
153                 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {\r
154                         /*\r
155                          * (non-Javadoc)\r
156                          * \r
157                          * @see org.eclipse.swt.events.SelectionAdapter#widgetS\r
158                          * elected(org.eclipse.swt.events.SelectionEvent)\r
159                          */\r
160                         public void widgetSelected(SelectionEvent e) {\r
161                                 handleLocationDirectoryButtonPressed();\r
162                         }\r
163                 });\r
164                 \r
165         }\r
166 \r
167         private void createTree(Composite workArea){\r
168                 \r
169                 //set label for tree\r
170                 Label title = new Label(workArea, SWT.NONE);\r
171                 title.setText("Select import location:");\r
172 \r
173                 try {\r
174                         Resource input = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
175 \r
176                                 @Override\r
177                                 public Resource perform(ReadGraph graph)\r
178                                                 throws DatabaseException {\r
179                                         Resource model = SimanticsUI.getProject().get();\r
180                                         return model;\r
181                                 }\r
182 \r
183                         });\r
184 \r
185                         modelExplorer = new GraphExplorerComposite(ArrayMap.keys(\r
186                                         "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);\r
187 \r
188                         modelExplorer\r
189                         .setBrowseContexts(SysdynResource.URIs.ImportModuleTree);\r
190 \r
191                         modelExplorer.finish();\r
192 \r
193                         modelExplorer.setInput(null, input);\r
194                         \r
195                         GridDataFactory.fillDefaults().grab(true, true).applyTo(\r
196                                         modelExplorer);\r
197                         \r
198                         ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {\r
199                                 \r
200                                 @Override\r
201                                 public void widgetSelected(SelectionEvent e) {\r
202                                         selectionMade = true;\r
203                                         validatePage();\r
204                                 }\r
205                                 @Override\r
206                                 public void widgetDefaultSelected(SelectionEvent e) {\r
207                                         selectionMade = true;\r
208                                         validatePage();\r
209                                 }\r
210                         });\r
211                         \r
212 \r
213                 } catch (DatabaseException e) {\r
214                         e.printStackTrace();\r
215                 }\r
216 \r
217         }\r
218         \r
219         //Set filePathField active\r
220         public void setVisible(boolean visible) {\r
221                 super.setVisible(visible);\r
222                 this.filePathField.setFocus();\r
223         }\r
224         \r
225         //Open dialog for choosing the file\r
226         protected void handleLocationDirectoryButtonPressed() {\r
227                 \r
228                 shell = filePathField.getShell();\r
229                 \r
230                 FileDialog dialog = new FileDialog(shell, SWT.OPEN);\r
231                 \r
232                 String[] ext = {"*.tg"};\r
233                 dialog.setFilterExtensions(ext);\r
234                 \r
235                 dialog.setText("Import Module");\r
236 \r
237                 String dirName = filePathField.getText().trim();\r
238                 \r
239                 File path = new File(dirName);\r
240                 if (path.exists()) {\r
241                         dialog.setFilterPath(new Path(dirName).toOSString());   \r
242                 }\r
243                 \r
244                 String selectedFile = dialog.open();\r
245                 if (selectedFile != null) {\r
246                         filePathField.setText(selectedFile);\r
247                         validatePage();\r
248                 }               \r
249         }\r
250         \r
251         //Get selection from the tree\r
252         @SuppressWarnings("unchecked")\r
253         public static <T> T getExplorerResource(GraphExplorerComposite explorer,\r
254                         Class<T> clazz) {\r
255                 \r
256                 if(explorer == null)\r
257                         return null;\r
258                 ISelection selection = ((ISelectionProvider) explorer\r
259                                 .getAdapter(ISelectionProvider.class)).getSelection();\r
260                 if (selection == null)\r
261                         return null;\r
262                 IStructuredSelection iss = (IStructuredSelection) selection;\r
263                 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();\r
264                 if (inc == null)\r
265                         return null;\r
266                 final T resource = (T) inc.getAdapter(clazz);\r
267 \r
268                 return resource;\r
269         }\r
270         \r
271         //Create project after finish is pressed.\r
272         public boolean createProjects() {\r
273                 \r
274                 String selected = previouslyBrowsedFile;\r
275                 if(selected == null){ \r
276                         setErrorMessage("Error when retrieving resource");\r
277                         return false;\r
278                 }\r
279                 \r
280                 selectedModel= getExplorerResource(modelExplorer, Resource.class);\r
281                 if(selectedModel == null){\r
282                         setErrorMessage("No file selected");\r
283                         return false;\r
284                 }\r
285                 \r
286                 IStatus status = ImportUtilsUI.importModuleFile(selectedModel, selected, null);\r
287                 \r
288                 /*\r
289                 TransferableGraph1 tg = null;\r
290                 try {\r
291                         tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
292                 } catch (RuntimeBindingConstructionException e) {\r
293                         e.printStackTrace();\r
294                 } catch (IOException e) {\r
295                         setErrorMessage("The imported file is not of type: Module Type");\r
296                         return false;\r
297                 }\r
298                 if(tg == null){\r
299                         setErrorMessage("The imported file is not of type: Module Type");\r
300                         return false;\r
301                 }\r
302 \r
303                 \r
304                 DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(selectedModel);\r
305                 try {\r
306                         DefaultPasteHandler.defaultExecute(tg, selectedModel, ia);\r
307                 } catch (MissingDependencyException e) {\r
308                         e.printStackTrace();\r
309                 } catch (Exception e) {\r
310                         e.printStackTrace();\r
311                 }\r
312                 \r
313                 final Resource root = ia.getRoot();\r
314                 \r
315                 try {\r
316                         SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
317                                 \r
318                                 @Override\r
319                                 public void perform(WriteGraph graph) throws DatabaseException {\r
320                                         if(!graph.isInheritedFrom(root, SysdynResource.getInstance(graph).Module)) {\r
321                                                 Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);\r
322                                                 String type = "...";\r
323                                                 if(instanceOf != null)\r
324                                                         type = NameUtils.getSafeName(graph, instanceOf);\r
325                                                 else {\r
326                                                         Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);\r
327                                                         if(inheritedFrom != null)\r
328                                                                 type = NameUtils.getSafeName(graph, inheritedFrom);\r
329                                                 }\r
330                                                 graph.deny(root, Layer0.getInstance(graph).PartOf);\r
331                                                 error = type;\r
332                                         }\r
333                                 }\r
334                                 });\r
335                 } catch (DatabaseException e) {\r
336                         e.printStackTrace();\r
337                 }\r
338                 \r
339                 if (!error.isEmpty()){\r
340                         setErrorMessage("The imported file is not of type: Module Type (" + error +")");\r
341                         error = "";\r
342                         return false;\r
343                 }\r
344                 */\r
345                 if(status == null || !status.equals(Status.OK_STATUS)) {\r
346                     setErrorMessage(status.getMessage());\r
347                     return false;\r
348                 }\r
349 \r
350                 return true;\r
351         }\r
352         \r
353         void validatePage() {\r
354                 \r
355                 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){\r
356                         setPageComplete(false);\r
357                         return;\r
358                 }\r
359                 \r
360                 setErrorMessage(null);\r
361                 setPageComplete(true);\r
362                 \r
363         }\r
364         \r
365 }