1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.sharedontology.wizard;
\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
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
49 * @author Antti Villberg
\r
51 public class ModelExportPage extends WizardPage {
\r
53 ExportPlan exportModel;
\r
56 CCombo exportLocation;
\r
58 List<LibraryInfo> models = Collections.emptyList();
\r
59 private Button overwrite;
\r
61 protected ModelExportPage(ExportPlan model) {
\r
62 super("Export Model", "Define Export Location", null);
\r
63 this.exportModel = model;
\r
67 public void createControl(Composite parent) {
\r
68 Composite container = new Composite(parent, SWT.NONE);
\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
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
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
88 new Label(container, SWT.NONE).setText("Exported &model:");
\r
89 model = new CCombo(container, SWT.BORDER);
\r
91 model.setEditable(false);
\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
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
107 new Label(container, SWT.NONE).setText("&Target file:");
\r
108 exportLocation = new CCombo(container, SWT.BORDER);
\r
110 exportLocation.setText("");
\r
111 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
\r
112 exportLocation.addModifyListener(new ModifyListener(){
\r
114 public void modifyText(ModifyEvent e) {
\r
119 Button browseFileButton = new Button(container, SWT.PUSH);
\r
121 browseFileButton.setText("Browse...");
\r
122 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
\r
123 browseFileButton.addSelectionListener(new SelectionAdapter() {
\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
136 exportLocation.setText(file);
\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
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
151 public void widgetSelected(SelectionEvent e) {
\r
158 } catch (DatabaseException e) {
\r
159 e.printStackTrace();
\r
162 setControl(container);
\r
166 private void initializeData() throws DatabaseException {
\r
168 List<Resource> selected = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
\r
170 models = exportModel.sessionContext.getSession().syncRequest(new UniqueRead<List<LibraryInfo>>() {
\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
180 Collections.sort(result, new Comparator<LibraryInfo>() {
\r
182 public int compare(LibraryInfo o1, LibraryInfo o2) {
\r
183 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.library.getName(), o2.library.getName());
\r
190 if (!models.isEmpty())
\r
191 exportModel.model = models.get(0);
\r
193 // Populate combo boxes
\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
203 for (String path : exportModel.recentLocations) {
\r
204 exportLocation.add(path);
\r
206 if (exportLocation.getItemCount() > 0)
\r
207 exportLocation.select(0);
\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
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
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
233 File file = new File(exportLoc);
\r
234 if (file.isDirectory()) {
\r
235 setErrorMessage("The target is a directory.");
\r
236 setPageComplete(false);
\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
245 exportModel.exportLocation = file;
\r
246 exportModel.overwrite = overwrite.getSelection();
\r
248 setErrorMessage(null);
\r
249 setMessage("Export selected model to " + exportModel.exportLocation + ".");
\r
250 setPageComplete(true);
\r