]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExportPage.java
7bdfecd60efb51d994f64f8b7e130bd94669f190
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sharedontology / wizard / ModelExportPage.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.ui.sharedontology.wizard;
13
14 import java.io.File;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.List;
19
20 import org.eclipse.jface.layout.GridDataFactory;
21 import org.eclipse.jface.layout.GridLayoutFactory;
22 import org.eclipse.jface.wizard.WizardPage;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.CCombo;
25 import org.eclipse.swt.events.ModifyEvent;
26 import org.eclipse.swt.events.ModifyListener;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.FileDialog;
34 import org.eclipse.swt.widgets.Label;
35 import org.simantics.databoard.Bindings;
36 import org.simantics.db.ReadGraph;
37 import org.simantics.db.Resource;
38 import org.simantics.db.common.request.UniqueRead;
39 import org.simantics.db.exception.DatabaseException;
40 import org.simantics.db.layer0.SelectionHints;
41 import org.simantics.db.layer0.util.DraftStatusBean;
42 import org.simantics.layer0.Layer0;
43 import org.simantics.modeling.ModelingUtils;
44 import org.simantics.modeling.ModelingUtils.LibraryInfo;
45 import org.simantics.utils.strings.AlphanumComparator;
46 import org.simantics.utils.ui.ISelectionUtils;
47
48 /**
49  * @author Antti Villberg
50  */
51 public class ModelExportPage extends WizardPage {
52
53     ExportPlan          exportModel;
54     Composite           draft;
55     CCombo              model;
56     CCombo              exportLocation;
57
58     List<LibraryInfo> models = Collections.emptyList();
59     private Button      overwrite;
60
61     protected ModelExportPage(ExportPlan model) {
62         super("Export Model", "Define Export Location", null);
63         this.exportModel = model;
64     }
65
66     @Override
67     public void createControl(Composite parent) {
68         Composite container = new Composite(parent, SWT.NONE);
69         {
70             GridLayout layout = new GridLayout();
71             layout.horizontalSpacing = 20;
72             layout.verticalSpacing = 10;
73             layout.numColumns = 3;
74             container.setLayout(layout);
75         }
76
77         draft = new Composite(container, SWT.NONE);
78         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);
79         draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));
80         GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
81
82         Composite draft2 = new Composite(draft, SWT.NONE);
83         GridLayoutFactory.swtDefaults().applyTo(draft2);
84         GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);
85         Label draftText = new Label(draft2, SWT.WRAP);
86         draftText.setText("Some dependencies of the selected model are not published. The model can only be saved with draft status.");
87
88         new Label(container, SWT.NONE).setText("Exported &model:");
89         model = new CCombo(container, SWT.BORDER);
90         {
91             model.setEditable(false);
92             model.setText("");
93             model.setToolTipText("Selects the model to export.");
94             GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);
95             model.addSelectionListener(new SelectionAdapter() {
96                 @Override
97                 public void widgetSelected(SelectionEvent e) {
98                     int selected = model.getSelectionIndex();
99                     if (selected != -1) {
100                         exportModel.model = (LibraryInfo) model.getData(String.valueOf(selected)); 
101                         validatePage();
102                     }
103                 }
104             });
105         }
106
107         new Label(container, SWT.NONE).setText("&Target file:");
108         exportLocation = new CCombo(container, SWT.BORDER);
109         {
110             exportLocation.setText("");
111             GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
112             exportLocation.addModifyListener(new ModifyListener(){
113                 @Override
114                 public void modifyText(ModifyEvent e) {
115                     validatePage();
116                 }
117             });
118         }
119         Button browseFileButton = new Button(container, SWT.PUSH);
120         {
121             browseFileButton.setText("Browse...");
122             browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
123             browseFileButton.addSelectionListener(new SelectionAdapter() {
124                 @Override
125                 public void widgetSelected(SelectionEvent e) {
126                     FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
127                     dialog.setText("Choose Export Target File");
128                     String loc = exportLocation.getText();
129                     dialog.setFilterPath(loc);
130                     dialog.setFilterExtensions(new String[] { "*.tg" });
131                     dialog.setFilterNames(new String[] { "Model (*.tg)" });
132                     dialog.setOverwrite(false);
133                     String file = dialog.open();
134                     if (file == null)
135                         return;
136                     exportLocation.setText(file);
137                     validatePage();
138                 }
139             });
140         }
141
142         Label horizRule = new Label(container, SWT.BORDER);
143         GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
144
145         overwrite = new Button(container, SWT.CHECK);
146         overwrite.setText("&Overwrite existing files without warning");
147         overwrite.setSelection(exportModel.overwrite);
148         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);
149         overwrite.addSelectionListener(new SelectionAdapter() {
150             @Override
151             public void widgetSelected(SelectionEvent e) {
152                 validatePage();
153             }
154         });
155
156         try {
157             initializeData();
158         } catch (DatabaseException e) {
159             e.printStackTrace();
160         }
161
162         setControl(container);
163         validatePage();
164     }
165
166     private void initializeData() throws DatabaseException {
167
168         List<Resource> selected = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
169
170         models = exportModel.sessionContext.getSession().syncRequest(new UniqueRead<List<LibraryInfo>>() {
171             @Override
172             public List<LibraryInfo> perform(ReadGraph graph) throws DatabaseException {
173                 List<LibraryInfo> result = new ArrayList<>();
174                 Layer0 L0 = Layer0.getInstance(graph);
175                 for (Resource r : selected) {
176                     String name = graph.getRelatedValue(r, L0.HasName, Bindings.STRING);
177                     DraftStatusBean draft = ModelingUtils.getDependencyDraftStatus(graph, r);
178                     result.add(new LibraryInfo(name, r, draft));
179                 }
180                 Collections.sort(result, new Comparator<LibraryInfo>() {
181                     @Override
182                     public int compare(LibraryInfo o1, LibraryInfo o2) {
183                         return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.library.getName(), o2.library.getName());
184                     }
185                 });
186                 return result;
187             }
188         });
189
190         if (!models.isEmpty())
191             exportModel.model = models.get(0);
192
193         // Populate combo boxes
194         int i = 0;
195         for (LibraryInfo m : models) {
196             model.add(m.library.getName());
197             model.setData(String.valueOf(i), m);
198             if (m.equals(exportModel.model))
199                 model.select(i);
200             ++i;
201         }
202
203         for (String path : exportModel.recentLocations) {
204             exportLocation.add(path);
205         }
206         if (exportLocation.getItemCount() > 0)
207             exportLocation.select(0);
208     }
209
210     void validatePage() {
211         if (exportModel.model == null) {
212             setMessage("Select library to export from.");
213             setErrorMessage(null);
214             setPageComplete(false);
215             return;
216         }
217
218         boolean draftVisible = draft.getVisible();
219         boolean hasDraftDependencies = exportModel.model.draft != null;
220         if (draftVisible != hasDraftDependencies) {
221             ((GridData) draft.getLayoutData()).exclude = !hasDraftDependencies;
222             draft.setVisible(hasDraftDependencies);
223             draft.getParent().getParent().layout(true, true);
224         }
225
226         String exportLoc = exportLocation.getText();
227         if (exportLoc.isEmpty()) {
228             setMessage("Select target file.");
229             setErrorMessage(null);
230             setPageComplete(false);
231             return;
232         }
233         File file = new File(exportLoc);
234         if (file.isDirectory()) {
235             setErrorMessage("The target is a directory.");
236             setPageComplete(false);
237             return;
238         }
239         File parent = file.getParentFile();
240         if (parent == null || !parent.isDirectory()) {
241             setErrorMessage("The target directory does not exist.");
242             setPageComplete(false);
243             return;
244         }
245         exportModel.exportLocation = file;
246         exportModel.overwrite = overwrite.getSelection();
247
248         setErrorMessage(null);
249         setMessage("Export selected model to " + exportModel.exportLocation + ".");
250         setPageComplete(true);
251     }
252
253 }