1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.sharedontology.wizard;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.List;
20 import org.eclipse.jface.layout.GridDataFactory;
21 import org.eclipse.jface.layout.GridLayoutFactory;
22 import org.eclipse.jface.wizard.WizardPage;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.CCombo;
25 import org.eclipse.swt.events.ModifyEvent;
26 import org.eclipse.swt.events.ModifyListener;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.FileDialog;
34 import org.eclipse.swt.widgets.Label;
35 import org.simantics.databoard.Bindings;
36 import org.simantics.db.ReadGraph;
37 import org.simantics.db.Resource;
38 import org.simantics.db.common.request.UniqueRead;
39 import org.simantics.db.exception.DatabaseException;
40 import org.simantics.db.layer0.SelectionHints;
41 import org.simantics.db.layer0.util.DraftStatusBean;
42 import org.simantics.layer0.Layer0;
43 import org.simantics.modeling.ModelingUtils;
44 import org.simantics.modeling.ModelingUtils.LibraryInfo;
45 import org.simantics.utils.strings.AlphanumComparator;
46 import org.simantics.utils.ui.ISelectionUtils;
49 * @author Antti Villberg
51 public class ModelExportPage extends WizardPage {
53 ExportPlan exportModel;
56 CCombo exportLocation;
58 List<LibraryInfo> models = Collections.emptyList();
59 private Button overwrite;
61 protected ModelExportPage(ExportPlan model) {
62 super("Export Model", "Define Export Location", null);
63 this.exportModel = model;
67 public void createControl(Composite parent) {
68 Composite container = new Composite(parent, SWT.NONE);
70 GridLayout layout = new GridLayout();
71 layout.horizontalSpacing = 20;
72 layout.verticalSpacing = 10;
73 layout.numColumns = 3;
74 container.setLayout(layout);
77 draft = new Composite(container, SWT.NONE);
78 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);
79 draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));
80 GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
82 Composite draft2 = new Composite(draft, SWT.NONE);
83 GridLayoutFactory.swtDefaults().applyTo(draft2);
84 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);
85 Label draftText = new Label(draft2, SWT.WRAP);
86 draftText.setText("Some dependencies of the selected model are not published. The model can only be saved with draft status.");
88 new Label(container, SWT.NONE).setText("Exported &model:");
89 model = new CCombo(container, SWT.BORDER);
91 model.setEditable(false);
93 model.setToolTipText("Selects the model to export.");
94 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);
95 model.addSelectionListener(new SelectionAdapter() {
97 public void widgetSelected(SelectionEvent e) {
98 int selected = model.getSelectionIndex();
100 exportModel.model = (LibraryInfo) model.getData(String.valueOf(selected));
107 new Label(container, SWT.NONE).setText("&Target file:");
108 exportLocation = new CCombo(container, SWT.BORDER);
110 exportLocation.setText("");
111 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
112 exportLocation.addModifyListener(new ModifyListener(){
114 public void modifyText(ModifyEvent e) {
119 Button browseFileButton = new Button(container, SWT.PUSH);
121 browseFileButton.setText("Browse...");
122 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
123 browseFileButton.addSelectionListener(new SelectionAdapter() {
125 public void widgetSelected(SelectionEvent e) {
126 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
127 dialog.setText("Choose Export Target File");
128 String loc = exportLocation.getText();
129 dialog.setFilterPath(loc);
130 dialog.setFilterExtensions(new String[] { "*.tg" });
131 dialog.setFilterNames(new String[] { "Model (*.tg)" });
132 dialog.setOverwrite(false);
133 String file = dialog.open();
136 exportLocation.setText(file);
142 Label horizRule = new Label(container, SWT.BORDER);
143 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
145 overwrite = new Button(container, SWT.CHECK);
146 overwrite.setText("&Overwrite existing files without warning");
147 overwrite.setSelection(exportModel.overwrite);
148 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);
149 overwrite.addSelectionListener(new SelectionAdapter() {
151 public void widgetSelected(SelectionEvent e) {
158 } catch (DatabaseException e) {
162 setControl(container);
166 private void initializeData() throws DatabaseException {
168 List<Resource> selected = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
170 models = exportModel.sessionContext.getSession().syncRequest(new UniqueRead<List<LibraryInfo>>() {
172 public List<LibraryInfo> perform(ReadGraph graph) throws DatabaseException {
173 List<LibraryInfo> result = new ArrayList<>();
174 Layer0 L0 = Layer0.getInstance(graph);
175 for (Resource r : selected) {
176 String name = graph.getRelatedValue(r, L0.HasName, Bindings.STRING);
177 DraftStatusBean draft = ModelingUtils.getDependencyDraftStatus(graph, r);
178 result.add(new LibraryInfo(name, r, draft));
180 Collections.sort(result, new Comparator<LibraryInfo>() {
182 public int compare(LibraryInfo o1, LibraryInfo o2) {
183 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.library.getName(), o2.library.getName());
190 if (!models.isEmpty())
191 exportModel.model = models.get(0);
193 // Populate combo boxes
195 for (LibraryInfo m : models) {
196 model.add(m.library.getName());
197 model.setData(String.valueOf(i), m);
198 if (m.equals(exportModel.model))
203 for (String path : exportModel.recentLocations) {
204 exportLocation.add(path);
206 if (exportLocation.getItemCount() > 0)
207 exportLocation.select(0);
210 void validatePage() {
211 if (exportModel.model == null) {
212 setMessage("Select library to export from.");
213 setErrorMessage(null);
214 setPageComplete(false);
218 boolean draftVisible = draft.getVisible();
219 boolean hasDraftDependencies = exportModel.model.draft != null;
220 if (draftVisible != hasDraftDependencies) {
221 ((GridData) draft.getLayoutData()).exclude = !hasDraftDependencies;
222 draft.setVisible(hasDraftDependencies);
223 draft.getParent().getParent().layout(true, true);
226 String exportLoc = exportLocation.getText();
227 if (exportLoc.isEmpty()) {
228 setMessage("Select target file.");
229 setErrorMessage(null);
230 setPageComplete(false);
233 File file = new File(exportLoc);
234 if (file.isDirectory()) {
235 setErrorMessage("The target is a directory.");
236 setPageComplete(false);
239 File parent = file.getParentFile();
240 if (parent == null || !parent.isDirectory()) {
241 setErrorMessage("The target directory does not exist.");
242 setPageComplete(false);
245 exportModel.exportLocation = file;
246 exportModel.overwrite = overwrite.getSelection();
248 setErrorMessage(null);
249 setMessage("Export selected model to " + exportModel.exportLocation + ".");
250 setPageComplete(true);