]> gerrit.simantics Code Review - simantics/platform.git/blob
f2c8423ebadcc2b9977f3f1b9e7baf2478357926
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.sharedontology.wizard;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.util.Collections;
17 import java.util.List;
18
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;
44
45 /**
46  * @author Tuukka Lehtonen
47  */
48 public class ModelImportPage extends WizardPage {
49
50     /**
51      * If non-null, the wizard cannot continue. This message tells why.
52      */
53     String              failure;
54
55     ImportPlan          importModel;
56
57     Composite           draft;
58     
59     Text                importTarget;
60     CCombo              importLocation;
61
62     List<NamedResource> models = Collections.emptyList();
63     Label               author;
64     Label               status;
65
66     protected ModelImportPage(ImportPlan model) {
67         super("Import Model", "Define Import Location", null);
68         this.importModel = model;
69     }
70
71     @Override
72     public void createControl(Composite parent) {
73         Composite container = new Composite(parent, SWT.NONE);
74         {
75             GridLayout layout = new GridLayout();
76             layout.horizontalSpacing = 20;
77             layout.verticalSpacing = 10;
78             layout.numColumns = 3;
79             container.setLayout(layout);
80         }
81
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);
86         
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.");
91         
92         new Label(container, SWT.NONE).setText("&Model file:");
93         importLocation = new CCombo(container, SWT.BORDER);
94         {
95             importLocation.setText("");
96             GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation);
97             importLocation.addModifyListener(new ModifyListener(){
98                 @Override
99                 public void modifyText(ModifyEvent e) {
100                     validatePage();
101                 }
102             });
103         }
104         Button browseFileButton = new Button(container, SWT.PUSH);
105         {
106             browseFileButton.setText("Br&owse...");
107             browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
108             browseFileButton.addSelectionListener(new SelectionAdapter() {
109                 @Override
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();
118                     if (file == null)
119                         return;
120                     importLocation.setText(file);
121                     validatePage();
122                 }
123             });
124         }
125         
126         author = new Label(container, SWT.NONE);
127         author.setText("");
128         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(author);
129
130         status = new Label(container, SWT.NONE);
131         status.setText("");
132         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(status);
133
134         try {
135             initializeData();
136         } catch (DatabaseException e) {
137             ErrorLogger.defaultLogError(e);
138         }
139
140         setControl(container);
141         validatePage();
142     }
143
144     private void initializeData() throws DatabaseException {
145         for (String path : importModel.recentLocations) {
146             importLocation.add(path);
147         }
148         if (importLocation.getItemCount() > 0)
149             importLocation.select(0);
150     }
151
152     void validatePage() {
153         
154         if (failure != null) {
155             setErrorMessage(failure);
156             setPageComplete(false);
157             return;
158         }
159         String importLoc = importLocation.getText();
160         if (importLoc.isEmpty()) {
161             setMessage("Select file to import.");
162             setErrorMessage(null);
163             setPageComplete(false);
164             return;
165         }
166         File file = new File(importLoc);
167         if (!file.exists() || !file.isFile()) {
168             setErrorMessage("Selected file is invalid.");
169             setPageComplete(false);
170             return;
171         }
172         importModel.importLocation = file;
173
174         try {
175
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);
181                 } else {
182                         GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
183                         draft.getParent().layout(true);
184                 }
185                 
186                 Variant authorVariant = container.metadata.get("author");
187                 Variant dateVariant = container.metadata.get("date");
188                 
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);
193                 } else {
194                         author.setText("");
195                 }
196
197         } catch (IOException e) {
198                 setErrorMessage("Could not read header information from " + file.getAbsolutePath());
199                 setPageComplete(false);
200                 return;
201         } catch (AdaptException e) {
202                 setErrorMessage("Could not read header information from " + file.getAbsolutePath());
203                 setPageComplete(false);
204                 return;
205                 }           
206         
207         setErrorMessage(null);
208         setMessage("Ready to import " + file.getName() + "");
209         setPageComplete(true);
210         
211     }
212
213 }