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.IndexRoot;
39 import org.simantics.db.common.request.UniqueRead;
40 import org.simantics.db.exception.DatabaseException;
41 import org.simantics.db.layer0.SelectionHints;
42 import org.simantics.db.layer0.util.DraftStatusBean;
43 import org.simantics.layer0.Layer0;
44 import org.simantics.modeling.ModelingUtils;
45 import org.simantics.modeling.ModelingUtils.LibraryInfo;
46 import org.simantics.simulation.ontology.SimulationResource;
47 import org.simantics.utils.strings.AlphanumComparator;
48 import org.simantics.utils.ui.ISelectionUtils;
51 * @author Antti Villberg
53 public class ModelExportPage extends WizardPage {
55 ExportPlan exportModel;
58 CCombo exportLocation;
60 List<LibraryInfo> models = Collections.emptyList();
61 private Button overwrite;
62 private Button dependencies;
64 protected ModelExportPage(ExportPlan model) {
65 super("Export Model", "Define Export Location", null);
66 this.exportModel = model;
70 public void createControl(Composite parent) {
71 Composite container = new Composite(parent, SWT.NONE);
73 GridLayout layout = new GridLayout();
74 layout.horizontalSpacing = 20;
75 layout.verticalSpacing = 10;
76 layout.numColumns = 3;
77 container.setLayout(layout);
80 draft = new Composite(container, SWT.NONE);
81 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);
82 draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));
83 GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
85 Composite draft2 = new Composite(draft, SWT.NONE);
86 GridLayoutFactory.swtDefaults().applyTo(draft2);
87 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);
88 Label draftText = new Label(draft2, SWT.WRAP);
89 draftText.setText("Some dependencies of the selected model are not published. The model can only be saved with draft status.");
91 new Label(container, SWT.NONE).setText("Exported &model:");
92 model = new CCombo(container, SWT.BORDER);
94 model.setEditable(false);
96 model.setToolTipText("Selects the model to export.");
97 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);
98 model.addSelectionListener(new SelectionAdapter() {
100 public void widgetSelected(SelectionEvent e) {
101 int selected = model.getSelectionIndex();
102 if (selected != -1) {
103 exportModel.model = (LibraryInfo) model.getData(String.valueOf(selected));
110 new Label(container, SWT.NONE).setText("&Target file:");
111 exportLocation = new CCombo(container, SWT.BORDER);
113 exportLocation.setText("");
114 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
115 exportLocation.addModifyListener(new ModifyListener(){
117 public void modifyText(ModifyEvent e) {
122 Button browseFileButton = new Button(container, SWT.PUSH);
124 browseFileButton.setText("Browse...");
125 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
126 browseFileButton.addSelectionListener(new SelectionAdapter() {
128 public void widgetSelected(SelectionEvent e) {
129 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
130 dialog.setText("Choose Export Target File");
131 String loc = exportLocation.getText();
132 dialog.setFilterPath(loc);
133 dialog.setFilterExtensions(new String[] { "*.tg" });
134 dialog.setFilterNames(new String[] { "Model (*.tg)" });
135 dialog.setOverwrite(false);
136 String file = dialog.open();
139 exportLocation.setText(file);
145 Label horizRule = new Label(container, SWT.BORDER);
146 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
148 overwrite = new Button(container, SWT.CHECK);
149 overwrite.setText("&Overwrite existing files without warning");
150 overwrite.setSelection(exportModel.overwrite);
151 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);
152 overwrite.addSelectionListener(new SelectionAdapter() {
154 public void widgetSelected(SelectionEvent e) {
159 dependencies = new Button(container, SWT.CHECK);
160 dependencies.setText("&Export dependencies");
161 dependencies.setSelection(exportModel.includeDependencies);
162 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(dependencies);
163 dependencies.addSelectionListener(new SelectionAdapter() {
165 public void widgetSelected(SelectionEvent e) {
172 } catch (DatabaseException e) {
176 setControl(container);
180 private void initializeData() throws DatabaseException {
182 List<Resource> selected = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
184 models = exportModel.sessionContext.getSession().syncRequest(new UniqueRead<List<LibraryInfo>>() {
186 public List<LibraryInfo> perform(ReadGraph graph) throws DatabaseException {
187 List<LibraryInfo> result = new ArrayList<>();
188 Layer0 L0 = Layer0.getInstance(graph);
189 SimulationResource SIMU = SimulationResource.getInstance(graph);
190 for (Resource r : selected) {
192 if(!graph.isInstanceOf(model, SIMU.Model)) {
193 model = graph.syncRequest(new IndexRoot(r));
194 if(!graph.isInstanceOf(model, SIMU.Model))
197 String name = graph.getRelatedValue(model, L0.HasName, Bindings.STRING);
198 DraftStatusBean draft = ModelingUtils.getDependencyDraftStatus(graph, model);
199 result.add(new LibraryInfo(name, model, draft));
201 Collections.sort(result, new Comparator<LibraryInfo>() {
203 public int compare(LibraryInfo o1, LibraryInfo o2) {
204 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.library.getName(), o2.library.getName());
211 if (!models.isEmpty())
212 exportModel.model = models.get(0);
214 // Populate combo boxes
216 for (LibraryInfo m : models) {
217 model.add(m.library.getName());
218 model.setData(String.valueOf(i), m);
219 if (m.equals(exportModel.model))
224 for (String path : exportModel.recentLocations) {
225 exportLocation.add(path);
227 if (exportLocation.getItemCount() > 0)
228 exportLocation.select(0);
231 void validatePage() {
232 if (exportModel.model == null) {
233 setMessage("Select library to export from.");
234 setErrorMessage(null);
235 setPageComplete(false);
239 boolean draftVisible = draft.getVisible();
240 boolean hasDraftDependencies = exportModel.model.draft != null;
241 if (draftVisible != hasDraftDependencies) {
242 ((GridData) draft.getLayoutData()).exclude = !hasDraftDependencies;
243 draft.setVisible(hasDraftDependencies);
244 draft.getParent().getParent().layout(true, true);
247 String exportLoc = exportLocation.getText();
248 if (exportLoc.isEmpty()) {
249 setMessage("Select target file.");
250 setErrorMessage(null);
251 setPageComplete(false);
254 File file = new File(exportLoc);
255 if (file.isDirectory()) {
256 setErrorMessage("The target is a directory.");
257 setPageComplete(false);
260 File parent = file.getParentFile();
261 if (parent == null || !parent.isDirectory()) {
262 setErrorMessage("The target directory does not exist.");
263 setPageComplete(false);
266 exportModel.exportLocation = file;
267 exportModel.overwrite = overwrite.getSelection();
268 exportModel.includeDependencies = dependencies.getSelection();
270 setErrorMessage(null);
271 setMessage("Export selected model to " + exportModel.exportLocation + ".");
272 setPageComplete(true);