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