]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportPage.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sharedontology / wizard / ModelImportPage.java
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
64     private Button      dependencies;
65
66     Label               author;
67     Label               status;
68
69     protected ModelImportPage(ImportPlan model) {
70         super("Import Model", "Define Import Location", null);
71         this.importModel = model;
72     }
73
74     @Override
75     public void createControl(Composite parent) {
76         Composite container = new Composite(parent, SWT.NONE);
77         {
78             GridLayout layout = new GridLayout();
79             layout.horizontalSpacing = 20;
80             layout.verticalSpacing = 10;
81             layout.numColumns = 3;
82             container.setLayout(layout);
83         }
84
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);
89         
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.");
94         
95         new Label(container, SWT.NONE).setText("&Model file:");
96         importLocation = new CCombo(container, SWT.BORDER);
97         {
98             importLocation.setText("");
99             GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation);
100             importLocation.addModifyListener(new ModifyListener(){
101                 @Override
102                 public void modifyText(ModifyEvent e) {
103                     validatePage();
104                 }
105             });
106         }
107         Button browseFileButton = new Button(container, SWT.PUSH);
108         {
109             browseFileButton.setText("Br&owse...");
110             browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
111             browseFileButton.addSelectionListener(new SelectionAdapter() {
112                 @Override
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();
121                     if (file == null)
122                         return;
123                     importLocation.setText(file);
124                     validatePage();
125                 }
126             });
127         }
128         
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() {
134             @Override
135             public void widgetSelected(SelectionEvent e) {
136                 validatePage();
137             }
138         });
139         
140         author = new Label(container, SWT.NONE);
141         author.setText("");
142         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(author);
143
144         status = new Label(container, SWT.NONE);
145         status.setText("");
146         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(status);
147
148         try {
149             initializeData();
150         } catch (DatabaseException e) {
151             ErrorLogger.defaultLogError(e);
152         }
153
154         setControl(container);
155         validatePage();
156     }
157
158     private void initializeData() throws DatabaseException {
159         for (String path : importModel.recentLocations) {
160             importLocation.add(path);
161         }
162         if (importLocation.getItemCount() > 0)
163             importLocation.select(0);
164     }
165
166     void validatePage() {
167         
168         if (failure != null) {
169             setErrorMessage(failure);
170             setPageComplete(false);
171             return;
172         }
173         String importLoc = importLocation.getText();
174         if (importLoc.isEmpty()) {
175             setMessage("Select file to import.");
176             setErrorMessage(null);
177             setPageComplete(false);
178             return;
179         }
180         File file = new File(importLoc);
181         if (!file.exists() || !file.isFile()) {
182             setErrorMessage("Selected file is invalid.");
183             setPageComplete(false);
184             return;
185         }
186         importModel.importLocation = file;
187         importModel.includeDependencies = dependencies.getSelection();
188
189         try {
190
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);
196                 } else {
197                         GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);
198                         draft.getParent().layout(true);
199                 }
200                 
201                 Variant authorVariant = container.metadata.get("author");
202                 Variant dateVariant = container.metadata.get("date");
203                 
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);
208                 } else {
209                         author.setText("");
210                 }
211
212         } catch (IOException e) {
213                 setErrorMessage("Could not read header information from " + file.getAbsolutePath());
214                 setPageComplete(false);
215                 return;
216         } catch (AdaptException e) {
217                 setErrorMessage("Could not read header information from " + file.getAbsolutePath());
218                 setPageComplete(false);
219                 return;
220                 }           
221         
222         setErrorMessage(null);
223         setMessage("Ready to import " + file.getName() + "");
224         setPageComplete(true);
225         
226     }
227
228 }