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