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.Collections;
16 import java.util.List;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.jface.layout.GridDataFactory;
20 import org.eclipse.jface.layout.GridLayoutFactory;
21 import org.eclipse.jface.wizard.WizardPage;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.custom.CCombo;
24 import org.eclipse.swt.events.ModifyEvent;
25 import org.eclipse.swt.events.ModifyListener;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.FileDialog;
33 import org.eclipse.swt.widgets.Label;
34 import org.simantics.databoard.Bindings;
35 import org.simantics.db.ReadGraph;
36 import org.simantics.db.Resource;
37 import org.simantics.db.common.primitiverequest.RelatedValue;
38 import org.simantics.db.common.request.ResourceRead;
39 import org.simantics.db.exception.DatabaseException;
40 import org.simantics.db.layer0.SelectionHints;
41 import org.simantics.db.layer0.util.DraftStatusBean;
42 import org.simantics.db.layer0.util.Layer0Utils;
43 import org.simantics.layer0.Layer0;
44 import org.simantics.modeling.ModelingUtils.LibraryInfo;
45 import org.simantics.utils.ui.ISelectionUtils;
48 * @author Antti Villberg
50 public class SharedOntologyExportPage extends WizardPage {
52 ExportPlan exportModel;
55 CCombo exportLocation;
57 List<LibraryInfo> models = Collections.emptyList();
58 private Button overwrite;
60 protected SharedOntologyExportPage(ExportPlan model) {
61 super("Export Shared Library", "Define Export Location", null);
62 this.exportModel = model;
66 public void createControl(Composite parent) {
67 Composite container = new Composite(parent, SWT.NONE);
69 GridLayout layout = new GridLayout();
70 layout.horizontalSpacing = 20;
71 layout.verticalSpacing = 10;
72 layout.numColumns = 3;
73 container.setLayout(layout);
76 draft = new Composite(container, SWT.NONE);
77 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);
78 draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));
79 GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
81 Composite draft2 = new Composite(draft, SWT.NONE);
82 GridLayoutFactory.swtDefaults().applyTo(draft2);
83 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);
84 new Label(draft2, SWT.NONE).setText("The shared library has not been finished for publishing. The symbol can only be saved with draft status.");
86 new Label(container, SWT.NONE).setText("Exported &shared library:");
87 model = new CCombo(container, SWT.BORDER);
89 model.setEditable(false);
91 model.setToolTipText("Selects the shared library to export.");
92 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);
93 model.addModifyListener(new ModifyListener(){
95 public void modifyText(ModifyEvent e) {
101 new Label(container, SWT.NONE).setText("&Target file:");
102 exportLocation = new CCombo(container, SWT.BORDER);
104 exportLocation.setText("");
105 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
106 exportLocation.addModifyListener(new ModifyListener(){
108 public void modifyText(ModifyEvent e) {
113 Button browseFileButton = new Button(container, SWT.PUSH);
115 browseFileButton.setText("Browse...");
116 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
117 browseFileButton.addSelectionListener(new SelectionAdapter() {
119 public void widgetSelected(SelectionEvent e) {
120 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
121 dialog.setText("Choose Export Target File");
122 String loc = exportLocation.getText();
123 dialog.setFilterPath(loc);
124 dialog.setFilterExtensions(new String[] { "*.sharedLibrary" });
125 dialog.setFilterNames(new String[] { "Shared Library (*.sharedLibrary)" });
126 dialog.setOverwrite(false);
127 String file = dialog.open();
130 exportLocation.setText(file);
136 Label horizRule = new Label(container, SWT.BORDER);
137 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
139 overwrite = new Button(container, SWT.CHECK);
140 overwrite.setText("&Overwrite existing files without warning");
141 overwrite.setSelection(exportModel.overwrite);
142 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);
143 overwrite.addSelectionListener(new SelectionAdapter() {
145 public void widgetSelected(SelectionEvent e) {
149 String prop = System.getProperty("org.simantics.modeling.exportTgAndPgraph");
150 if (prop != null || Platform.inDevelopmentMode()) {
151 Button tgAndPgraph = new Button(container, SWT.CHECK);
152 tgAndPgraph.setText("&Generate TG and Pgraph with export");
153 tgAndPgraph.setSelection(exportModel.tgAndPgraph);
154 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(tgAndPgraph);
155 tgAndPgraph.addSelectionListener(new SelectionAdapter() {
157 public void widgetSelected(SelectionEvent e) {
158 exportModel.tgAndPgraph = tgAndPgraph.getSelection();
166 } catch (DatabaseException e) {
170 setControl(container);
174 private void initializeData() throws DatabaseException {
176 List<Resource> libraries = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
177 if(libraries.size() != 1) throw new RuntimeException();
179 Layer0 L0 = Layer0.getInstance(exportModel.sessionContext.getSession());
180 String name = exportModel.sessionContext.getSession().sync(new RelatedValue<String>(libraries.get(0), L0.HasName, Bindings.STRING));
182 DraftStatusBean draft = exportModel.sessionContext.getSession().syncRequest(new ResourceRead<DraftStatusBean>(libraries.get(0)) {
185 public DraftStatusBean perform(ReadGraph graph) throws DatabaseException {
186 boolean published = Layer0Utils.isPublished(graph, resource);
187 if(!published) return new DraftStatusBean(new String[0]);
193 // Load all states in the selected model
194 exportModel.model = new LibraryInfo(name, libraries.get(0), draft);
195 models = Collections.singletonList(exportModel.model);
197 // Populate combo boxes
199 for (LibraryInfo m : models) {
200 model.add(m.library.getName());
201 model.setData(String.valueOf(i), m);
202 if (m.equals(exportModel.model))
207 for (String path : exportModel.recentLocations) {
208 exportLocation.add(path);
210 if (exportLocation.getItemCount() > 0)
211 exportLocation.select(0);
214 void validatePage() {
215 if (exportModel.model == null) {
216 setMessage("Select library to export from.");
217 setErrorMessage(null);
218 setPageComplete(false);
222 if(exportModel.model.draft != null) {
223 GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
224 draft.getParent().layout(true);
226 GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
227 draft.getParent().layout(true);
230 String exportLoc = exportLocation.getText();
231 if (exportLoc.isEmpty()) {
232 setMessage("Select target file.");
233 setErrorMessage(null);
234 setPageComplete(false);
237 if (!exportLoc.endsWith(".sharedLibrary"))
238 exportLoc += exportLoc.endsWith(".") ? "sharedLibrary" : ".sharedLibrary";
239 File file = new File(exportLoc);
240 if (file.isDirectory()) {
241 setErrorMessage("The target is a directory.");
242 setPageComplete(false);
245 File parent = file.getParentFile();
246 if (parent == null || !parent.isDirectory()) {
247 setErrorMessage("The target directory does not exist.");
248 setPageComplete(false);
251 exportModel.exportLocation = file;
252 exportModel.overwrite = overwrite.getSelection();
254 setErrorMessage(null);
255 setMessage("Export shared library to " + exportLoc + ".");
256 setPageComplete(true);