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