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.layer0.Layer0;
\r
42 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
45 public abstract class ModelExportWizardPage<T extends IExportModel> extends WizardPage{
\r
49 CCombo exportLocation;
\r
51 boolean exportToFile = true;
\r
53 private List<NamedResource> models = new ArrayList<NamedResource>();
\r
55 private Button overwrite;
\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
65 protected abstract List<NamedResource> getSupportedModels(ReadGraph graph, Resource project) throws DatabaseException;
\r
67 public String[] getFilterExtensions() {
\r
68 return new String[0];
\r
71 public String[] getFilterNames() {
\r
72 return new String[0];
\r
76 public void createControl(Composite parent) {
\r
77 Composite container = new Composite(parent, SWT.NONE);
\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
86 new Label(container, SWT.NONE).setText("Exported &model:");
\r
87 model = new CCombo(container, SWT.BORDER);
\r
89 model.setEditable(false);
\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
95 public void widgetSelected(SelectionEvent e) {
\r
96 exportData.setModel(((NamedResource) model.getData(Integer.toString(model.getSelectionIndex()))));
\r
102 if (exportToFile) {
\r
103 new Label(container, SWT.NONE).setText("&Target file:");
\r
104 exportLocation = new CCombo(container, SWT.BORDER);
\r
106 exportLocation.setText("");
\r
107 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
\r
108 exportLocation.addModifyListener(new ModifyListener(){
\r
110 public void modifyText(ModifyEvent e) {
\r
115 Button browseFileButton = new Button(container, SWT.PUSH);
\r
117 browseFileButton.setText("Browse...");
\r
118 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
\r
119 browseFileButton.addSelectionListener(new SelectionAdapter() {
\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
131 exportLocation.setText(file);
\r
141 new Label(container, SWT.NONE).setText("&Target folder:");
\r
142 exportLocation = new CCombo(container, SWT.BORDER);
\r
144 exportLocation.setText("");
\r
145 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
\r
146 exportLocation.addModifyListener(new ModifyListener(){
\r
148 public void modifyText(ModifyEvent e) {
\r
153 Button browseFileButton = new Button(container, SWT.PUSH);
\r
155 browseFileButton.setText("Browse...");
\r
156 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
\r
157 browseFileButton.addSelectionListener(new SelectionAdapter() {
\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
167 exportLocation.setText(file);
\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
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
183 public void widgetSelected(SelectionEvent e) {
\r
190 } catch (DatabaseException e) {
\r
191 e.printStackTrace();
\r
194 setControl(container);
\r
202 protected void initializeData() throws DatabaseException {
\r
203 final Resource selection = ResourceAdaptionUtils.toSingleResource(exportData.getSelection());
\r
205 Simantics.getSessionContext().getSession().syncRequest(
\r
206 new ReadRequest() {
\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
214 Collections.sort(models);
\r
216 // Populate combo boxes
\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
228 if (!selected && i > 0)
\r
231 if (model.getSelectionIndex() >= 0) {
\r
232 exportData.setModel((NamedResource)model.getData(Integer.toString(model.getSelectionIndex())));
\r
234 for (String path : exportData.getRecentLocations()) {
\r
235 exportLocation.add(path);
\r
237 if (exportLocation.getItemCount() > 0)
\r
238 exportLocation.select(0);
\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
250 String exportLoc = exportLocation.getText();
\r
252 if (exportToFile) {
\r
254 if (exportLoc.isEmpty()) {
\r
255 setMessage("Select target file.");
\r
256 setErrorMessage(null);
\r
257 setPageComplete(false);
\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
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
274 if (exportLoc.isEmpty()) {
\r
275 setMessage("Select target directory.");
\r
276 setErrorMessage(null);
\r
277 setPageComplete(false);
\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
289 exportData.setExportLocation(file);
\r
290 exportData.setOverwrite(overwrite.getSelection());
\r
292 setErrorMessage(null);
\r
293 setMessage("Export selected model.");
\r
294 setPageComplete(true);
\r