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