1 /*******************************************************************************
\r
2 * Copyright (c) 2012 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.modeling.template2d.ui.wizard;
\r
14 import java.io.File;
\r
15 import java.util.Collections;
\r
16 import java.util.List;
\r
18 import org.eclipse.jface.layout.GridDataFactory;
\r
19 import org.eclipse.jface.wizard.WizardPage;
\r
20 import org.eclipse.swt.SWT;
\r
21 import org.eclipse.swt.custom.CCombo;
\r
22 import org.eclipse.swt.events.ModifyEvent;
\r
23 import org.eclipse.swt.events.ModifyListener;
\r
24 import org.eclipse.swt.events.SelectionAdapter;
\r
25 import org.eclipse.swt.events.SelectionEvent;
\r
26 import org.eclipse.swt.layout.GridData;
\r
27 import org.eclipse.swt.layout.GridLayout;
\r
28 import org.eclipse.swt.widgets.Button;
\r
29 import org.eclipse.swt.widgets.Composite;
\r
30 import org.eclipse.swt.widgets.FileDialog;
\r
31 import org.eclipse.swt.widgets.Label;
\r
32 import org.simantics.databoard.Bindings;
\r
33 import org.simantics.db.Resource;
\r
34 import org.simantics.db.common.NamedResource;
\r
35 import org.simantics.db.common.primitiverequest.RelatedValue;
\r
36 import org.simantics.db.exception.DatabaseException;
\r
37 import org.simantics.db.layer0.SelectionHints;
\r
38 import org.simantics.layer0.Layer0;
\r
39 import org.simantics.utils.ui.ISelectionUtils;
\r
42 * @author Tuukka Lehtonen
\r
43 * @author Teemu Mätäsniemi
\r
45 public class DrawingTemplateExportPage extends WizardPage {
\r
47 ExportPlan exportModel;
\r
49 CCombo exportLocation;
\r
51 List<NamedResource> models = Collections.emptyList();
\r
52 private Button overwrite;
\r
54 protected DrawingTemplateExportPage(ExportPlan model) {
\r
55 super("Export Diagram Template", "Define Export Location", null);
\r
56 this.exportModel = model;
\r
60 public void createControl(Composite parent) {
\r
61 Composite container = new Composite(parent, SWT.NONE);
\r
63 GridLayout layout = new GridLayout();
\r
64 layout.horizontalSpacing = 20;
\r
65 layout.verticalSpacing = 10;
\r
66 layout.numColumns = 3;
\r
67 container.setLayout(layout);
\r
70 new Label(container, SWT.NONE).setText("&Exported template:");
\r
71 model = new CCombo(container, SWT.BORDER);
\r
73 model.setEditable(false);
\r
75 model.setToolTipText("Selects the diagram template to export a state from.");
\r
76 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);
\r
77 model.addModifyListener(new ModifyListener(){
\r
79 public void modifyText(ModifyEvent e) {
\r
85 new Label(container, SWT.NONE).setText("&Target file:");
\r
86 exportLocation = new CCombo(container, SWT.BORDER);
\r
88 exportLocation.setText("");
\r
89 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
\r
90 exportLocation.addModifyListener(new ModifyListener(){
\r
92 public void modifyText(ModifyEvent e) {
\r
97 Button browseFileButton = new Button(container, SWT.PUSH);
\r
99 browseFileButton.setText("Browse...");
\r
100 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
\r
101 browseFileButton.addSelectionListener(new SelectionAdapter() {
\r
103 public void widgetSelected(SelectionEvent e) {
\r
104 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
\r
105 dialog.setText("Choose Export Target File");
\r
106 String loc = exportLocation.getText();
\r
107 dialog.setFilterPath(loc);
\r
108 dialog.setFilterExtensions(new String[] { "*.diagramTemplate" });
\r
109 dialog.setFilterNames(new String[] { "Diagram Template (*.diagramTemplate)" });
\r
110 dialog.setOverwrite(false);
\r
111 String file = dialog.open();
\r
114 exportLocation.setText(file);
\r
120 Label horizRule = new Label(container, SWT.BORDER);
\r
121 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
\r
123 overwrite = new Button(container, SWT.CHECK);
\r
124 overwrite.setText("&Overwrite existing files without warning");
\r
125 overwrite.setSelection(exportModel.overwrite);
\r
126 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);
\r
127 overwrite.addSelectionListener(new SelectionAdapter() {
\r
129 public void widgetSelected(SelectionEvent e) {
\r
136 } catch (DatabaseException e) {
\r
137 e.printStackTrace();
\r
140 setControl(container);
\r
144 private void initializeData() throws DatabaseException {
\r
145 List<Resource> drawingTemplates = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
\r
146 // FIXME: YUCK! Do not do this.
\r
147 if(drawingTemplates.size() != 1) throw new RuntimeException();
\r
149 Layer0 L0 = Layer0.getInstance(exportModel.sessionContext.getSession());
\r
150 String name = exportModel.sessionContext.getSession().sync(new RelatedValue<String>(drawingTemplates.get(0), L0.HasName, Bindings.STRING));
\r
152 // Load all states in the selected model
\r
153 exportModel.model = new NamedResource(name, drawingTemplates.get(0));
\r
154 models = Collections.singletonList(exportModel.model);
\r
156 // Populate combo boxes
\r
158 for (NamedResource m : models) {
\r
159 model.add(m.getName());
\r
160 model.setData(String.valueOf(i), m);
\r
161 if (m.equals(exportModel.model))
\r
166 for (String path : exportModel.recentLocations) {
\r
167 exportLocation.add(path);
\r
169 if (exportLocation.getItemCount() > 0)
\r
170 exportLocation.select(0);
\r
173 void validatePage() {
\r
174 if (exportModel.model == null) {
\r
175 setMessage("Select model to export from.");
\r
176 setErrorMessage(null);
\r
177 setPageComplete(false);
\r
181 String exportLoc = exportLocation.getText();
\r
182 if (exportLoc.isEmpty()) {
\r
183 setMessage("Select target file.");
\r
184 setErrorMessage(null);
\r
185 setPageComplete(false);
\r
188 if (!exportLoc.endsWith(".diagramTemplate"))
\r
189 exportLoc += exportLoc.endsWith(".") ? "diagramTemplate" : ".diagramTemplate";
\r
190 File file = new File(exportLoc);
\r
191 if (file.isDirectory()) {
\r
192 setErrorMessage("The target is a directory.");
\r
193 setPageComplete(false);
\r
196 File parent = file.getParentFile();
\r
197 if (parent == null || !parent.isDirectory()) {
\r
198 setErrorMessage("The target directory does not exist.");
\r
199 setPageComplete(false);
\r
202 exportModel.exportLocation = file;
\r
203 exportModel.overwrite = overwrite.getSelection();
\r
205 setErrorMessage(null);
\r
206 setMessage("Export diagram template to " + exportLoc + ".");
\r
207 setPageComplete(true);
\r