X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.annotation.ui%2Fsrc%2Forg%2Fsimantics%2Fannotation%2Fui%2Fwizard%2FAnnotationTypeExportWizard.java;h=95b416aef70191bc637f2df1535836db1ac367af;hp=aaa10a115045bc9db4867320010fee017879011a;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/wizard/AnnotationTypeExportWizard.java b/bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/wizard/AnnotationTypeExportWizard.java index aaa10a115..95b416aef 100644 --- a/bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/wizard/AnnotationTypeExportWizard.java +++ b/bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/wizard/AnnotationTypeExportWizard.java @@ -1,152 +1,152 @@ -/******************************************************************************* - * 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.annotation.ui.wizard; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.util.Deque; - -import org.eclipse.core.runtime.preferences.InstanceScope; -import org.eclipse.jface.dialogs.MessageDialog; -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.IWorkbench; -import org.eclipse.ui.preferences.ScopedPreferenceStore; -import org.simantics.annotation.ui.Activator; -import org.simantics.db.management.ISessionContext; -import org.simantics.modeling.ui.utils.NoProjectPage; -import org.simantics.project.IProject; -import org.simantics.project.ProjectKeys; -import org.simantics.ui.SimanticsUI; -import org.simantics.utils.ui.ErrorLogger; -import org.simantics.utils.ui.ExceptionUtils; - -/** - * @author Tuukka Lehtonen - * @author Teemu Mätäsniemi - * @author Antti Villberg - */ -public class AnnotationTypeExportWizard extends Wizard implements IExportWizard { - - private static final int MAX_RECENT_EXPORT_PATHS = 10; - - Deque recentExportPaths; - boolean overwrite; - - ExportPlan exportModel; - - private boolean readPreferences() { - IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID); - - String recentPathsPref = store.getString(Preferences.RECENT_ANNOTATION_TYPE_EXPORT_LOCATIONS); - recentExportPaths = Preferences.decodePaths(recentPathsPref); - overwrite = store.getBoolean(Preferences.ANNOTATION_TYPE_EXPORT_OVERWRITE); - - return true; - } - - private void writePreferences() throws IOException { - IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID); - - store.putValue(Preferences.RECENT_ANNOTATION_TYPE_EXPORT_LOCATIONS, Preferences.encodePaths(recentExportPaths)); - store.setValue(Preferences.ANNOTATION_TYPE_EXPORT_OVERWRITE, exportModel.overwrite); - - if (store.needsSaving()) - store.save(); - } - - public AnnotationTypeExportWizard() { - setWindowTitle("Export Annotation Type"); - setNeedsProgressMonitor(true); - } - - @Override - public void init(IWorkbench workbench, IStructuredSelection selection) { - readPreferences(); - - ISessionContext ctx = SimanticsUI.getSessionContext(); - if (ctx == null) - return; - IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT); - if (project == null) - return; - - exportModel = new ExportPlan(ctx, recentExportPaths); - exportModel.project = project; - exportModel.selection = selection; - exportModel.overwrite = overwrite; - } - - @Override - public void addPages() { - super.addPages(); - if (exportModel != null) { - addPage(new AnnotationTypeExportPage(exportModel)); - } else { - addPage(new NoProjectPage("Export Annotation Type")); - } - } - - @Override - public boolean performFinish() { - try { - recentExportPaths.addFirst(exportModel.exportLocation.getAbsolutePath()); - Preferences.removeDuplicates(recentExportPaths); - if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS) - recentExportPaths.pollLast(); - - writePreferences(); - } catch (IOException e) { - ErrorLogger.defaultLogError("Failed to write preferences", e); - } - - final File output = exportModel.exportLocation; - if (output.exists()) { - if (!exportModel.overwrite) { - boolean ok = MessageDialog.openConfirm(getShell(), "Overwrite", "A file by the name " + output.getAbsolutePath() + " already exists.\n\nDo you want to overwrite?"); - if (!ok) { - return false; - } - } - if (!output.delete()) { - MessageDialog.openError(getShell(), "Delete Problem", "Could not overwrite previously existing file " + output); - return false; - } - } - - try { - getContainer().run(true, true, new AnnotationTypeExporter(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 annotation type. See exception for details.", t); - cp.setErrorMessage("An I/O problem occurred while exporting the annotation type.\n\nMessage: " + e.getMessage()); - } else { - ErrorLogger.defaultLogError("Unexpected exception while exporting the annotation type. See exception for details.", t); - cp.setErrorMessage("Unexpected exception while exporting the annotation type. See error log for details.\n\nMessage: " + e.getMessage()); - } - return false; - } catch (InterruptedException e) { - ExceptionUtils.logAndShowError(e); - return false; - } - - return true; - } - -} +/******************************************************************************* + * 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.annotation.ui.wizard; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.Deque; + +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.jface.dialogs.MessageDialog; +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.IWorkbench; +import org.eclipse.ui.preferences.ScopedPreferenceStore; +import org.simantics.annotation.ui.Activator; +import org.simantics.db.management.ISessionContext; +import org.simantics.modeling.ui.utils.NoProjectPage; +import org.simantics.project.IProject; +import org.simantics.project.ProjectKeys; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.ErrorLogger; +import org.simantics.utils.ui.ExceptionUtils; + +/** + * @author Tuukka Lehtonen + * @author Teemu Mätäsniemi + * @author Antti Villberg + */ +public class AnnotationTypeExportWizard extends Wizard implements IExportWizard { + + private static final int MAX_RECENT_EXPORT_PATHS = 10; + + Deque recentExportPaths; + boolean overwrite; + + ExportPlan exportModel; + + private boolean readPreferences() { + IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID); + + String recentPathsPref = store.getString(Preferences.RECENT_ANNOTATION_TYPE_EXPORT_LOCATIONS); + recentExportPaths = Preferences.decodePaths(recentPathsPref); + overwrite = store.getBoolean(Preferences.ANNOTATION_TYPE_EXPORT_OVERWRITE); + + return true; + } + + private void writePreferences() throws IOException { + IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID); + + store.putValue(Preferences.RECENT_ANNOTATION_TYPE_EXPORT_LOCATIONS, Preferences.encodePaths(recentExportPaths)); + store.setValue(Preferences.ANNOTATION_TYPE_EXPORT_OVERWRITE, exportModel.overwrite); + + if (store.needsSaving()) + store.save(); + } + + public AnnotationTypeExportWizard() { + setWindowTitle("Export Annotation Type"); + setNeedsProgressMonitor(true); + } + + @Override + public void init(IWorkbench workbench, IStructuredSelection selection) { + readPreferences(); + + ISessionContext ctx = SimanticsUI.getSessionContext(); + if (ctx == null) + return; + IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT); + if (project == null) + return; + + exportModel = new ExportPlan(ctx, recentExportPaths); + exportModel.project = project; + exportModel.selection = selection; + exportModel.overwrite = overwrite; + } + + @Override + public void addPages() { + super.addPages(); + if (exportModel != null) { + addPage(new AnnotationTypeExportPage(exportModel)); + } else { + addPage(new NoProjectPage("Export Annotation Type")); + } + } + + @Override + public boolean performFinish() { + try { + recentExportPaths.addFirst(exportModel.exportLocation.getAbsolutePath()); + Preferences.removeDuplicates(recentExportPaths); + if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS) + recentExportPaths.pollLast(); + + writePreferences(); + } catch (IOException e) { + ErrorLogger.defaultLogError("Failed to write preferences", e); + } + + final File output = exportModel.exportLocation; + if (output.exists()) { + if (!exportModel.overwrite) { + boolean ok = MessageDialog.openConfirm(getShell(), "Overwrite", "A file by the name " + output.getAbsolutePath() + " already exists.\n\nDo you want to overwrite?"); + if (!ok) { + return false; + } + } + if (!output.delete()) { + MessageDialog.openError(getShell(), "Delete Problem", "Could not overwrite previously existing file " + output); + return false; + } + } + + try { + getContainer().run(true, true, new AnnotationTypeExporter(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 annotation type. See exception for details.", t); + cp.setErrorMessage("An I/O problem occurred while exporting the annotation type.\n\nMessage: " + e.getMessage()); + } else { + ErrorLogger.defaultLogError("Unexpected exception while exporting the annotation type. See exception for details.", t); + cp.setErrorMessage("Unexpected exception while exporting the annotation type. See error log for details.\n\nMessage: " + e.getMessage()); + } + return false; + } catch (InterruptedException e) { + ExceptionUtils.logAndShowError(e); + return false; + } + + return true; + } + +}