]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/wizard/DrawingTemplateExportPage.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / wizard / DrawingTemplateExportPage.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.wizard;
13
14 import java.io.File;
15 import java.util.Collections;
16 import java.util.List;
17
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;
40
41 /**
42  * @author Tuukka Lehtonen
43  * @author Teemu Mätäsniemi
44  */
45 public class DrawingTemplateExportPage extends WizardPage {
46
47     ExportPlan          exportModel;
48     CCombo              model;
49     CCombo              exportLocation;
50
51     List<NamedResource> models = Collections.emptyList();
52     private Button      overwrite;
53
54     protected DrawingTemplateExportPage(ExportPlan model) {
55         super("Export Diagram Template", "Define Export Location", null);
56         this.exportModel = model;
57     }
58
59     @Override
60     public void createControl(Composite parent) {
61         Composite container = new Composite(parent, SWT.NONE);
62         {
63             GridLayout layout = new GridLayout();
64             layout.horizontalSpacing = 20;
65             layout.verticalSpacing = 10;
66             layout.numColumns = 3;
67             container.setLayout(layout);
68         }
69
70         new Label(container, SWT.NONE).setText("&Exported template:");
71         model = new CCombo(container, SWT.BORDER);
72         {
73             model.setEditable(false);
74             model.setText("");
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(){
78                 @Override
79                 public void modifyText(ModifyEvent e) {
80                     validatePage();
81                 }
82             });
83         }
84
85         new Label(container, SWT.NONE).setText("&Target file:");
86         exportLocation = new CCombo(container, SWT.BORDER);
87         {
88             exportLocation.setText("");
89             GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
90             exportLocation.addModifyListener(new ModifyListener(){
91                 @Override
92                 public void modifyText(ModifyEvent e) {
93                     validatePage();
94                 }
95             });
96         }
97         Button browseFileButton = new Button(container, SWT.PUSH);
98         {
99             browseFileButton.setText("Browse...");
100             browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
101             browseFileButton.addSelectionListener(new SelectionAdapter() {
102                 @Override
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();
112                     if (file == null)
113                         return;
114                     exportLocation.setText(file);
115                     validatePage();
116                 }
117             });
118         }
119
120         Label horizRule = new Label(container, SWT.BORDER);
121         GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
122
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() {
128             @Override
129             public void widgetSelected(SelectionEvent e) {
130                 validatePage();
131             }
132         });
133
134         try {
135             initializeData();
136         } catch (DatabaseException e) {
137             e.printStackTrace();
138         }
139
140         setControl(container);
141         validatePage();
142     }
143
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();
148
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));
151
152         // Load all states in the selected model
153         exportModel.model = new NamedResource(name, drawingTemplates.get(0));
154         models = Collections.singletonList(exportModel.model);
155
156         // Populate combo boxes
157         int i = 0;
158         for (NamedResource m : models) {
159             model.add(m.getName());
160             model.setData(String.valueOf(i), m);
161             if (m.equals(exportModel.model))
162                 model.select(i);
163             ++i;
164         }
165
166         for (String path : exportModel.recentLocations) {
167             exportLocation.add(path);
168         }
169         if (exportLocation.getItemCount() > 0)
170             exportLocation.select(0);
171     }
172
173     void validatePage() {
174         if (exportModel.model == null) {
175             setMessage("Select model to export from.");
176             setErrorMessage(null);
177             setPageComplete(false);
178             return;
179         }
180
181         String exportLoc = exportLocation.getText();
182         if (exportLoc.isEmpty()) {
183             setMessage("Select target file.");
184             setErrorMessage(null);
185             setPageComplete(false);
186             return;
187         }
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);
194             return;
195         }
196         File parent = file.getParentFile();
197         if (parent == null || !parent.isDirectory()) {
198             setErrorMessage("The target directory does not exist.");
199             setPageComplete(false);
200             return;
201         }
202         exportModel.exportLocation = file;
203         exportModel.overwrite = overwrite.getSelection();
204
205         setErrorMessage(null);
206         setMessage("Export diagram template to " + exportLoc + ".");
207         setPageComplete(true);
208     }
209
210 }