]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/wizard/ModelExportWizardPage.java
Compiler warning elimination
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / wizard / ModelExportWizardPage.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g3d.wizard;\r
13 \r
14 import java.io.File;\r
15 import java.util.ArrayList;\r
16 import java.util.Collections;\r
17 import java.util.List;\r
18 \r
19 import org.eclipse.jface.layout.GridDataFactory;\r
20 import org.eclipse.jface.resource.ImageDescriptor;\r
21 import org.eclipse.jface.wizard.WizardPage;\r
22 import org.eclipse.swt.SWT;\r
23 import org.eclipse.swt.custom.CCombo;\r
24 import org.eclipse.swt.events.ModifyEvent;\r
25 import org.eclipse.swt.events.ModifyListener;\r
26 import org.eclipse.swt.events.SelectionAdapter;\r
27 import org.eclipse.swt.events.SelectionEvent;\r
28 import org.eclipse.swt.layout.GridData;\r
29 import org.eclipse.swt.layout.GridLayout;\r
30 import org.eclipse.swt.widgets.Button;\r
31 import org.eclipse.swt.widgets.Composite;\r
32 import org.eclipse.swt.widgets.DirectoryDialog;\r
33 import org.eclipse.swt.widgets.FileDialog;\r
34 import org.eclipse.swt.widgets.Label;\r
35 import org.simantics.Simantics;\r
36 import org.simantics.db.ReadGraph;\r
37 import org.simantics.db.Resource;\r
38 import org.simantics.db.common.NamedResource;\r
39 import org.simantics.db.common.request.ReadRequest;\r
40 import org.simantics.db.exception.DatabaseException;\r
41 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
42 \r
43 \r
44 public abstract class ModelExportWizardPage<T extends IExportModel> extends WizardPage{\r
45         T exportData;\r
46 \r
47         CCombo model;\r
48         CCombo exportLocation;\r
49         \r
50         boolean exportToFile = true;\r
51 \r
52         private List<NamedResource> models = new ArrayList<NamedResource>();\r
53 \r
54         private Button overwrite;\r
55         \r
56         \r
57         \r
58         public ModelExportWizardPage(String pageName, String title, ImageDescriptor titleImage, T data) {\r
59                 super(pageName, title, titleImage);\r
60                 this.exportData = data;\r
61                 exportToFile = data.usesFile();\r
62         }\r
63         \r
64         protected abstract List<NamedResource> getSupportedModels(ReadGraph graph, Resource project) throws DatabaseException;\r
65         \r
66         public String[] getFilterExtensions() {\r
67                 return new String[0];\r
68         }\r
69         \r
70         public String[] getFilterNames() {\r
71                 return new String[0];\r
72         }\r
73 \r
74         @Override\r
75         public void createControl(Composite parent) {\r
76                 Composite container = new Composite(parent, SWT.NONE);\r
77                 {\r
78                         GridLayout layout = new GridLayout();\r
79                         layout.horizontalSpacing = 20;\r
80                         layout.verticalSpacing = 10;\r
81                         layout.numColumns = 3;\r
82                         container.setLayout(layout);\r
83                 }\r
84 \r
85                 new Label(container, SWT.NONE).setText("Exported &model:");\r
86                 model = new CCombo(container, SWT.BORDER);\r
87                 {\r
88                         model.setEditable(false);\r
89                         model.setText("");\r
90                         model.setToolTipText("Selects the model to export.");\r
91                         GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);\r
92                         model.addSelectionListener(new SelectionAdapter() {\r
93                                 @Override\r
94                                 public void widgetSelected(SelectionEvent e) {\r
95                                         exportData.setModel(((NamedResource) model.getData(Integer.toString(model.getSelectionIndex()))));\r
96                                         validatePage();\r
97                                 }\r
98                         });\r
99 \r
100                 }\r
101                 if (exportToFile) {\r
102                         new Label(container, SWT.NONE).setText("&Target file:");\r
103                         exportLocation = new CCombo(container, SWT.BORDER);\r
104                         {\r
105                                 exportLocation.setText("");\r
106                                 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);\r
107                                 exportLocation.addModifyListener(new ModifyListener(){\r
108                                         @Override\r
109                                         public void modifyText(ModifyEvent e) {\r
110                                                 validatePage();\r
111                                         }\r
112                                 });\r
113                         }\r
114                         Button browseFileButton = new Button(container, SWT.PUSH);\r
115                         {\r
116                                 browseFileButton.setText("Browse...");\r
117                                 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));\r
118                                 browseFileButton.addSelectionListener(new SelectionAdapter() {\r
119                                         @Override\r
120                                         public void widgetSelected(SelectionEvent e) {\r
121                                                 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);\r
122                                                 dialog.setText("Choose Export Target File");\r
123                                                 dialog.setFilterExtensions(getFilterExtensions());\r
124                                                 dialog.setFilterNames(getFilterNames());\r
125                                                 String loc = exportLocation.getText();\r
126                                                 dialog.setFilterPath(loc);\r
127                                                 String file = dialog.open();\r
128                                                 if (file == null)\r
129                                                         return;\r
130                                                 exportLocation.setText(file);\r
131                                                 validatePage();\r
132                                         }\r
133                                 });\r
134                         }\r
135 \r
136                         \r
137 \r
138                         \r
139                 } else {\r
140                         new Label(container, SWT.NONE).setText("&Target folder:");\r
141                         exportLocation = new CCombo(container, SWT.BORDER);\r
142                         {\r
143                                 exportLocation.setText("");\r
144                                 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);\r
145                                 exportLocation.addModifyListener(new ModifyListener(){\r
146                                         @Override\r
147                                         public void modifyText(ModifyEvent e) {\r
148                                                 validatePage();\r
149                                         }\r
150                                 });\r
151                         }\r
152                         Button browseFileButton = new Button(container, SWT.PUSH);\r
153                         {\r
154                                 browseFileButton.setText("Browse...");\r
155                                 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));\r
156                                 browseFileButton.addSelectionListener(new SelectionAdapter() {\r
157                                         @Override\r
158                                         public void widgetSelected(SelectionEvent e) {\r
159                                                 DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.SAVE);\r
160                                                 dialog.setText("Choose Export Target Folder");\r
161                                                 String loc = exportLocation.getText();\r
162                                                 dialog.setFilterPath(loc);\r
163                                                 String file = dialog.open();\r
164                                                 if (file == null)\r
165                                                         return;\r
166                                                 exportLocation.setText(file);\r
167                                                 validatePage();\r
168                                         }\r
169                                 });\r
170                         }\r
171                 }\r
172                 \r
173                 Label horizRule = new Label(container, SWT.BORDER);\r
174                 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);\r
175                 \r
176                 overwrite = new Button(container, SWT.CHECK);\r
177                 overwrite.setText("&Overwrite existing files without warning");\r
178                 overwrite.setSelection(exportData.isOverwrite());\r
179                 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);\r
180                 overwrite.addSelectionListener(new SelectionAdapter() {\r
181                         @Override\r
182                         public void widgetSelected(SelectionEvent e) {\r
183                                 validatePage();\r
184                         }\r
185                 });\r
186 \r
187                 try {\r
188                         initializeData();\r
189                 } catch (DatabaseException e) {\r
190                         e.printStackTrace();\r
191                 }\r
192 \r
193                 setControl(container);\r
194                 validatePage();\r
195                 \r
196         }\r
197         \r
198 \r
199         \r
200         \r
201         protected void initializeData() throws DatabaseException {\r
202                 final Resource selection = ResourceAdaptionUtils.toSingleResource(exportData.getSelection());\r
203 \r
204                 Simantics.getSessionContext().getSession().syncRequest(\r
205                                 new ReadRequest() {\r
206                                         @Override\r
207                                         public void run(ReadGraph graph) throws DatabaseException {\r
208                                                 models = getSupportedModels(graph, Simantics.getProject().get());\r
209                                         }\r
210                                 });\r
211 \r
212                         Collections.sort(models);\r
213 \r
214                         // Populate combo boxes\r
215                         int i = 0;\r
216                         boolean selected = false;\r
217                         for (NamedResource s : models) {\r
218                                 model.add(s.getName());\r
219                                 model.setData(String.valueOf(i), s);\r
220                                 if (s.getResource().equals(selection)) {\r
221                                         model.select(i);\r
222                                         selected = true;\r
223                                 }\r
224                                 ++i;\r
225                         }\r
226                         if (!selected && i > 0)\r
227                                 model.select(0);\r
228                         \r
229                         if (model.getSelectionIndex() >= 0) {\r
230                                 exportData.setModel((NamedResource)model.getData(Integer.toString(model.getSelectionIndex())));\r
231                         }\r
232                         for (String path : exportData.getRecentLocations()) {\r
233                                 exportLocation.add(path);\r
234                         }\r
235                         if (exportLocation.getItemCount() > 0)\r
236                                 exportLocation.select(0);\r
237         }\r
238         \r
239         \r
240         \r
241         protected void validatePage() {\r
242                 if (exportData.getModel() == null) {\r
243                         setMessage("Select model to export.");\r
244                         setErrorMessage(null);\r
245                         setPageComplete(false);\r
246                         return;\r
247                 }\r
248                 String exportLoc = exportLocation.getText();\r
249                 File file;\r
250                 if (exportToFile) {\r
251 \r
252                         if (exportLoc.isEmpty()) {\r
253                                 setMessage("Select target file.");\r
254                                 setErrorMessage(null);\r
255                                 setPageComplete(false);\r
256                                 return;\r
257                         }\r
258                         file = new File(exportLoc);\r
259                         if (file.exists() && !file.isFile()) {\r
260                                 setErrorMessage("The target must be a file, an existing directory was given.");\r
261                                 setPageComplete(false);\r
262                                 return;\r
263                         }\r
264                         File parent = file.getParentFile();\r
265                         if (parent == null || !parent.isDirectory()) {\r
266                                 setErrorMessage("The target directory does not exist.");\r
267                                 setPageComplete(false);\r
268                                 return;\r
269                         }\r
270 \r
271                 } else {\r
272                         if (exportLoc.isEmpty()) {\r
273                                 setMessage("Select target directory.");\r
274                                 setErrorMessage(null);\r
275                                 setPageComplete(false);\r
276                                 return;\r
277                         }\r
278                         file = new File(exportLoc);\r
279                         if (file.exists() && !file.isDirectory()) {\r
280                                 setErrorMessage("The target must be a directory, an existing file was given.");\r
281                                 setPageComplete(false);\r
282                                 return;\r
283                         }\r
284 \r
285                 }\r
286 \r
287                 exportData.setExportLocation(file);\r
288                 exportData.setOverwrite(overwrite.getSelection());\r
289 \r
290                 setErrorMessage(null);\r
291                 setMessage("Export selected model.");\r
292                 setPageComplete(true);\r
293         }\r
294 }\r