]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
af6c0b215c40340bd481aca2ca5b4396adb3b6ea
[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.ui.SimanticsUI;\r
45 import org.simantics.utils.datastructures.ArrayMap;\r
46 import org.simantics.utils.datastructures.Pair;\r
47 \r
48 public class WizardFunctionsExportPage extends WizardPage {\r
49         \r
50         // dialog store id constants\r
51         private Text filePathField;\r
52         \r
53         // Keep track of the archive that we browsed to last time\r
54         // the wizard was invoked.\r
55         private static String previouslyBrowsedFile = "";\r
56 \r
57         private Button browseDirectoriesButton;\r
58         \r
59         //private IStructuredSelection currentSelection;\r
60         \r
61         GraphExplorerComposite functionLibraryExplorer;\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 WizardFunctionsExportPage() {\r
70                 this("wizardFunctionsExportPage", 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 WizardFunctionsExportPage(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 WizardFunctionsExportPage(String pageName,String initialPath,\r
91                         IStructuredSelection currentSelection) {\r
92                 super(pageName);\r
93                 //this.currentSelection = currentSelection;\r
94                 setPageComplete(false);\r
95                 setTitle("Export Function Library");\r
96                 setDescription("Choose the Function Library and the export 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                 createTree(workArea);\r
112                 \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 the export location for Function Library:");\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                 // function library 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 Function Library to export:");\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                         functionLibraryExplorer = new GraphExplorerComposite(ArrayMap.keys(\r
186                                         "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);\r
187 \r
188                         functionLibraryExplorer\r
189                         .setBrowseContexts("http://www.simantics.org/Sysdyn-1.0/FunctionTree");\r
190 \r
191                         functionLibraryExplorer.finish();\r
192 \r
193                         functionLibraryExplorer.setInput(null, input);\r
194 \r
195                         GridDataFactory.fillDefaults().grab(true, true).applyTo(\r
196                                         functionLibraryExplorer);\r
197                         \r
198                         ((Tree)functionLibraryExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {\r
199                                 \r
200                                 @Override\r
201                                 public void widgetSelected(SelectionEvent e) {\r
202                                         setMessage(null);\r
203                                         selectionMade = true;\r
204                                         validatePage();\r
205                                 }\r
206                                 @Override\r
207                                 public void widgetDefaultSelected(SelectionEvent e) {\r
208                                         setMessage(null);\r
209                                         selectionMade = true;\r
210                                         validatePage();\r
211                                 }\r
212                         });\r
213 \r
214                 } catch (DatabaseException e) {\r
215                         e.printStackTrace();\r
216                 }\r
217 \r
218 \r
219         }\r
220     \r
221         //Set filePathField active\r
222         public void setVisible(boolean visible) {\r
223                 super.setVisible(visible);\r
224                 this.filePathField.setFocus();\r
225         }\r
226         \r
227         \r
228         //Open dialog for choosing the file\r
229         protected void handleLocationDirectoryButtonPressed() {\r
230                 \r
231                 final Shell shell = filePathField.getShell();\r
232                 \r
233                 FileDialog dialog = new FileDialog(shell, SWT.SAVE);\r
234                 \r
235                 String[] ext = {"*.tg"};\r
236                 dialog.setFilterExtensions(ext);\r
237                 \r
238                 dialog.setText("Export Function Library");\r
239 \r
240                 String dirName = filePathField.getText().trim();\r
241                 \r
242                 File path = new File(dirName);\r
243                 if (path.exists()) {\r
244                         dialog.setFilterPath(new Path(dirName).toOSString());   \r
245                 }\r
246                 \r
247                 String selectedFile = dialog.open();\r
248                 if (selectedFile != null) {\r
249                         filePathField.setText(selectedFile);\r
250                         validatePage();\r
251                 }               \r
252 \r
253         }\r
254         \r
255         //Get selection from the tree\r
256         @SuppressWarnings("unchecked")\r
257         public static <T> T getExplorerResource(GraphExplorerComposite explorer,\r
258                         Class<T> clazz) {\r
259                 if(explorer == null)\r
260                         return null;\r
261                 ISelection selection = ((ISelectionProvider) explorer\r
262                                 .getAdapter(ISelectionProvider.class)).getSelection();\r
263                 if (selection == null)\r
264                         return null;\r
265                 IStructuredSelection iss = (IStructuredSelection) selection;\r
266                 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();\r
267                 if (inc == null)\r
268                         return null;\r
269                 final T resource = (T) inc.getAdapter(clazz);\r
270 \r
271                 return resource;\r
272         }\r
273         \r
274         public boolean createProjects(Resource selection) {\r
275                 \r
276                 final String selected = previouslyBrowsedFile;\r
277                 if(selected == null) return false;\r
278                 \r
279         final Resource functionLibrary = getExplorerResource(functionLibraryExplorer, Resource.class);\r
280         if(functionLibrary == null) return false;\r
281         \r
282                 String name = null;\r
283                 try {\r
284                         name = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
285 \r
286                                 @Override\r
287                                 public String perform(ReadGraph graph) throws DatabaseException {\r
288                                         if (!graph.hasStatement(functionLibrary, Layer0.getInstance(graph).PartOf))\r
289                                                 return null;\r
290                                         Layer0 l0 = Layer0.getInstance(graph);\r
291                                         String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));\r
292                                         return name;\r
293                                         \r
294                                 }\r
295                                 \r
296                         });\r
297                 } catch (DatabaseException e1) {\r
298                         e1.printStackTrace();\r
299                 }\r
300                 if(name == null) return false;\r
301         \r
302                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
303                         \r
304                         @Override\r
305                         public void run(ReadGraph graph) throws DatabaseException {\r
306                                 Layer0 l0 = Layer0.getInstance(graph);\r
307                                 String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));\r
308                                 ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();\r
309                                 roots.add(Pair.make(functionLibrary, name));\r
310                                 TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, functionLibrary));\r
311 \r
312                                 try {\r
313                                         Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
314                                 } catch (RuntimeBindingConstructionException e) {\r
315                                         e.printStackTrace();\r
316                                 } catch (IOException e) {\r
317                                         e.printStackTrace();\r
318                                 }\r
319 \r
320                                 \r
321                         }\r
322                 });\r
323         \r
324         return true;\r
325         }\r
326         \r
327         void validatePage() {\r
328                 \r
329                 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){\r
330                         setPageComplete(false);\r
331                         return;\r
332                 }\r
333                 \r
334                 if (functionLibraryExplorer != null){\r
335                         final Resource selectedResource = getExplorerResource(functionLibraryExplorer, Resource.class);\r
336 \r
337                         String root = null;\r
338                         try {\r
339                                 root = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
340 \r
341                                         @Override\r
342                                         public String perform(ReadGraph graph) throws DatabaseException {\r
343                                                 Layer0 l0 = Layer0.getInstance(graph);\r
344                                                 Resource model = graph.getPossibleObject(selectedResource, l0.PartOf);\r
345                                                 String rootName = NameUtils.getSafeName(graph, model);\r
346 \r
347                                                 return rootName;\r
348                                         }\r
349 \r
350                                 });\r
351                                 if (root != null && root.equalsIgnoreCase("Development Project")){\r
352                                         setPageComplete(false);\r
353                                         setMessage("Select Function Library folder under the Model or from the Shared Functions folder.");\r
354                                         return;\r
355                                 }\r
356                         } catch (DatabaseException e) {\r
357                                 e.printStackTrace();\r
358                         }\r
359                 }\r
360                 \r
361                 setPageComplete(true);\r
362                 \r
363         }\r
364         \r
365 }