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