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;
63 protected ModelExportPage(ExportPlan model) {
64 super("Export Model", "Define Export Location", null);
65 this.exportModel = model;
69 public void createControl(Composite parent) {
70 Composite container = new Composite(parent, SWT.NONE);
72 GridLayout layout = new GridLayout();
73 layout.horizontalSpacing = 20;
74 layout.verticalSpacing = 10;
75 layout.numColumns = 3;
76 container.setLayout(layout);
79 draft = new Composite(container, SWT.NONE);
80 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(draft);
81 draft.setBackground(draft.getDisplay().getSystemColor(SWT.COLOR_RED));
82 GridLayoutFactory.swtDefaults().spacing(5, 5).applyTo(draft);
84 Composite draft2 = new Composite(draft, SWT.NONE);
85 GridLayoutFactory.swtDefaults().applyTo(draft2);
86 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(draft2);
87 Label draftText = new Label(draft2, SWT.WRAP);
88 draftText.setText("Some dependencies of the selected model are not published. The model can only be saved with draft status.");
90 new Label(container, SWT.NONE).setText("Exported &model:");
91 model = new CCombo(container, SWT.BORDER);
93 model.setEditable(false);
95 model.setToolTipText("Selects the model to export.");
96 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);
97 model.addSelectionListener(new SelectionAdapter() {
99 public void widgetSelected(SelectionEvent e) {
100 int selected = model.getSelectionIndex();
101 if (selected != -1) {
102 exportModel.model = (LibraryInfo) model.getData(String.valueOf(selected));
109 new Label(container, SWT.NONE).setText("&Target file:");
110 exportLocation = new CCombo(container, SWT.BORDER);
112 exportLocation.setText("");
113 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
114 exportLocation.addModifyListener(new ModifyListener(){
116 public void modifyText(ModifyEvent e) {
121 Button browseFileButton = new Button(container, SWT.PUSH);
123 browseFileButton.setText("Browse...");
124 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
125 browseFileButton.addSelectionListener(new SelectionAdapter() {
127 public void widgetSelected(SelectionEvent e) {
128 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
129 dialog.setText("Choose Export Target File");
130 String loc = exportLocation.getText();
131 dialog.setFilterPath(loc);
132 dialog.setFilterExtensions(new String[] { "*.tg" });
133 dialog.setFilterNames(new String[] { "Model (*.tg)" });
134 dialog.setOverwrite(false);
135 String file = dialog.open();
138 exportLocation.setText(file);
144 Label horizRule = new Label(container, SWT.BORDER);
145 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
147 overwrite = new Button(container, SWT.CHECK);
148 overwrite.setText("&Overwrite existing files without warning");
149 overwrite.setSelection(exportModel.overwrite);
150 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);
151 overwrite.addSelectionListener(new SelectionAdapter() {
153 public void widgetSelected(SelectionEvent e) {
160 } catch (DatabaseException e) {
164 setControl(container);
168 private void initializeData() throws DatabaseException {
170 List<Resource> selected = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
172 models = exportModel.sessionContext.getSession().syncRequest(new UniqueRead<List<LibraryInfo>>() {
174 public List<LibraryInfo> perform(ReadGraph graph) throws DatabaseException {
175 List<LibraryInfo> result = new ArrayList<>();
176 Layer0 L0 = Layer0.getInstance(graph);
177 SimulationResource SIMU = SimulationResource.getInstance(graph);
178 for (Resource r : selected) {
180 if(!graph.isInstanceOf(model, SIMU.Model)) {
181 model = graph.syncRequest(new IndexRoot(r));
182 if(!graph.isInstanceOf(model, SIMU.Model))
185 String name = graph.getRelatedValue(model, L0.HasName, Bindings.STRING);
186 DraftStatusBean draft = ModelingUtils.getDependencyDraftStatus(graph, model);
187 result.add(new LibraryInfo(name, model, draft));
189 Collections.sort(result, new Comparator<LibraryInfo>() {
191 public int compare(LibraryInfo o1, LibraryInfo o2) {
192 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.library.getName(), o2.library.getName());
199 if (!models.isEmpty())
200 exportModel.model = models.get(0);
202 // Populate combo boxes
204 for (LibraryInfo m : models) {
205 model.add(m.library.getName());
206 model.setData(String.valueOf(i), m);
207 if (m.equals(exportModel.model))
212 for (String path : exportModel.recentLocations) {
213 exportLocation.add(path);
215 if (exportLocation.getItemCount() > 0)
216 exportLocation.select(0);
219 void validatePage() {
220 if (exportModel.model == null) {
221 setMessage("Select library to export from.");
222 setErrorMessage(null);
223 setPageComplete(false);
227 boolean draftVisible = draft.getVisible();
228 boolean hasDraftDependencies = exportModel.model.draft != null;
229 if (draftVisible != hasDraftDependencies) {
230 ((GridData) draft.getLayoutData()).exclude = !hasDraftDependencies;
231 draft.setVisible(hasDraftDependencies);
232 draft.getParent().getParent().layout(true, true);
235 String exportLoc = exportLocation.getText();
236 if (exportLoc.isEmpty()) {
237 setMessage("Select target file.");
238 setErrorMessage(null);
239 setPageComplete(false);
242 File file = new File(exportLoc);
243 if (file.isDirectory()) {
244 setErrorMessage("The target is a directory.");
245 setPageComplete(false);
248 File parent = file.getParentFile();
249 if (parent == null || !parent.isDirectory()) {
250 setErrorMessage("The target directory does not exist.");
251 setPageComplete(false);
254 exportModel.exportLocation = file;
255 exportModel.overwrite = overwrite.getSelection();
257 setErrorMessage(null);
258 setMessage("Export selected model to " + exportModel.exportLocation + ".");
259 setPageComplete(true);