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