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();
66 protected ModelImportPage(ImportPlan model) {
67 super("Import Model", "Define Import Location", null);
68 this.importModel = model;
72 public void createControl(Composite parent) {
73 Composite container = new Composite(parent, SWT.NONE);
75 GridLayout layout = new GridLayout();
76 layout.horizontalSpacing = 20;
77 layout.verticalSpacing = 10;
78 layout.numColumns = 3;
79 container.setLayout(layout);
82 draft = new Composite(container, SWT.NONE);
83 draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));
84 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);
85 GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
87 Composite draft2 = new Composite(draft, SWT.NONE);
88 GridLayoutFactory.swtDefaults().applyTo(draft2);
89 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);
90 new Label(draft2, SWT.NONE).setText("This model draft was not finished for publishing.");
92 new Label(container, SWT.NONE).setText("&Model file:");
93 importLocation = new CCombo(container, SWT.BORDER);
95 importLocation.setText("");
96 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation);
97 importLocation.addModifyListener(new ModifyListener(){
99 public void modifyText(ModifyEvent e) {
104 Button browseFileButton = new Button(container, SWT.PUSH);
106 browseFileButton.setText("Br&owse...");
107 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
108 browseFileButton.addSelectionListener(new SelectionAdapter() {
110 public void widgetSelected(SelectionEvent e) {
111 FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
112 dialog.setText("Choose Model to Import");
113 String loc = importLocation.getText();
114 dialog.setFilterPath(loc);
115 dialog.setFilterExtensions(new String[] { "*.tg" });
116 dialog.setFilterNames(new String[] { "Model (*.tg)" });
117 String file = dialog.open();
120 importLocation.setText(file);
126 author = new Label(container, SWT.NONE);
128 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(author);
130 status = new Label(container, SWT.NONE);
132 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(status);
136 } catch (DatabaseException e) {
137 ErrorLogger.defaultLogError(e);
140 setControl(container);
144 private void initializeData() throws DatabaseException {
145 for (String path : importModel.recentLocations) {
146 importLocation.add(path);
148 if (importLocation.getItemCount() > 0)
149 importLocation.select(0);
152 void validatePage() {
154 if (failure != null) {
155 setErrorMessage(failure);
156 setPageComplete(false);
159 String importLoc = importLocation.getText();
160 if (importLoc.isEmpty()) {
161 setMessage("Select file to import.");
162 setErrorMessage(null);
163 setPageComplete(false);
166 File file = new File(importLoc);
167 if (!file.exists() || !file.isFile()) {
168 setErrorMessage("Selected file is invalid.");
169 setPageComplete(false);
172 importModel.importLocation = file;
176 DataContainer container = DataContainers.readHeader(file);
177 Variant draftStatus = container.metadata.get(DraftStatusBean.EXTENSION_KEY);
178 if(draftStatus != null) {
179 GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
180 draft.getParent().layout(true);
182 GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
183 draft.getParent().layout(true);
186 Variant authorVariant = container.metadata.get("author");
187 Variant dateVariant = container.metadata.get("date");
189 if(authorVariant != null && dateVariant != null) {
190 String auth = (String)authorVariant.getValue(Bindings.STRING);
191 String date = (String)dateVariant.getValue(Bindings.STRING);
192 author.setText("Created by " + auth + " on " + date);
197 } catch (IOException e) {
198 setErrorMessage("Could not read header information from " + file.getAbsolutePath());
199 setPageComplete(false);
201 } catch (AdaptException e) {
202 setErrorMessage("Could not read header information from " + file.getAbsolutePath());
203 setPageComplete(false);
207 setErrorMessage(null);
208 setMessage("Ready to import " + file.getName() + "");
209 setPageComplete(true);