1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g3d.wizard;
\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
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
44 public abstract class ModelExportWizardPage<T extends IExportModel> extends WizardPage{
\r
48 CCombo exportLocation;
\r
50 boolean exportToFile = true;
\r
52 private List<NamedResource> models = new ArrayList<NamedResource>();
\r
54 private Button overwrite;
\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
64 protected abstract List<NamedResource> getSupportedModels(ReadGraph graph, Resource project) throws DatabaseException;
\r
66 public String[] getFilterExtensions() {
\r
67 return new String[0];
\r
70 public String[] getFilterNames() {
\r
71 return new String[0];
\r
75 public void createControl(Composite parent) {
\r
76 Composite container = new Composite(parent, SWT.NONE);
\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
85 new Label(container, SWT.NONE).setText("Exported &model:");
\r
86 model = new CCombo(container, SWT.BORDER);
\r
88 model.setEditable(false);
\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
94 public void widgetSelected(SelectionEvent e) {
\r
95 exportData.setModel(((NamedResource) model.getData(Integer.toString(model.getSelectionIndex()))));
\r
101 if (exportToFile) {
\r
102 new Label(container, SWT.NONE).setText("&Target file:");
\r
103 exportLocation = new CCombo(container, SWT.BORDER);
\r
105 exportLocation.setText("");
\r
106 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
\r
107 exportLocation.addModifyListener(new ModifyListener(){
\r
109 public void modifyText(ModifyEvent e) {
\r
114 Button browseFileButton = new Button(container, SWT.PUSH);
\r
116 browseFileButton.setText("Browse...");
\r
117 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
\r
118 browseFileButton.addSelectionListener(new SelectionAdapter() {
\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
130 exportLocation.setText(file);
\r
140 new Label(container, SWT.NONE).setText("&Target folder:");
\r
141 exportLocation = new CCombo(container, SWT.BORDER);
\r
143 exportLocation.setText("");
\r
144 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
\r
145 exportLocation.addModifyListener(new ModifyListener(){
\r
147 public void modifyText(ModifyEvent e) {
\r
152 Button browseFileButton = new Button(container, SWT.PUSH);
\r
154 browseFileButton.setText("Browse...");
\r
155 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
\r
156 browseFileButton.addSelectionListener(new SelectionAdapter() {
\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
166 exportLocation.setText(file);
\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
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
182 public void widgetSelected(SelectionEvent e) {
\r
189 } catch (DatabaseException e) {
\r
190 e.printStackTrace();
\r
193 setControl(container);
\r
201 protected void initializeData() throws DatabaseException {
\r
202 final Resource selection = ResourceAdaptionUtils.toSingleResource(exportData.getSelection());
\r
204 Simantics.getSessionContext().getSession().syncRequest(
\r
205 new ReadRequest() {
\r
207 public void run(ReadGraph graph) throws DatabaseException {
\r
208 models = getSupportedModels(graph, Simantics.getProject().get());
\r
212 Collections.sort(models);
\r
214 // Populate combo boxes
\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
226 if (!selected && i > 0)
\r
229 if (model.getSelectionIndex() >= 0) {
\r
230 exportData.setModel((NamedResource)model.getData(Integer.toString(model.getSelectionIndex())));
\r
232 for (String path : exportData.getRecentLocations()) {
\r
233 exportLocation.add(path);
\r
235 if (exportLocation.getItemCount() > 0)
\r
236 exportLocation.select(0);
\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
248 String exportLoc = exportLocation.getText();
\r
250 if (exportToFile) {
\r
252 if (exportLoc.isEmpty()) {
\r
253 setMessage("Select target file.");
\r
254 setErrorMessage(null);
\r
255 setPageComplete(false);
\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
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
272 if (exportLoc.isEmpty()) {
\r
273 setMessage("Select target directory.");
\r
274 setErrorMessage(null);
\r
275 setPageComplete(false);
\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
287 exportData.setExportLocation(file);
\r
288 exportData.setOverwrite(overwrite.getSelection());
\r
290 setErrorMessage(null);
\r
291 setMessage("Export selected model.");
\r
292 setPageComplete(true);
\r