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