1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.sharedontology.wizard;
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;
21 import java.util.concurrent.atomic.AtomicReference;
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;
59 * @author Antti Villberg
61 public class SharedOntologyExportPage extends WizardPage {
63 private static final Logger LOGGER = LoggerFactory.getLogger(SharedOntologyExportPage.class);
65 ExportPlan exportModel;
68 CCombo exportLocation;
69 private Button overwrite;
71 List<LibraryInfo> libraries = Collections.emptyList();
73 protected SharedOntologyExportPage(ExportPlan model) {
74 super("Export Shared Library", "Define Export Location", null);
75 this.exportModel = model;
79 public void createControl(Composite parent) {
80 Composite container = new Composite(parent, SWT.NONE);
82 GridLayout layout = new GridLayout();
83 layout.horizontalSpacing = 20;
84 layout.verticalSpacing = 10;
85 layout.numColumns = 3;
86 container.setLayout(layout);
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);
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.");
99 new Label(container, SWT.NONE).setText("Exported &shared library:");
100 library = new CCombo(container, SWT.BORDER);
102 library.setEditable(false);
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() {
108 public void widgetSelected(SelectionEvent e) {
109 int i = library.getSelectionIndex();
110 exportModel.model = i >= 0 && libraries.size() > 0 ? libraries.get(i) : null;
116 new Label(container, SWT.NONE).setText("&Target file:");
117 exportLocation = new CCombo(container, SWT.BORDER);
119 exportLocation.setText("");
120 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
121 exportLocation.addModifyListener(new ModifyListener(){
123 public void modifyText(ModifyEvent e) {
128 Button browseFileButton = new Button(container, SWT.PUSH);
130 browseFileButton.setText("Browse...");
131 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
132 browseFileButton.addSelectionListener(new SelectionAdapter() {
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();
145 exportLocation.setText(file);
151 Label horizRule = new Label(container, SWT.BORDER);
152 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
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() {
160 public void widgetSelected(SelectionEvent e) {
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();
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();
184 } catch (DatabaseException e) {
185 LOGGER.error("Failed to initialize shared ontology wizard export page", e);
188 setControl(container);
192 private void initializeData() throws DatabaseException {
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<>();
198 libraries = session.syncRequest(new UniqueRead<List<LibraryInfo>>() {
200 public List<LibraryInfo> perform(ReadGraph graph) throws DatabaseException {
201 List<LibraryInfo> result = new ArrayList<>();
203 Set<Resource> roots = toRoots(graph, selection);
204 selectedRoots.set(roots);
206 List<Resource> libs = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
207 for (Resource lib : libs)
208 result.add(library(graph, lib));
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));
223 private LibraryInfo library(ReadGraph graph, Resource library) throws DatabaseException {
224 return new LibraryInfo(
225 NameLabelUtil.modalName(graph, library, NameLabelMode.NAME),
227 isDraft(graph, library));
230 private DraftStatusBean isDraft(ReadGraph graph, Resource resource) throws DatabaseException {
231 return Layer0Utils.isPublished(graph, resource) ? null : new DraftStatusBean(new String[0]);
235 for (LibraryInfo lib : libraries) {
236 if (selectedRoots.get().contains(lib.library.getResource()))
237 exportModel.model = lib;
240 // Populate combo boxes
242 for (LibraryInfo m : libraries) {
243 library.add(m.library.getName());
244 library.setData(String.valueOf(i), m);
245 if (m.equals(exportModel.model))
250 for (String path : exportModel.recentLocations) {
251 exportLocation.add(path);
253 if (exportLocation.getItemCount() > 0)
254 exportLocation.select(0);
257 void validatePage() {
258 if (exportModel.model == null) {
259 setMessage("Select library to export from.");
260 setErrorMessage(null);
261 setPageComplete(false);
265 if(exportModel.model.draft != null) {
266 GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
267 draft.getParent().layout(true);
269 GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
270 draft.getParent().layout(true);
273 String exportLoc = exportLocation.getText();
274 if (exportLoc.isEmpty()) {
275 setMessage("Select target file.");
276 setErrorMessage(null);
277 setPageComplete(false);
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);
288 File parent = file.getParentFile();
289 if (parent == null || !parent.isDirectory()) {
290 setErrorMessage("The target directory does not exist.");
291 setPageComplete(false);
294 exportModel.exportLocation = file;
295 exportModel.overwrite = overwrite.getSelection();
297 setErrorMessage(null);
298 setMessage("Export shared library to " + exportLoc + ".");
299 setPageComplete(true);