]> gerrit.simantics Code Review - simantics/platform.git/blob
90a155231e279dce3026c0c30126df579ac28ad1
[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.Collection;
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.concurrent.atomic.AtomicReference;
22
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.jface.layout.GridDataFactory;
25 import org.eclipse.jface.layout.GridLayoutFactory;
26 import org.eclipse.jface.wizard.WizardPage;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.custom.CCombo;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.FileDialog;
38 import org.eclipse.swt.widgets.Label;
39 import org.simantics.NameLabelMode;
40 import org.simantics.NameLabelUtil;
41 import org.simantics.Simantics;
42 import org.simantics.db.ReadGraph;
43 import org.simantics.db.Resource;
44 import org.simantics.db.Session;
45 import org.simantics.db.common.request.PossibleIndexRoot;
46 import org.simantics.db.common.request.UniqueRead;
47 import org.simantics.db.exception.DatabaseException;
48 import org.simantics.db.layer0.SelectionHints;
49 import org.simantics.db.layer0.util.DraftStatusBean;
50 import org.simantics.db.layer0.util.Layer0Utils;
51 import org.simantics.modeling.ModelingUtils.LibraryInfo;
52 import org.simantics.scl.runtime.tuple.Tuple0;
53 import org.simantics.utils.ui.ISelectionUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  * @author Antti Villberg
59  */
60 public class SharedOntologyExportPage extends WizardPage {
61
62     private static final Logger LOGGER = LoggerFactory.getLogger(SharedOntologyExportPage.class);
63
64     ExportPlan          exportModel;
65     Composite           draft;
66     CCombo              library;
67     CCombo              exportLocation;
68     private Button      overwrite;
69
70     List<LibraryInfo>   libraries = Collections.emptyList();
71
72     protected SharedOntologyExportPage(ExportPlan model) {
73         super("Export Shared Library", "Define Export Location", null);
74         this.exportModel = model;
75     }
76
77     @Override
78     public void createControl(Composite parent) {
79         Composite container = new Composite(parent, SWT.NONE);
80         {
81             GridLayout layout = new GridLayout();
82             layout.horizontalSpacing = 20;
83             layout.verticalSpacing = 10;
84             layout.numColumns = 3;
85             container.setLayout(layout);
86         }
87
88         draft = new Composite(container, SWT.NONE);
89         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);
90         draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));
91         GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
92         
93         Composite draft2 = new Composite(draft, SWT.NONE);
94         GridLayoutFactory.swtDefaults().applyTo(draft2);
95         GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);
96         new Label(draft2, SWT.NONE).setText("The shared library has not been finished for publishing. The symbol can only be saved with draft status.");
97         
98         new Label(container, SWT.NONE).setText("Exported &shared library:");
99         library = new CCombo(container, SWT.BORDER);
100         {
101             library.setEditable(false);
102             library.setText("");
103             library.setToolTipText("Selects the shared library to export.");
104             GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(library);
105             library.addSelectionListener(new SelectionAdapter() {
106                 @Override
107                 public void widgetSelected(SelectionEvent e) {
108                     int i = library.getSelectionIndex();
109                     exportModel.model = i >= 0 && libraries.size() > 0 ? libraries.get(i) : null;
110                     validatePage();
111                 }
112             });
113         }
114
115         new Label(container, SWT.NONE).setText("&Target file:");
116         exportLocation = new CCombo(container, SWT.BORDER);
117         {
118             exportLocation.setText("");
119             GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
120             exportLocation.addModifyListener(new ModifyListener(){
121                 @Override
122                 public void modifyText(ModifyEvent e) {
123                     validatePage();
124                 }
125             });
126         }
127         Button browseFileButton = new Button(container, SWT.PUSH);
128         {
129             browseFileButton.setText("Browse...");
130             browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
131             browseFileButton.addSelectionListener(new SelectionAdapter() {
132                 @Override
133                 public void widgetSelected(SelectionEvent e) {
134                     FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
135                     dialog.setText("Choose Export Target File");
136                     String loc = exportLocation.getText();
137                     dialog.setFilterPath(loc);
138                     dialog.setFilterExtensions(new String[] { "*.sharedLibrary" });
139                     dialog.setFilterNames(new String[] { "Shared Library (*.sharedLibrary)" });
140                     dialog.setOverwrite(false);
141                     String file = dialog.open();
142                     if (file == null)
143                         return;
144                     exportLocation.setText(file);
145                     validatePage();
146                 }
147             });
148         }
149
150         Label horizRule = new Label(container, SWT.BORDER);
151         GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
152
153         overwrite = new Button(container, SWT.CHECK);
154         overwrite.setText("&Overwrite existing files without warning");
155         overwrite.setSelection(exportModel.overwrite);
156         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);
157         overwrite.addSelectionListener(new SelectionAdapter() {
158             @Override
159             public void widgetSelected(SelectionEvent e) {
160                 validatePage();
161             }
162         });
163         String prop = System.getProperty("org.simantics.modeling.exportTgAndPgraph");
164         if (prop != null || Platform.inDevelopmentMode()) {
165             Button tgAndPgraph = new Button(container, SWT.CHECK);
166             tgAndPgraph.setText("&Generate TG and Pgraph with export");
167             tgAndPgraph.setSelection(exportModel.tgAndPgraph);
168             GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(tgAndPgraph);
169             tgAndPgraph.addSelectionListener(new SelectionAdapter() {
170                 @Override
171                 public void widgetSelected(SelectionEvent e) {
172                     exportModel.tgAndPgraph = tgAndPgraph.getSelection();
173                 }
174             });
175         }
176
177         try {
178             initializeData();
179         } catch (DatabaseException e) {
180             LOGGER.error("Failed to initialize shared ontology wizard export page", e);
181         }
182
183         setControl(container);
184         validatePage();
185     }
186
187     private void initializeData() throws DatabaseException {
188
189         Session session = exportModel.sessionContext.getSession();
190         List<Resource> selection = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
191         AtomicReference<Set<Resource>> selectedRoots = new AtomicReference<>();
192
193         libraries = session.syncRequest(new UniqueRead<List<LibraryInfo>>() {
194             @Override
195             public List<LibraryInfo> perform(ReadGraph graph) throws DatabaseException {
196                 List<LibraryInfo> result = new ArrayList<>();
197
198                 Set<Resource> roots = toRoots(graph, selection);
199                 selectedRoots.set(roots);
200
201                 List<Resource> libs = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
202                 for (Resource lib : libs)
203                     result.add(library(graph, lib));
204
205                 return result;
206             }
207
208             private Set<Resource> toRoots(ReadGraph graph, Collection<Resource> resources) throws DatabaseException {
209                 Set<Resource> result = new HashSet<>();
210                 for (Resource sel : selection) {
211                     Resource root = graph.syncRequest(new PossibleIndexRoot(sel));
212                     if (root != null)
213                         result.add(root);
214                 }
215                 return result;
216             }
217
218             private LibraryInfo library(ReadGraph graph, Resource library) throws DatabaseException {
219                 return new LibraryInfo(
220                         NameLabelUtil.modalName(graph, library, NameLabelMode.NAME),
221                         library,
222                         isDraft(graph, library));
223             }
224
225             private DraftStatusBean isDraft(ReadGraph graph, Resource resource) throws DatabaseException {
226                 return Layer0Utils.isPublished(graph, resource) ? null : new DraftStatusBean(new String[0]);
227             }
228         });
229
230         for (LibraryInfo lib : libraries) {
231             if (selectedRoots.get().contains(lib.library.getResource()))
232                 exportModel.model = lib;
233         }
234
235         // Populate combo boxes
236         int i = 0;
237         for (LibraryInfo m : libraries) {
238             library.add(m.library.getName());
239             library.setData(String.valueOf(i), m);
240             if (m.equals(exportModel.model))
241                 library.select(i);
242             ++i;
243         }
244
245         for (String path : exportModel.recentLocations) {
246             exportLocation.add(path);
247         }
248         if (exportLocation.getItemCount() > 0)
249             exportLocation.select(0);
250     }
251
252     void validatePage() {
253         if (exportModel.model == null) {
254             setMessage("Select library to export from.");
255             setErrorMessage(null);
256             setPageComplete(false);
257             return;
258         }
259
260         if(exportModel.model.draft != null) {
261             GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
262             draft.getParent().layout(true);
263         } else {
264             GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
265             draft.getParent().layout(true);
266         }
267
268         String exportLoc = exportLocation.getText();
269         if (exportLoc.isEmpty()) {
270             setMessage("Select target file.");
271             setErrorMessage(null);
272             setPageComplete(false);
273             return;
274         }
275         if (!exportLoc.endsWith(".sharedLibrary"))
276             exportLoc += exportLoc.endsWith(".") ? "sharedLibrary" : ".sharedLibrary";
277         File file = new File(exportLoc);
278         if (file.isDirectory()) {
279             setErrorMessage("The target is a directory.");
280             setPageComplete(false);
281             return;
282         }
283         File parent = file.getParentFile();
284         if (parent == null || !parent.isDirectory()) {
285             setErrorMessage("The target directory does not exist.");
286             setPageComplete(false);
287             return;
288         }
289         exportModel.exportLocation = file;
290         exportModel.overwrite = overwrite.getSelection();
291
292         setErrorMessage(null);
293         setMessage("Export shared library to " + exportLoc + ".");
294         setPageComplete(true);
295     }
296
297 }