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