/******************************************************************************* * Copyright (c) 2012 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.modeling.template2d.ui.wizard; import java.io.File; import java.util.Collections; import java.util.List; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.simantics.databoard.Bindings; import org.simantics.db.Resource; import org.simantics.db.common.NamedResource; import org.simantics.db.common.primitiverequest.RelatedValue; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.SelectionHints; import org.simantics.layer0.Layer0; import org.simantics.utils.ui.ISelectionUtils; /** * @author Tuukka Lehtonen * @author Teemu Mätäsniemi */ public class DrawingTemplateExportPage extends WizardPage { ExportPlan exportModel; CCombo model; CCombo exportLocation; List models = Collections.emptyList(); private Button overwrite; protected DrawingTemplateExportPage(ExportPlan model) { super("Export Diagram Template", "Define Export Location", null); this.exportModel = model; } @Override public void createControl(Composite parent) { Composite container = new Composite(parent, SWT.NONE); { GridLayout layout = new GridLayout(); layout.horizontalSpacing = 20; layout.verticalSpacing = 10; layout.numColumns = 3; container.setLayout(layout); } new Label(container, SWT.NONE).setText("&Exported template:"); model = new CCombo(container, SWT.BORDER); { model.setEditable(false); model.setText(""); model.setToolTipText("Selects the diagram template to export a state from."); GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model); model.addModifyListener(new ModifyListener(){ @Override public void modifyText(ModifyEvent e) { validatePage(); } }); } new Label(container, SWT.NONE).setText("&Target file:"); exportLocation = new CCombo(container, SWT.BORDER); { exportLocation.setText(""); GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation); exportLocation.addModifyListener(new ModifyListener(){ @Override public void modifyText(ModifyEvent e) { validatePage(); } }); } Button browseFileButton = new Button(container, SWT.PUSH); { browseFileButton.setText("Browse..."); browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); browseFileButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { FileDialog dialog = new FileDialog(getShell(), SWT.SAVE); dialog.setText("Choose Export Target File"); String loc = exportLocation.getText(); dialog.setFilterPath(loc); dialog.setFilterExtensions(new String[] { "*.diagramTemplate" }); dialog.setFilterNames(new String[] { "Diagram Template (*.diagramTemplate)" }); dialog.setOverwrite(false); String file = dialog.open(); if (file == null) return; exportLocation.setText(file); validatePage(); } }); } Label horizRule = new Label(container, SWT.BORDER); GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule); overwrite = new Button(container, SWT.CHECK); overwrite.setText("&Overwrite existing files without warning"); overwrite.setSelection(exportModel.overwrite); GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite); overwrite.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { validatePage(); } }); try { initializeData(); } catch (DatabaseException e) { e.printStackTrace(); } setControl(container); validatePage(); } private void initializeData() throws DatabaseException { List drawingTemplates = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class); // FIXME: YUCK! Do not do this. if(drawingTemplates.size() != 1) throw new RuntimeException(); Layer0 L0 = Layer0.getInstance(exportModel.sessionContext.getSession()); String name = exportModel.sessionContext.getSession().sync(new RelatedValue(drawingTemplates.get(0), L0.HasName, Bindings.STRING)); // Load all states in the selected model exportModel.model = new NamedResource(name, drawingTemplates.get(0)); models = Collections.singletonList(exportModel.model); // Populate combo boxes int i = 0; for (NamedResource m : models) { model.add(m.getName()); model.setData(String.valueOf(i), m); if (m.equals(exportModel.model)) model.select(i); ++i; } for (String path : exportModel.recentLocations) { exportLocation.add(path); } if (exportLocation.getItemCount() > 0) exportLocation.select(0); } void validatePage() { if (exportModel.model == null) { setMessage("Select model to export from."); setErrorMessage(null); setPageComplete(false); return; } String exportLoc = exportLocation.getText(); if (exportLoc.isEmpty()) { setMessage("Select target file."); setErrorMessage(null); setPageComplete(false); return; } if (!exportLoc.endsWith(".diagramTemplate")) exportLoc += exportLoc.endsWith(".") ? "diagramTemplate" : ".diagramTemplate"; File file = new File(exportLoc); if (file.isDirectory()) { setErrorMessage("The target is a directory."); setPageComplete(false); return; } File parent = file.getParentFile(); if (parent == null || !parent.isDirectory()) { setErrorMessage("The target directory does not exist."); setPageComplete(false); return; } exportModel.exportLocation = file; exportModel.overwrite = overwrite.getSelection(); setErrorMessage(null); setMessage("Export diagram template to " + exportLoc + "."); setPageComplete(true); } }