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