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