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.jface.layout.GridDataFactory;
19 import org.eclipse.jface.layout.GridLayoutFactory;
20 import org.eclipse.jface.wizard.WizardPage;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.custom.CCombo;
23 import org.eclipse.swt.events.ModifyEvent;
24 import org.eclipse.swt.events.ModifyListener;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.FileDialog;
32 import org.eclipse.swt.widgets.Label;
33 import org.simantics.databoard.Bindings;
34 import org.simantics.db.ReadGraph;
35 import org.simantics.db.Resource;
36 import org.simantics.db.common.primitiverequest.RelatedValue;
37 import org.simantics.db.common.request.ResourceRead;
38 import org.simantics.db.exception.DatabaseException;
39 import org.simantics.db.layer0.SelectionHints;
40 import org.simantics.db.layer0.util.DraftStatusBean;
41 import org.simantics.db.layer0.util.Layer0Utils;
42 import org.simantics.layer0.Layer0;
43 import org.simantics.modeling.ModelingUtils.LibraryInfo;
44 import org.simantics.utils.ui.ISelectionUtils;
47 * @author Antti Villberg
49 public class SharedOntologyExportPage extends WizardPage {
51 ExportPlan exportModel;
54 CCombo exportLocation;
56 List<LibraryInfo> models = Collections.emptyList();
57 private Button overwrite;
59 protected SharedOntologyExportPage(ExportPlan model) {
60 super("Export Shared Library", "Define Export Location", null);
61 this.exportModel = model;
65 public void createControl(Composite parent) {
66 Composite container = new Composite(parent, SWT.NONE);
68 GridLayout layout = new GridLayout();
69 layout.horizontalSpacing = 20;
70 layout.verticalSpacing = 10;
71 layout.numColumns = 3;
72 container.setLayout(layout);
75 draft = new Composite(container, SWT.NONE);
76 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);
77 draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));
78 GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
80 Composite draft2 = new Composite(draft, SWT.NONE);
81 GridLayoutFactory.swtDefaults().applyTo(draft2);
82 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);
83 new Label(draft2, SWT.NONE).setText("The shared library has not been finished for publishing. The symbol can only be saved with draft status.");
85 new Label(container, SWT.NONE).setText("Exported &shared library:");
86 model = new CCombo(container, SWT.BORDER);
88 model.setEditable(false);
90 model.setToolTipText("Selects the shared library to export.");
91 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);
92 model.addModifyListener(new ModifyListener(){
94 public void modifyText(ModifyEvent e) {
100 new Label(container, SWT.NONE).setText("&Target file:");
101 exportLocation = new CCombo(container, SWT.BORDER);
103 exportLocation.setText("");
104 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
105 exportLocation.addModifyListener(new ModifyListener(){
107 public void modifyText(ModifyEvent e) {
112 Button browseFileButton = new Button(container, SWT.PUSH);
114 browseFileButton.setText("Browse...");
115 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
116 browseFileButton.addSelectionListener(new SelectionAdapter() {
118 public void widgetSelected(SelectionEvent e) {
119 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
120 dialog.setText("Choose Export Target File");
121 String loc = exportLocation.getText();
122 dialog.setFilterPath(loc);
123 dialog.setFilterExtensions(new String[] { "*.sharedLibrary" });
124 dialog.setFilterNames(new String[] { "Shared Library (*.sharedLibrary)" });
125 dialog.setOverwrite(false);
126 String file = dialog.open();
129 exportLocation.setText(file);
135 Label horizRule = new Label(container, SWT.BORDER);
136 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
138 overwrite = new Button(container, SWT.CHECK);
139 overwrite.setText("&Overwrite existing files without warning");
140 overwrite.setSelection(exportModel.overwrite);
141 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);
142 overwrite.addSelectionListener(new SelectionAdapter() {
144 public void widgetSelected(SelectionEvent e) {
151 } catch (DatabaseException e) {
155 setControl(container);
159 private void initializeData() throws DatabaseException {
161 List<Resource> libraries = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
162 if(libraries.size() != 1) throw new RuntimeException();
164 Layer0 L0 = Layer0.getInstance(exportModel.sessionContext.getSession());
165 String name = exportModel.sessionContext.getSession().sync(new RelatedValue<String>(libraries.get(0), L0.HasName, Bindings.STRING));
167 DraftStatusBean draft = exportModel.sessionContext.getSession().syncRequest(new ResourceRead<DraftStatusBean>(libraries.get(0)) {
170 public DraftStatusBean perform(ReadGraph graph) throws DatabaseException {
171 boolean published = Layer0Utils.isPublished(graph, resource);
172 if(!published) return new DraftStatusBean(new String[0]);
178 // Load all states in the selected model
179 exportModel.model = new LibraryInfo(name, libraries.get(0), draft);
180 models = Collections.singletonList(exportModel.model);
182 // Populate combo boxes
184 for (LibraryInfo m : models) {
185 model.add(m.library.getName());
186 model.setData(String.valueOf(i), m);
187 if (m.equals(exportModel.model))
192 for (String path : exportModel.recentLocations) {
193 exportLocation.add(path);
195 if (exportLocation.getItemCount() > 0)
196 exportLocation.select(0);
199 void validatePage() {
200 if (exportModel.model == null) {
201 setMessage("Select library to export from.");
202 setErrorMessage(null);
203 setPageComplete(false);
207 if(exportModel.model.draft != null) {
208 GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
209 draft.getParent().layout(true);
211 GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
212 draft.getParent().layout(true);
215 String exportLoc = exportLocation.getText();
216 if (exportLoc.isEmpty()) {
217 setMessage("Select target file.");
218 setErrorMessage(null);
219 setPageComplete(false);
222 if (!exportLoc.endsWith(".sharedLibrary"))
223 exportLoc += exportLoc.endsWith(".") ? "sharedLibrary" : ".sharedLibrary";
224 File file = new File(exportLoc);
225 if (file.isDirectory()) {
226 setErrorMessage("The target is a directory.");
227 setPageComplete(false);
230 File parent = file.getParentFile();
231 if (parent == null || !parent.isDirectory()) {
232 setErrorMessage("The target directory does not exist.");
233 setPageComplete(false);
236 exportModel.exportLocation = file;
237 exportModel.overwrite = overwrite.getSelection();
239 setErrorMessage(null);
240 setMessage("Export shared library to " + exportLoc + ".");
241 setPageComplete(true);