]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyExportPage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sharedontology / wizard / SharedOntologyExportPage.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.util.Collections;\r
16 import java.util.List;\r
17 \r
18 import org.eclipse.jface.layout.GridDataFactory;\r
19 import org.eclipse.jface.layout.GridLayoutFactory;\r
20 import org.eclipse.jface.wizard.WizardPage;\r
21 import org.eclipse.swt.SWT;\r
22 import org.eclipse.swt.custom.CCombo;\r
23 import org.eclipse.swt.events.ModifyEvent;\r
24 import org.eclipse.swt.events.ModifyListener;\r
25 import org.eclipse.swt.events.SelectionAdapter;\r
26 import org.eclipse.swt.events.SelectionEvent;\r
27 import org.eclipse.swt.layout.GridData;\r
28 import org.eclipse.swt.layout.GridLayout;\r
29 import org.eclipse.swt.widgets.Button;\r
30 import org.eclipse.swt.widgets.Composite;\r
31 import org.eclipse.swt.widgets.FileDialog;\r
32 import org.eclipse.swt.widgets.Label;\r
33 import org.simantics.databoard.Bindings;\r
34 import org.simantics.db.ReadGraph;\r
35 import org.simantics.db.Resource;\r
36 import org.simantics.db.common.primitiverequest.RelatedValue;\r
37 import org.simantics.db.common.request.ResourceRead;\r
38 import org.simantics.db.exception.DatabaseException;\r
39 import org.simantics.db.layer0.SelectionHints;\r
40 import org.simantics.db.layer0.util.DraftStatusBean;\r
41 import org.simantics.db.layer0.util.Layer0Utils;\r
42 import org.simantics.layer0.Layer0;\r
43 import org.simantics.modeling.ModelingUtils.LibraryInfo;\r
44 import org.simantics.utils.ui.ISelectionUtils;\r
45 \r
46 /**\r
47  * @author Antti Villberg\r
48  */\r
49 public class SharedOntologyExportPage extends WizardPage {\r
50 \r
51     ExportPlan          exportModel;\r
52     Composite           draft;\r
53     CCombo              model;\r
54     CCombo              exportLocation;\r
55 \r
56     List<LibraryInfo> models = Collections.emptyList();\r
57     private Button      overwrite;\r
58 \r
59     protected SharedOntologyExportPage(ExportPlan model) {\r
60         super("Export Shared Library", "Define Export Location", null);\r
61         this.exportModel = model;\r
62     }\r
63 \r
64     @Override\r
65     public void createControl(Composite parent) {\r
66         Composite container = new Composite(parent, SWT.NONE);\r
67         {\r
68             GridLayout layout = new GridLayout();\r
69             layout.horizontalSpacing = 20;\r
70             layout.verticalSpacing = 10;\r
71             layout.numColumns = 3;\r
72             container.setLayout(layout);\r
73         }\r
74 \r
75         draft = new Composite(container, SWT.NONE);\r
76         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);\r
77         draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));\r
78         GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);\r
79         \r
80         Composite draft2 = new Composite(draft, SWT.NONE);\r
81         GridLayoutFactory.swtDefaults().applyTo(draft2);\r
82         GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);\r
83         new Label(draft2, SWT.NONE).setText("The shared library has not been finished for publishing. The symbol can only be saved with draft status.");\r
84         \r
85         new Label(container, SWT.NONE).setText("Exported &shared library:");\r
86         model = new CCombo(container, SWT.BORDER);\r
87         {\r
88             model.setEditable(false);\r
89             model.setText("");\r
90             model.setToolTipText("Selects the shared library to export.");\r
91             GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);\r
92             model.addModifyListener(new ModifyListener(){\r
93                 @Override\r
94                 public void modifyText(ModifyEvent e) {\r
95                     validatePage();\r
96                 }\r
97             });\r
98         }\r
99 \r
100         new Label(container, SWT.NONE).setText("&Target file:");\r
101         exportLocation = new CCombo(container, SWT.BORDER);\r
102         {\r
103             exportLocation.setText("");\r
104             GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);\r
105             exportLocation.addModifyListener(new ModifyListener(){\r
106                 @Override\r
107                 public void modifyText(ModifyEvent e) {\r
108                     validatePage();\r
109                 }\r
110             });\r
111         }\r
112         Button browseFileButton = new Button(container, SWT.PUSH);\r
113         {\r
114             browseFileButton.setText("Browse...");\r
115             browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));\r
116             browseFileButton.addSelectionListener(new SelectionAdapter() {\r
117                 @Override\r
118                 public void widgetSelected(SelectionEvent e) {\r
119                     FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);\r
120                     dialog.setText("Choose Export Target File");\r
121                     String loc = exportLocation.getText();\r
122                     dialog.setFilterPath(loc);\r
123                     dialog.setFilterExtensions(new String[] { "*.sharedLibrary" });\r
124                     dialog.setFilterNames(new String[] { "Shared Library (*.sharedLibrary)" });\r
125                     dialog.setOverwrite(false);\r
126                     String file = dialog.open();\r
127                     if (file == null)\r
128                         return;\r
129                     exportLocation.setText(file);\r
130                     validatePage();\r
131                 }\r
132             });\r
133         }\r
134 \r
135         Label horizRule = new Label(container, SWT.BORDER);\r
136         GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);\r
137 \r
138         overwrite = new Button(container, SWT.CHECK);\r
139         overwrite.setText("&Overwrite existing files without warning");\r
140         overwrite.setSelection(exportModel.overwrite);\r
141         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);\r
142         overwrite.addSelectionListener(new SelectionAdapter() {\r
143             @Override\r
144             public void widgetSelected(SelectionEvent e) {\r
145                 validatePage();\r
146             }\r
147         });\r
148 \r
149         try {\r
150             initializeData();\r
151         } catch (DatabaseException e) {\r
152             e.printStackTrace();\r
153         }\r
154 \r
155         setControl(container);\r
156         validatePage();\r
157     }\r
158 \r
159     private void initializeData() throws DatabaseException {\r
160 \r
161         List<Resource> libraries = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);\r
162         if(libraries.size() != 1) throw new RuntimeException();\r
163 \r
164         Layer0 L0 = Layer0.getInstance(exportModel.sessionContext.getSession());\r
165         String name = exportModel.sessionContext.getSession().sync(new RelatedValue<String>(libraries.get(0), L0.HasName, Bindings.STRING));\r
166 \r
167         DraftStatusBean draft = exportModel.sessionContext.getSession().syncRequest(new ResourceRead<DraftStatusBean>(libraries.get(0)) {\r
168 \r
169                         @Override\r
170                         public DraftStatusBean perform(ReadGraph graph) throws DatabaseException {\r
171                 boolean published = Layer0Utils.isPublished(graph, resource);\r
172                 if(!published) return new DraftStatusBean(new String[0]);\r
173                                 return null;\r
174                         }\r
175                 \r
176         });\r
177         \r
178         // Load all states in the selected model\r
179         exportModel.model = new LibraryInfo(name, libraries.get(0), draft);\r
180         models = Collections.singletonList(exportModel.model);\r
181 \r
182         // Populate combo boxes\r
183         int i = 0;\r
184         for (LibraryInfo m : models) {\r
185             model.add(m.library.getName());\r
186             model.setData(String.valueOf(i), m);\r
187             if (m.equals(exportModel.model))\r
188                 model.select(i);\r
189             ++i;\r
190         }\r
191 \r
192         for (String path : exportModel.recentLocations) {\r
193             exportLocation.add(path);\r
194         }\r
195         if (exportLocation.getItemCount() > 0)\r
196             exportLocation.select(0);\r
197     }\r
198 \r
199     void validatePage() {\r
200         if (exportModel.model == null) {\r
201             setMessage("Select library to export from.");\r
202             setErrorMessage(null);\r
203             setPageComplete(false);\r
204             return;\r
205         }\r
206 \r
207         if(exportModel.model.draft != null) {\r
208             GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);\r
209                 draft.getParent().layout(true);\r
210         } else {\r
211                 GridLayoutFactory.swtDefaults().numColumns(0).margins(0, 0).applyTo(draft);\r
212                 draft.getParent().layout(true);\r
213         }\r
214         \r
215         String exportLoc = exportLocation.getText();\r
216         if (exportLoc.isEmpty()) {\r
217             setMessage("Select target file.");\r
218             setErrorMessage(null);\r
219             setPageComplete(false);\r
220             return;\r
221         }\r
222         if (!exportLoc.endsWith(".sharedLibrary"))\r
223             exportLoc += exportLoc.endsWith(".") ? "sharedLibrary" : ".sharedLibrary";\r
224         File file = new File(exportLoc);\r
225         if (file.isDirectory()) {\r
226             setErrorMessage("The target is a directory.");\r
227             setPageComplete(false);\r
228             return;\r
229         }\r
230         File parent = file.getParentFile();\r
231         if (parent == null || !parent.isDirectory()) {\r
232             setErrorMessage("The target directory does not exist.");\r
233             setPageComplete(false);\r
234             return;\r
235         }\r
236         exportModel.exportLocation = file;\r
237         exportModel.overwrite = overwrite.getSelection();\r
238 \r
239         setErrorMessage(null);\r
240         setMessage("Export shared library to " + exportLoc + ".");\r
241         setPageComplete(true);\r
242     }\r
243 \r
244 }\r