]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
a7194de6250afccba0ec6f60fb727dcea90c4814
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.wizards.models;\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.exception.DatabaseException;\r
39 import org.simantics.db.layer0.util.TransferableGraphRequest2;\r
40 import org.simantics.db.request.Read;\r
41 import org.simantics.graph.representation.TransferableGraph1;\r
42 import org.simantics.layer0.Layer0;\r
43 import org.simantics.sysdyn.SysdynResource;\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 WizardModelsExportPage 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 \r
56         private static String previouslyBrowsedFile = "";\r
57 \r
58         private Button browseDirectoriesButton;\r
59         \r
60         //private IStructuredSelection currentSelection;\r
61         \r
62         private Resource selectedModel;\r
63         \r
64         GraphExplorerComposite modelExplorer;\r
65         \r
66         private boolean selectionMade = false;\r
67         \r
68         \r
69         \r
70         /**\r
71          * Creates a new project creation wizard page.\r
72          * \r
73          */\r
74         public WizardModelsExportPage() {\r
75                 this("wizardModelsExportPage", null, null); //$NON-NLS-1$\r
76         }\r
77 \r
78         /**\r
79          * Create a new instance of the receiver.\r
80          * \r
81          * @param pageName\r
82          */\r
83         public WizardModelsExportPage(String pageName) {\r
84                 this(pageName,null, null);\r
85         }\r
86                         \r
87         /**\r
88          * More (many more) parameters.\r
89          * \r
90          * @param pageName\r
91          * @param initialPath\r
92          * @param currentSelection\r
93          * @since 3.5\r
94          */\r
95         public WizardModelsExportPage(String pageName,String initialPath,\r
96                         IStructuredSelection currentSelection) {\r
97                 super(pageName);\r
98                 //this.currentSelection = currentSelection;\r
99                 setPageComplete(false);\r
100                 setTitle("Export Model");\r
101                 setDescription("Choose the Model and the export location, then press Finish.");\r
102         }\r
103         \r
104         public void createControl(Composite parent) {\r
105                 \r
106                 initializeDialogUnits(parent);\r
107 \r
108                 Composite workArea = new Composite(parent, SWT.NONE);\r
109                 setControl(workArea);\r
110 \r
111                 workArea.setLayout(new GridLayout());\r
112                 workArea.setLayoutData(new GridData(GridData.FILL_BOTH\r
113                                 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));\r
114                 \r
115                 createProjectsRoot(workArea);\r
116                 createTree (workArea);\r
117         }\r
118         \r
119         private void createProjectsRoot(Composite workArea) {\r
120                 \r
121                 // set label for field\r
122                 Label title = new Label(workArea, SWT.NONE);\r
123                 title.setText("Select the export location for Model:");\r
124                 \r
125                 Composite projectGroup = new Composite(workArea, SWT.NONE);\r
126                 GridLayout layout = new GridLayout();\r
127                 layout.numColumns = 2;\r
128                 layout.makeColumnsEqualWidth = false;\r
129                 layout.marginWidth = 0;\r
130 \r
131                 projectGroup.setLayout(layout);\r
132                 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));\r
133                 \r
134                 // model location entry field\r
135                 this.filePathField = new Text(projectGroup, SWT.BORDER);\r
136 \r
137                 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);\r
138                 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);\r
139                 filePathField.setLayoutData(directoryPathData);\r
140                 \r
141                 filePathField.addModifyListener(new ModifyListener(){\r
142             @Override\r
143             public void modifyText(ModifyEvent e) {\r
144                 previouslyBrowsedFile = filePathField.getText();        \r
145             }\r
146                 });\r
147                 if (previouslyBrowsedFile != null){\r
148                         filePathField.setText(previouslyBrowsedFile);\r
149                         validatePage();\r
150                 }\r
151 \r
152                 // browse button\r
153                 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);\r
154                 browseDirectoriesButton.setText("Browse");\r
155                 setButtonLayoutData(browseDirectoriesButton);\r
156                 \r
157                 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {\r
158                         /*\r
159                          * (non-Javadoc)\r
160                          * \r
161                          * @see org.eclipse.swt.events.SelectionAdapter#widgetS\r
162                          * elected(org.eclipse.swt.events.SelectionEvent)\r
163                          */\r
164                         public void widgetSelected(SelectionEvent e) {\r
165                                 handleLocationDirectoryButtonPressed();\r
166                         }\r
167                 });\r
168                 \r
169         }\r
170         \r
171         private void createTree(Composite workArea){\r
172                 \r
173                 //set label for tree\r
174                 Label title = new Label(workArea, SWT.NONE);\r
175                 title.setText("Select Model to export:");\r
176 \r
177                 try {\r
178                         Resource input = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
179 \r
180                                 @Override\r
181                                 public Resource perform(ReadGraph graph)\r
182                                                 throws DatabaseException {\r
183                                         Resource model = SimanticsUI.getProject().get();\r
184                                         return model;\r
185                                 }\r
186 \r
187                         });\r
188 \r
189                         modelExplorer = new GraphExplorerComposite(ArrayMap.keys(\r
190                                         "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);\r
191 \r
192                         modelExplorer\r
193                         .setBrowseContexts(SysdynResource.URIs.ImportModuleTree);\r
194 \r
195                         modelExplorer.finish();\r
196 \r
197                         modelExplorer.setInput(null, input);\r
198 \r
199                         GridDataFactory.fillDefaults().grab(true, true).applyTo(\r
200                                         modelExplorer);\r
201                         \r
202                         ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {\r
203                                 \r
204                                 @Override\r
205                                 public void widgetSelected(SelectionEvent e) {\r
206                                         selectionMade = true;\r
207                                         validatePage();\r
208                                 }\r
209                                 @Override\r
210                                 public void widgetDefaultSelected(SelectionEvent e) {\r
211                                         selectionMade = true;\r
212                                         validatePage();\r
213                                 }\r
214                         });\r
215                         \r
216 \r
217                 } catch (DatabaseException e) {\r
218                         e.printStackTrace();\r
219                 }\r
220 \r
221         }\r
222         \r
223         //Set filePathField active\r
224         public void setVisible(boolean visible) {\r
225                 super.setVisible(visible);\r
226                 this.filePathField.setFocus();\r
227         }\r
228         \r
229         //Open dialog for choosing the file\r
230         protected void handleLocationDirectoryButtonPressed() {\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 Model");\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         //Get selection from the tree\r
255         @SuppressWarnings("unchecked")\r
256         public static <T> T getExplorerResource(GraphExplorerComposite explorer,\r
257                         Class<T> clazz) {\r
258                 \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() {\r
275                 \r
276                 final String selected = previouslyBrowsedFile;\r
277                 if(selected == null) return false;\r
278                 \r
279                 selectedModel= getExplorerResource(modelExplorer, Resource.class);\r
280                 if(selectedModel == null)\r
281                         return false;\r
282         \r
283         // FIXME: Model browser doesn't change its selection even if the selected object is removed,\r
284         // so you can try to export a removed model \r
285                 \r
286                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
287                         \r
288                         @Override\r
289                         public void run(ReadGraph graph) throws DatabaseException {\r
290                                 Layer0 l0 = Layer0.getInstance(graph);\r
291                                 String name = graph.syncRequest(new PossibleRelatedValue<String>(selectedModel, l0.HasName, Bindings.STRING ));\r
292                                 ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();\r
293                                 roots.add(Pair.make(selectedModel, name));\r
294                                 TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, selectedModel));\r
295 \r
296                                 try {\r
297                                         Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
298                                 } catch (RuntimeBindingConstructionException e) {\r
299                                         e.printStackTrace();\r
300                                 } catch (IOException e) {\r
301                                         e.printStackTrace();\r
302                                 }\r
303                                 \r
304                         }\r
305                 });\r
306 \r
307                 return true;\r
308         }\r
309         \r
310         void validatePage() {\r
311                 \r
312                 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){\r
313                         setPageComplete(false);\r
314                         return;\r
315                 }\r
316                 \r
317                 setPageComplete(true);\r
318                 \r
319         }\r
320 }\r