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