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.io.IOException;
16 import java.util.Collections;
17 import java.util.List;
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.eclipse.swt.widgets.Text;
35 import org.simantics.databoard.Bindings;
36 import org.simantics.databoard.adapter.AdaptException;
37 import org.simantics.databoard.binding.mutable.Variant;
38 import org.simantics.databoard.container.DataContainer;
39 import org.simantics.databoard.container.DataContainers;
40 import org.simantics.db.common.NamedResource;
41 import org.simantics.db.exception.DatabaseException;
42 import org.simantics.db.layer0.util.DraftStatusBean;
43 import org.simantics.utils.ui.ErrorLogger;
46 * @author Tuukka Lehtonen
48 public class ModelImportPage extends WizardPage {
51 * If non-null, the wizard cannot continue. This message tells why.
55 ImportPlan importModel;
60 CCombo importLocation;
62 List<NamedResource> models = Collections.emptyList();
64 private Button dependencies;
69 protected ModelImportPage(ImportPlan model) {
70 super("Import Model", "Define Import Location", null);
71 this.importModel = model;
75 public void createControl(Composite parent) {
76 Composite container = new Composite(parent, SWT.NONE);
78 GridLayout layout = new GridLayout();
79 layout.horizontalSpacing = 20;
80 layout.verticalSpacing = 10;
81 layout.numColumns = 3;
82 container.setLayout(layout);
85 draft = new Composite(container, SWT.NONE);
86 draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));
87 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);
88 GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
90 Composite draft2 = new Composite(draft, SWT.NONE);
91 GridLayoutFactory.swtDefaults().applyTo(draft2);
92 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);
93 new Label(draft2, SWT.NONE).setText("This model draft was not finished for publishing.");
95 new Label(container, SWT.NONE).setText("&Model file:");
96 importLocation = new CCombo(container, SWT.BORDER);
98 importLocation.setText("");
99 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation);
100 importLocation.addModifyListener(new ModifyListener(){
102 public void modifyText(ModifyEvent e) {
107 Button browseFileButton = new Button(container, SWT.PUSH);
109 browseFileButton.setText("Br&owse...");
110 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
111 browseFileButton.addSelectionListener(new SelectionAdapter() {
113 public void widgetSelected(SelectionEvent e) {
114 FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
115 dialog.setText("Choose Model to Import");
116 String loc = importLocation.getText();
117 dialog.setFilterPath(loc);
118 dialog.setFilterExtensions(new String[] { "*.tg" });
119 dialog.setFilterNames(new String[] { "Model (*.tg)" });
120 String file = dialog.open();
123 importLocation.setText(file);
129 dependencies = new Button(container, SWT.CHECK);
130 dependencies.setText("&Import dependencies");
131 dependencies.setSelection(importModel.includeDependencies);
132 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(dependencies);
133 dependencies.addSelectionListener(new SelectionAdapter() {
135 public void widgetSelected(SelectionEvent e) {
140 author = new Label(container, SWT.NONE);
142 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(author);
144 status = new Label(container, SWT.NONE);
146 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(status);
150 } catch (DatabaseException e) {
151 ErrorLogger.defaultLogError(e);
154 setControl(container);
158 private void initializeData() throws DatabaseException {
159 for (String path : importModel.recentLocations) {
160 importLocation.add(path);
162 if (importLocation.getItemCount() > 0)
163 importLocation.select(0);
166 void validatePage() {
168 if (failure != null) {
169 setErrorMessage(failure);
170 setPageComplete(false);
173 String importLoc = importLocation.getText();
174 if (importLoc.isEmpty()) {
175 setMessage("Select file to import.");
176 setErrorMessage(null);
177 setPageComplete(false);
180 File file = new File(importLoc);
181 if (!file.exists() || !file.isFile()) {
182 setErrorMessage("Selected file is invalid.");
183 setPageComplete(false);
186 importModel.importLocation = file;
187 importModel.includeDependencies = dependencies.getSelection();
191 DataContainer container = DataContainers.readHeader(file);
192 Variant draftStatus = container.metadata.get(DraftStatusBean.EXTENSION_KEY);
193 if(draftStatus != null) {
194 GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
195 draft.getParent().layout(true);
197 GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
198 draft.getParent().layout(true);
201 Variant authorVariant = container.metadata.get("author");
202 Variant dateVariant = container.metadata.get("date");
204 if(authorVariant != null && dateVariant != null) {
205 String auth = (String)authorVariant.getValue(Bindings.STRING);
206 String date = (String)dateVariant.getValue(Bindings.STRING);
207 author.setText("Created by " + auth + " on " + date);
212 } catch (IOException e) {
213 setErrorMessage("Could not read header information from " + file.getAbsolutePath());
214 setPageComplete(false);
216 } catch (AdaptException e) {
217 setErrorMessage("Could not read header information from " + file.getAbsolutePath());
218 setPageComplete(false);
222 setErrorMessage(null);
223 setMessage("Ready to import " + file.getName() + "");
224 setPageComplete(true);