1 package org.simantics.g3d.wizard;
\r
4 import java.util.ArrayList;
\r
5 import java.util.Collections;
\r
6 import java.util.List;
\r
8 import org.eclipse.jface.layout.GridDataFactory;
\r
9 import org.eclipse.jface.resource.ImageDescriptor;
\r
10 import org.eclipse.jface.wizard.WizardPage;
\r
11 import org.eclipse.swt.SWT;
\r
12 import org.eclipse.swt.custom.CCombo;
\r
13 import org.eclipse.swt.events.ModifyEvent;
\r
14 import org.eclipse.swt.events.ModifyListener;
\r
15 import org.eclipse.swt.events.SelectionAdapter;
\r
16 import org.eclipse.swt.events.SelectionEvent;
\r
17 import org.eclipse.swt.layout.GridData;
\r
18 import org.eclipse.swt.layout.GridLayout;
\r
19 import org.eclipse.swt.widgets.Button;
\r
20 import org.eclipse.swt.widgets.Composite;
\r
21 import org.eclipse.swt.widgets.DirectoryDialog;
\r
22 import org.eclipse.swt.widgets.FileDialog;
\r
23 import org.eclipse.swt.widgets.Label;
\r
24 import org.simantics.Simantics;
\r
25 import org.simantics.db.ReadGraph;
\r
26 import org.simantics.db.Resource;
\r
27 import org.simantics.db.common.NamedResource;
\r
28 import org.simantics.db.common.request.ReadRequest;
\r
29 import org.simantics.db.exception.DatabaseException;
\r
30 import org.simantics.layer0.Layer0;
\r
31 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
34 public abstract class ModelExportWizardPage<T extends IExportModel> extends WizardPage{
\r
38 CCombo exportLocation;
\r
40 boolean exportToFile = true;
\r
42 private List<NamedResource> models = new ArrayList<NamedResource>();
\r
44 private Button overwrite;
\r
48 public ModelExportWizardPage(String pageName, String title, ImageDescriptor titleImage, T data) {
\r
49 super(pageName, title, titleImage);
\r
50 this.exportData = data;
\r
51 exportToFile = data.usesFile();
\r
54 protected abstract List<NamedResource> getSupportedModels(ReadGraph graph, Resource project) throws DatabaseException;
\r
56 public String[] getFilterExtensions() {
\r
57 return new String[0];
\r
60 public String[] getFilterNames() {
\r
61 return new String[0];
\r
65 public void createControl(Composite parent) {
\r
66 Composite container = new Composite(parent, SWT.NONE);
\r
68 GridLayout layout = new GridLayout();
\r
69 layout.horizontalSpacing = 20;
\r
70 layout.verticalSpacing = 10;
\r
71 layout.numColumns = 3;
\r
72 container.setLayout(layout);
\r
75 new Label(container, SWT.NONE).setText("Exported &model:");
\r
76 model = new CCombo(container, SWT.BORDER);
\r
78 model.setEditable(false);
\r
80 model.setToolTipText("Selects the model to export.");
\r
81 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);
\r
82 model.addSelectionListener(new SelectionAdapter() {
\r
84 public void widgetSelected(SelectionEvent e) {
\r
85 exportData.setModel(((NamedResource) model.getData(Integer.toString(model.getSelectionIndex()))));
\r
92 new Label(container, SWT.NONE).setText("&Target file:");
\r
93 exportLocation = new CCombo(container, SWT.BORDER);
\r
95 exportLocation.setText("");
\r
96 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
\r
97 exportLocation.addModifyListener(new ModifyListener(){
\r
99 public void modifyText(ModifyEvent e) {
\r
104 Button browseFileButton = new Button(container, SWT.PUSH);
\r
106 browseFileButton.setText("Browse...");
\r
107 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
\r
108 browseFileButton.addSelectionListener(new SelectionAdapter() {
\r
110 public void widgetSelected(SelectionEvent e) {
\r
111 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
\r
112 dialog.setText("Choose Export Target File");
\r
113 dialog.setFilterExtensions(getFilterExtensions());
\r
114 dialog.setFilterNames(getFilterNames());
\r
115 String loc = exportLocation.getText();
\r
116 dialog.setFilterPath(loc);
\r
117 String file = dialog.open();
\r
120 exportLocation.setText(file);
\r
130 new Label(container, SWT.NONE).setText("&Target folder:");
\r
131 exportLocation = new CCombo(container, SWT.BORDER);
\r
133 exportLocation.setText("");
\r
134 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
\r
135 exportLocation.addModifyListener(new ModifyListener(){
\r
137 public void modifyText(ModifyEvent e) {
\r
142 Button browseFileButton = new Button(container, SWT.PUSH);
\r
144 browseFileButton.setText("Browse...");
\r
145 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
\r
146 browseFileButton.addSelectionListener(new SelectionAdapter() {
\r
148 public void widgetSelected(SelectionEvent e) {
\r
149 DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.SAVE);
\r
150 dialog.setText("Choose Export Target Folder");
\r
151 String loc = exportLocation.getText();
\r
152 dialog.setFilterPath(loc);
\r
153 String file = dialog.open();
\r
156 exportLocation.setText(file);
\r
163 Label horizRule = new Label(container, SWT.BORDER);
\r
164 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
\r
166 overwrite = new Button(container, SWT.CHECK);
\r
167 overwrite.setText("&Overwrite existing files without warning");
\r
168 overwrite.setSelection(exportData.isOverwrite());
\r
169 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);
\r
170 overwrite.addSelectionListener(new SelectionAdapter() {
\r
172 public void widgetSelected(SelectionEvent e) {
\r
179 } catch (DatabaseException e) {
\r
180 e.printStackTrace();
\r
183 setControl(container);
\r
191 protected void initializeData() throws DatabaseException {
\r
192 final Resource selection = ResourceAdaptionUtils.toSingleResource(exportData.getSelection());
\r
194 Simantics.getSessionContext().getSession().syncRequest(
\r
195 new ReadRequest() {
\r
197 public void run(ReadGraph graph) throws DatabaseException {
\r
198 Layer0 L0 = Layer0.getInstance(graph);
\r
199 models = getSupportedModels(graph, Simantics.getProject().get());
\r
203 Collections.sort(models);
\r
205 // Populate combo boxes
\r
207 boolean selected = false;
\r
208 for (NamedResource s : models) {
\r
209 model.add(s.getName());
\r
210 model.setData(String.valueOf(i), s);
\r
211 if (s.getResource().equals(selection)) {
\r
217 if (!selected && i > 0)
\r
220 if (model.getSelectionIndex() >= 0) {
\r
221 exportData.setModel((NamedResource)model.getData(Integer.toString(model.getSelectionIndex())));
\r
223 for (String path : exportData.getRecentLocations()) {
\r
224 exportLocation.add(path);
\r
226 if (exportLocation.getItemCount() > 0)
\r
227 exportLocation.select(0);
\r
232 protected void validatePage() {
\r
233 if (exportData.getModel() == null) {
\r
234 setMessage("Select model to export.");
\r
235 setErrorMessage(null);
\r
236 setPageComplete(false);
\r
239 String exportLoc = exportLocation.getText();
\r
241 if (exportToFile) {
\r
243 if (exportLoc.isEmpty()) {
\r
244 setMessage("Select target file.");
\r
245 setErrorMessage(null);
\r
246 setPageComplete(false);
\r
249 file = new File(exportLoc);
\r
250 if (file.exists() && !file.isFile()) {
\r
251 setErrorMessage("The target must be a file, an existing directory was given.");
\r
252 setPageComplete(false);
\r
255 File parent = file.getParentFile();
\r
256 if (parent == null || !parent.isDirectory()) {
\r
257 setErrorMessage("The target directory does not exist.");
\r
258 setPageComplete(false);
\r
263 if (exportLoc.isEmpty()) {
\r
264 setMessage("Select target directory.");
\r
265 setErrorMessage(null);
\r
266 setPageComplete(false);
\r
269 file = new File(exportLoc);
\r
270 if (file.exists() && !file.isDirectory()) {
\r
271 setErrorMessage("The target must be a directory, an existing file was given.");
\r
272 setPageComplete(false);
\r
278 exportData.setExportLocation(file);
\r
279 exportData.setOverwrite(overwrite.getSelection());
\r
281 setErrorMessage(null);
\r
282 setMessage("Export selected model.");
\r
283 setPageComplete(true);
\r