+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.template2d.ui.wizard;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.util.Deque;\r
+\r
+import org.eclipse.core.runtime.preferences.InstanceScope;\r
+import org.eclipse.jface.dialogs.MessageDialog;\r
+import org.eclipse.jface.preference.IPersistentPreferenceStore;\r
+import org.eclipse.jface.preference.IPreferenceStore;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.Wizard;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.ui.IExportWizard;\r
+import org.eclipse.ui.IWorkbench;\r
+import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.modeling.template2d.ui.Activator;\r
+import org.simantics.modeling.ui.utils.NoProjectPage;\r
+import org.simantics.project.IProject;\r
+import org.simantics.project.ProjectKeys;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ * @author Teemu Mätäsniemi\r
+ */\r
+public class DrawingTemplateExportWizard extends Wizard implements IExportWizard {\r
+\r
+ private static final int MAX_RECENT_EXPORT_PATHS = 10;\r
+\r
+ Deque<String> recentExportPaths;\r
+ boolean overwrite;\r
+\r
+ ExportPlan exportModel;\r
+\r
+ private boolean readPreferences() {\r
+ IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
+\r
+ String recentPathsPref = store.getString(Preferences.RECENT_DRAWING_TEMPLATE_EXPORT_LOCATIONS);\r
+ recentExportPaths = Preferences.decodePaths(recentPathsPref);\r
+ overwrite = store.getBoolean(Preferences.DRAWING_TEMPLATE_EXPORT_OVERWRITE);\r
+\r
+ return true;\r
+ }\r
+\r
+ private void writePreferences() throws IOException {\r
+ IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
+\r
+ store.putValue(Preferences.RECENT_DRAWING_TEMPLATE_EXPORT_LOCATIONS, Preferences.encodePaths(recentExportPaths));\r
+ store.setValue(Preferences.DRAWING_TEMPLATE_EXPORT_OVERWRITE, exportModel.overwrite);\r
+\r
+ if (store.needsSaving())\r
+ store.save();\r
+ }\r
+\r
+ public DrawingTemplateExportWizard() {\r
+ setWindowTitle("Export Diagram Template");\r
+ setNeedsProgressMonitor(true);\r
+ }\r
+\r
+ @Override\r
+ public void init(IWorkbench workbench, IStructuredSelection selection) {\r
+ readPreferences();\r
+\r
+ ISessionContext ctx = SimanticsUI.getSessionContext();\r
+ if (ctx == null)\r
+ return;\r
+ IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);\r
+ if (project == null)\r
+ return;\r
+\r
+ exportModel = new ExportPlan(ctx, recentExportPaths);\r
+ exportModel.project = project;\r
+ exportModel.selection = selection;\r
+ exportModel.overwrite = overwrite;\r
+ }\r
+\r
+ @Override\r
+ public void addPages() {\r
+ super.addPages();\r
+ if (exportModel != null) {\r
+ addPage(new DrawingTemplateExportPage(exportModel));\r
+ } else {\r
+ addPage(new NoProjectPage("Export Diagram Template"));\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public boolean performFinish() {\r
+ try {\r
+ recentExportPaths.addFirst(exportModel.exportLocation.getAbsolutePath());\r
+ Preferences.removeDuplicates(recentExportPaths);\r
+ if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS)\r
+ recentExportPaths.pollLast();\r
+\r
+ writePreferences();\r
+ } catch (IOException e) {\r
+ ErrorLogger.defaultLogError("Failed to write preferences", e);\r
+ }\r
+\r
+ final File output = exportModel.exportLocation;\r
+ if (output.exists()) {\r
+ if (!exportModel.overwrite) {\r
+ boolean ok = MessageDialog.openConfirm(getShell(), "Overwrite", "A file by the name " + output.getAbsolutePath() + " already exists.\n\nDo you want to overwrite?");\r
+ if (!ok) {\r
+ return false;\r
+ }\r
+ }\r
+ if (!output.delete()) {\r
+ MessageDialog.openError(getShell(), "Delete Problem", "Could not overwrite previously existing file " + output);\r
+ return false;\r
+ }\r
+ }\r
+\r
+ try {\r
+ getContainer().run(true, true, new DrawingTemplateExporter(exportModel));\r
+ } catch (InvocationTargetException e) {\r
+ Throwable t = e.getTargetException();\r
+ WizardPage cp = (WizardPage) getContainer().getCurrentPage();\r
+ if (t instanceof IOException) {\r
+ ErrorLogger.defaultLogError("An I/O problem occurred while exporting diagram template. See exception for details.", t);\r
+ cp.setErrorMessage("An I/O problem occurred while exporting diagram template.\n\nMessage: " + e.getMessage());\r
+ } else {\r
+ ErrorLogger.defaultLogError("Unexpected exception while exporting diagram template. See exception for details.", t);\r
+ cp.setErrorMessage("Unexpected exception while exporting diagram template. See error log for details.\n\nMessage: " + e.getMessage());\r
+ }\r
+ return false;\r
+ } catch (InterruptedException e) {\r
+ ExceptionUtils.logAndShowError(e);\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+}\r