/******************************************************************************* * Copyright (c) 2012, 2013 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.g3d.wizard; import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.resource.ImageDescriptor; 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.DirectoryDialog; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.NamedResource; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.ui.utils.ResourceAdaptionUtils; public abstract class ModelExportWizardPage extends WizardPage{ T exportData; CCombo model; CCombo exportLocation; boolean exportToFile = true; private List models = new ArrayList(); private Button overwrite; public ModelExportWizardPage(String pageName, String title, ImageDescriptor titleImage, T data) { super(pageName, title, titleImage); this.exportData = data; exportToFile = data.usesFile(); } protected abstract List getSupportedModels(ReadGraph graph, Resource project) throws DatabaseException; public String[] getFilterExtensions() { return new String[0]; } public String[] getFilterNames() { return new String[0]; } @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 &model:"); model = new CCombo(container, SWT.BORDER); { model.setEditable(false); model.setText(""); model.setToolTipText("Selects the model to export."); GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model); model.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { exportData.setModel(((NamedResource) model.getData(Integer.toString(model.getSelectionIndex())))); validatePage(); } }); } if (exportToFile) { 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"); dialog.setFilterExtensions(getFilterExtensions()); dialog.setFilterNames(getFilterNames()); String loc = exportLocation.getText(); dialog.setFilterPath(loc); String file = dialog.open(); if (file == null) return; exportLocation.setText(file); validatePage(); } }); } } else { new Label(container, SWT.NONE).setText("&Target folder:"); 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) { DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.SAVE); dialog.setText("Choose Export Target Folder"); String loc = exportLocation.getText(); dialog.setFilterPath(loc); 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(exportData.isOverwrite()); 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(); } protected void initializeData() throws DatabaseException { final Resource selection = ResourceAdaptionUtils.toSingleResource(exportData.getSelection()); Simantics.getSessionContext().getSession().syncRequest( new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); models = getSupportedModels(graph, Simantics.getProject().get()); } }); Collections.sort(models); // Populate combo boxes int i = 0; boolean selected = false; for (NamedResource s : models) { model.add(s.getName()); model.setData(String.valueOf(i), s); if (s.getResource().equals(selection)) { model.select(i); selected = true; } ++i; } if (!selected && i > 0) model.select(0); if (model.getSelectionIndex() >= 0) { exportData.setModel((NamedResource)model.getData(Integer.toString(model.getSelectionIndex()))); } for (String path : exportData.getRecentLocations()) { exportLocation.add(path); } if (exportLocation.getItemCount() > 0) exportLocation.select(0); } protected void validatePage() { if (exportData.getModel() == null) { setMessage("Select model to export."); setErrorMessage(null); setPageComplete(false); return; } String exportLoc = exportLocation.getText(); File file; if (exportToFile) { if (exportLoc.isEmpty()) { setMessage("Select target file."); setErrorMessage(null); setPageComplete(false); return; } file = new File(exportLoc); if (file.exists() && !file.isFile()) { setErrorMessage("The target must be a file, an existing directory was given."); setPageComplete(false); return; } File parent = file.getParentFile(); if (parent == null || !parent.isDirectory()) { setErrorMessage("The target directory does not exist."); setPageComplete(false); return; } } else { if (exportLoc.isEmpty()) { setMessage("Select target directory."); setErrorMessage(null); setPageComplete(false); return; } file = new File(exportLoc); if (file.exists() && !file.isDirectory()) { setErrorMessage("The target must be a directory, an existing file was given."); setPageComplete(false); return; } } exportData.setExportLocation(file); exportData.setOverwrite(overwrite.getSelection()); setErrorMessage(null); setMessage("Export selected model."); setPageComplete(true); } }