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.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;
58 * @author Antti Villberg
60 public class SharedOntologyExportPage extends WizardPage {
62 private static final Logger LOGGER = LoggerFactory.getLogger(SharedOntologyExportPage.class);
64 ExportPlan exportModel;
67 CCombo exportLocation;
68 private Button overwrite;
70 List<LibraryInfo> libraries = Collections.emptyList();
72 protected SharedOntologyExportPage(ExportPlan model) {
73 super("Export Shared Library", "Define Export Location", null);
74 this.exportModel = model;
78 public void createControl(Composite parent) {
79 Composite container = new Composite(parent, SWT.NONE);
81 GridLayout layout = new GridLayout();
82 layout.horizontalSpacing = 20;
83 layout.verticalSpacing = 10;
84 layout.numColumns = 3;
85 container.setLayout(layout);
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);
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.");
98 new Label(container, SWT.NONE).setText("Exported &shared library:");
99 library = new CCombo(container, SWT.BORDER);
101 library.setEditable(false);
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() {
107 public void widgetSelected(SelectionEvent e) {
108 int i = library.getSelectionIndex();
109 exportModel.model = i >= 0 && libraries.size() > 0 ? libraries.get(i) : null;
115 new Label(container, SWT.NONE).setText("&Target file:");
116 exportLocation = new CCombo(container, SWT.BORDER);
118 exportLocation.setText("");
119 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
120 exportLocation.addModifyListener(new ModifyListener(){
122 public void modifyText(ModifyEvent e) {
127 Button browseFileButton = new Button(container, SWT.PUSH);
129 browseFileButton.setText("Browse...");
130 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
131 browseFileButton.addSelectionListener(new SelectionAdapter() {
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();
144 exportLocation.setText(file);
150 Label horizRule = new Label(container, SWT.BORDER);
151 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
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() {
159 public void widgetSelected(SelectionEvent e) {
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() {
171 public void widgetSelected(SelectionEvent e) {
172 exportModel.tgAndPgraph = tgAndPgraph.getSelection();
179 } catch (DatabaseException e) {
180 LOGGER.error("Failed to initialize shared ontology wizard export page", e);
183 setControl(container);
187 private void initializeData() throws DatabaseException {
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<>();
193 libraries = session.syncRequest(new UniqueRead<List<LibraryInfo>>() {
195 public List<LibraryInfo> perform(ReadGraph graph) throws DatabaseException {
196 List<LibraryInfo> result = new ArrayList<>();
198 Set<Resource> roots = toRoots(graph, selection);
199 selectedRoots.set(roots);
201 List<Resource> libs = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
202 for (Resource lib : libs)
203 result.add(library(graph, lib));
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));
218 private LibraryInfo library(ReadGraph graph, Resource library) throws DatabaseException {
219 return new LibraryInfo(
220 NameLabelUtil.modalName(graph, library, NameLabelMode.NAME),
222 isDraft(graph, library));
225 private DraftStatusBean isDraft(ReadGraph graph, Resource resource) throws DatabaseException {
226 return Layer0Utils.isPublished(graph, resource) ? null : new DraftStatusBean(new String[0]);
230 for (LibraryInfo lib : libraries) {
231 if (selectedRoots.get().contains(lib.library.getResource()))
232 exportModel.model = lib;
235 // Populate combo boxes
237 for (LibraryInfo m : libraries) {
238 library.add(m.library.getName());
239 library.setData(String.valueOf(i), m);
240 if (m.equals(exportModel.model))
245 for (String path : exportModel.recentLocations) {
246 exportLocation.add(path);
248 if (exportLocation.getItemCount() > 0)
249 exportLocation.select(0);
252 void validatePage() {
253 if (exportModel.model == null) {
254 setMessage("Select library to export from.");
255 setErrorMessage(null);
256 setPageComplete(false);
260 if(exportModel.model.draft != null) {
261 GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
262 draft.getParent().layout(true);
264 GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
265 draft.getParent().layout(true);
268 String exportLoc = exportLocation.getText();
269 if (exportLoc.isEmpty()) {
270 setMessage("Select target file.");
271 setErrorMessage(null);
272 setPageComplete(false);
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);
283 File parent = file.getParentFile();
284 if (parent == null || !parent.isDirectory()) {
285 setErrorMessage("The target directory does not exist.");
286 setPageComplete(false);
289 exportModel.exportLocation = file;
290 exportModel.overwrite = overwrite.getSelection();
292 setErrorMessage(null);
293 setMessage("Export shared library to " + exportLoc + ".");
294 setPageComplete(true);