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