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.io.IOException;
\r
16 import java.util.Collections;
\r
17 import java.util.List;
\r
19 import org.eclipse.jface.layout.GridDataFactory;
\r
20 import org.eclipse.jface.layout.GridLayoutFactory;
\r
21 import org.eclipse.jface.wizard.WizardPage;
\r
22 import org.eclipse.swt.SWT;
\r
23 import org.eclipse.swt.custom.CCombo;
\r
24 import org.eclipse.swt.events.ModifyEvent;
\r
25 import org.eclipse.swt.events.ModifyListener;
\r
26 import org.eclipse.swt.events.SelectionAdapter;
\r
27 import org.eclipse.swt.events.SelectionEvent;
\r
28 import org.eclipse.swt.layout.GridData;
\r
29 import org.eclipse.swt.layout.GridLayout;
\r
30 import org.eclipse.swt.widgets.Button;
\r
31 import org.eclipse.swt.widgets.Composite;
\r
32 import org.eclipse.swt.widgets.FileDialog;
\r
33 import org.eclipse.swt.widgets.Label;
\r
34 import org.eclipse.swt.widgets.Text;
\r
35 import org.simantics.databoard.Bindings;
\r
36 import org.simantics.databoard.adapter.AdaptException;
\r
37 import org.simantics.databoard.binding.mutable.Variant;
\r
38 import org.simantics.databoard.container.DataContainer;
\r
39 import org.simantics.databoard.container.DataContainers;
\r
40 import org.simantics.db.common.NamedResource;
\r
41 import org.simantics.db.exception.DatabaseException;
\r
42 import org.simantics.db.layer0.util.DraftStatusBean;
\r
43 import org.simantics.utils.ui.ErrorLogger;
\r
46 * @author Tuukka Lehtonen
\r
48 public class SharedOntologyImportPage extends WizardPage {
\r
51 * If non-null, the wizard cannot continue. This message tells why.
\r
55 ImportPlan importModel;
\r
60 CCombo importLocation;
\r
62 List<NamedResource> models = Collections.emptyList();
\r
66 protected SharedOntologyImportPage(ImportPlan model) {
\r
67 super("Import Shared Library", "Define Import Location", null);
\r
68 this.importModel = model;
\r
72 public void createControl(Composite parent) {
\r
73 Composite container = new Composite(parent, SWT.NONE);
\r
75 GridLayout layout = new GridLayout();
\r
76 layout.horizontalSpacing = 20;
\r
77 layout.verticalSpacing = 10;
\r
78 layout.numColumns = 3;
\r
79 container.setLayout(layout);
\r
82 draft = new Composite(container, SWT.NONE);
\r
83 draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));
\r
84 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);
\r
85 GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
\r
87 Composite draft2 = new Composite(draft, SWT.NONE);
\r
88 GridLayoutFactory.swtDefaults().applyTo(draft2);
\r
89 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);
\r
90 new Label(draft2, SWT.NONE).setText("This library draft was not finished for publishing.");
\r
92 new Label(container, SWT.NONE).setText("&Shared library file:");
\r
93 importLocation = new CCombo(container, SWT.BORDER);
\r
95 importLocation.setText("");
\r
96 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation);
\r
97 importLocation.addModifyListener(new ModifyListener(){
\r
99 public void modifyText(ModifyEvent e) {
\r
104 Button browseFileButton = new Button(container, SWT.PUSH);
\r
106 browseFileButton.setText("Br&owse...");
\r
107 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
\r
108 browseFileButton.addSelectionListener(new SelectionAdapter() {
\r
110 public void widgetSelected(SelectionEvent e) {
\r
111 FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
\r
112 dialog.setText("Choose Shared Library to Import");
\r
113 String loc = importLocation.getText();
\r
114 dialog.setFilterPath(loc);
\r
115 dialog.setFilterExtensions(new String[] { "*.sharedLibrary" });
\r
116 dialog.setFilterNames(new String[] { "Shared Library (*.sharedLibrary)" });
\r
117 String file = dialog.open();
\r
120 importLocation.setText(file);
\r
126 author = new Label(container, SWT.NONE);
\r
127 author.setText("");
\r
128 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(author);
\r
130 status = new Label(container, SWT.NONE);
\r
131 status.setText("");
\r
132 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(status);
\r
136 } catch (DatabaseException e) {
\r
137 ErrorLogger.defaultLogError(e);
\r
140 setControl(container);
\r
144 private void initializeData() throws DatabaseException {
\r
145 for (String path : importModel.recentLocations) {
\r
146 importLocation.add(path);
\r
148 if (importLocation.getItemCount() > 0)
\r
149 importLocation.select(0);
\r
152 void validatePage() {
\r
154 if (failure != null) {
\r
155 setErrorMessage(failure);
\r
156 setPageComplete(false);
\r
159 String importLoc = importLocation.getText();
\r
160 if (importLoc.isEmpty()) {
\r
161 setMessage("Select file to import.");
\r
162 setErrorMessage(null);
\r
163 setPageComplete(false);
\r
166 File file = new File(importLoc);
\r
167 if (!file.exists() || !file.isFile()) {
\r
168 setErrorMessage("Selected file is invalid.");
\r
169 setPageComplete(false);
\r
172 importModel.importLocation = file;
\r
176 DataContainer container = DataContainers.readHeader(file);
\r
177 Variant draftStatus = container.metadata.get(DraftStatusBean.EXTENSION_KEY);
\r
178 if(draftStatus != null) {
\r
179 GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
\r
180 draft.getParent().layout(true);
\r
182 GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
\r
183 draft.getParent().layout(true);
\r
186 Variant authorVariant = container.metadata.get("author");
\r
187 Variant dateVariant = container.metadata.get("date");
\r
189 if(authorVariant != null && dateVariant != null) {
\r
190 String auth = (String)authorVariant.getValue(Bindings.STRING);
\r
191 String date = (String)dateVariant.getValue(Bindings.STRING);
\r
192 author.setText("Created by " + auth + " on " + date);
\r
194 author.setText("");
\r
197 } catch (IOException e) {
\r
198 setErrorMessage("Could not read header information from " + file.getAbsolutePath());
\r
199 setPageComplete(false);
\r
201 } catch (AdaptException e) {
\r
202 setErrorMessage("Could not read header information from " + file.getAbsolutePath());
\r
203 setPageComplete(false);
\r
207 setErrorMessage(null);
\r
208 setMessage("Ready to import " + file.getName() + "");
\r
209 setPageComplete(true);
\r