/******************************************************************************* * 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.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Deque; import java.util.Iterator; import java.util.LinkedList; import java.util.Set; import java.util.TreeSet; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.preference.IPersistentPreferenceStore; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.ui.IExportWizard; import org.eclipse.ui.IMemento; import org.eclipse.ui.IWorkbench; import org.simantics.db.management.ISessionContext; import org.simantics.project.IProject; import org.simantics.project.ProjectKeys; import org.simantics.Simantics; import org.simantics.utils.ui.ErrorLogger; import org.simantics.utils.ui.ExceptionUtils; import org.simantics.utils.ui.workbench.StringMemento; public abstract class ModelExportWizard extends Wizard implements IExportWizard { private static final int MAX_RECENT_EXPORT_PATHS = 10; Deque recentExportPaths; boolean overwrite; T exportModel; protected abstract T createExportModel(Deque recentExportPaths); protected abstract ModelExportWizardPage createExportPage(T exportModel); protected abstract IPersistentPreferenceStore getPreferenceStore(); protected abstract IRunnableWithProgress createExportRunnable(T exportModel); protected abstract String getExportLocationId(); protected abstract String getExportOverwriteId(); @Override public void init(IWorkbench workbench, IStructuredSelection selection) { readPreferences(); ISessionContext ctx = Simantics.getSessionContext(); if (ctx == null) return; IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT); if (project == null) return; exportModel = createExportModel(recentExportPaths); exportModel.setSelection(selection.getFirstElement()); exportModel.setOverwrite(overwrite); } @Override public void addPages() { super.addPages(); if (exportModel != null) { addPage(createExportPage(exportModel)); } } @Override public boolean performFinish() { try { recentExportPaths.addFirst(exportModel.getExportLocation().getAbsolutePath()); removeDuplicates(recentExportPaths); if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS) recentExportPaths.pollLast(); writePreferences(); } catch (IOException e) { ErrorLogger.defaultLogError("Failed to write preferences", e); } if (exportModel.usesFile()) { File outputFile = exportModel.getExportLocation(); if (outputFile.exists()) { if (!outputFile.isFile()) { MessageDialog.openError(getShell(), "File Problem", "Output target is not a file " + outputFile.getAbsolutePath()); return false; } if (!exportModel.isOverwrite()) { boolean ok = MessageDialog.openConfirm(getShell(), "Overwrite", "A file by the name " + outputFile.getAbsolutePath() + " contains files.\n\nDo you want to overwrite the files?"); if (!ok) { return false; } if (!outputFile.delete()) { MessageDialog.openError(getShell(), "Delete Problem", "Could not overwrite previously existing file " + outputFile.getAbsolutePath()); return false; } } } } else { File outputFolder = exportModel.getExportLocation(); if (outputFolder.exists()) { if (!outputFolder.isDirectory()) { MessageDialog.openError(getShell(), "Folder Problem", "Output target is not a folder " + outputFolder.getAbsolutePath()); return false; } String files[] = outputFolder.list(); if (files.length > 0) { if (!exportModel.isOverwrite()) { boolean ok = MessageDialog.openConfirm(getShell(), "Overwrite", "A folder by the name " + outputFolder.getAbsolutePath() + " contains files.\n\nDo you want to overwrite the files?"); if (!ok) { return false; } } } } else { if (!outputFolder.mkdir()) { MessageDialog.openError(getShell(), "Folder Problem", "Could not create new folder " + outputFolder); return false; } } } try { getContainer().run(true, true,createExportRunnable(exportModel)); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); WizardPage cp = (WizardPage) getContainer().getCurrentPage(); if (t instanceof IOException) { ErrorLogger.defaultLogError("An I/O problem occurred while exporting the model. See exception for details.", t); cp.setErrorMessage("An I/O problem occurred while exporting the model.\n\nMessage: " + e.getMessage()); } else { ErrorLogger.defaultLogError("Unexpected exception while exporting the model. See exception for details.", t); cp.setErrorMessage("Unexpected exception while exporting the model. See error log for details.\n\nMessage: " + e.getMessage()); } return false; } catch (InterruptedException e) { ExceptionUtils.logAndShowError(e); return false; } return true; } private boolean readPreferences() { IPreferenceStore store = getPreferenceStore(); String recentPathsPref = store.getString(getExportLocationId()); recentExportPaths = decodePaths(recentPathsPref); overwrite = store.getBoolean(getExportOverwriteId()); return true; } private void writePreferences() throws IOException { IPersistentPreferenceStore store = getPreferenceStore(); store.putValue(getExportLocationId(), encodePaths(recentExportPaths)); store.setValue(getExportOverwriteId(), exportModel.isOverwrite()); if (store.needsSaving()) store.save(); } private static final String TAG_PATH = "path"; private static final String ATTR_NAME = "name"; public static Deque decodePaths(String recentPathsPref) { Deque result = new LinkedList(); try { StringMemento sm = new StringMemento(recentPathsPref); for (IMemento m : sm.getChildren(TAG_PATH)) { String name = m.getString(ATTR_NAME); if (name != null && !name.isEmpty()) result.add(name); } } catch (IllegalArgumentException e) { } return result; } public static String encodePaths(Deque recentPaths) { StringMemento sm = new StringMemento(); for (String path : recentPaths) { IMemento m = sm.createChild(TAG_PATH); m.putString(ATTR_NAME, path); } return sm.toString(); } public static void removeDuplicates(Iterable iter) { // Remove duplicates Set dups = new TreeSet(String.CASE_INSENSITIVE_ORDER); for (Iterator it = iter.iterator(); it.hasNext();) { String path = it.next(); if (!dups.add(path)) { it.remove(); } } } }