1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.wizard;
14 import java.io.IOException;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.Deque;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.preferences.InstanceScope;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.jface.preference.IPersistentPreferenceStore;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.wizard.Wizard;
25 import org.eclipse.jface.wizard.WizardPage;
26 import org.eclipse.ui.IImportWizard;
27 import org.eclipse.ui.IWorkbench;
28 import org.eclipse.ui.preferences.ScopedPreferenceStore;
29 import org.simantics.db.Resource;
30 import org.simantics.db.management.ISessionContext;
31 import org.simantics.modeling.template2d.DiagramTemplates;
32 import org.simantics.modeling.template2d.ui.Activator;
33 import org.simantics.modeling.ui.utils.NoProjectPage;
34 import org.simantics.project.IProject;
35 import org.simantics.project.ProjectKeys;
36 import org.simantics.ui.SimanticsUI;
37 import org.simantics.ui.utils.ResourceAdaptionUtils;
38 import org.simantics.utils.ui.ErrorLogger;
39 import org.simantics.utils.ui.ExceptionUtils;
42 * @author Tuukka Lehtonen
44 public class DrawingTemplateImportWizard extends Wizard implements IImportWizard {
46 private static final int MAX_RECENT_IMPORT_PATHS = 10;
48 ImportPlan importModel;
50 private boolean readPreferences(IStructuredSelection selection) {
51 IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
53 String recentPathsPref = store.getString(Preferences.RECENT_DRAWING_TEMPLATE_IMPORT_LOCATIONS);
54 Deque<String> recentImportPaths = Preferences.decodePaths(recentPathsPref);
56 ISessionContext ctx = SimanticsUI.getSessionContext();
59 IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
63 importModel = new ImportPlan(ctx, recentImportPaths);
64 importModel.project = project;
65 importModel.selection = selection.getFirstElement();
70 private void writePreferences() throws IOException {
71 IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
73 store.putValue(Preferences.RECENT_DRAWING_TEMPLATE_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));
75 if (store.needsSaving())
79 public DrawingTemplateImportWizard() {
80 setWindowTitle("Import Diagram Template");
81 setNeedsProgressMonitor(true);
85 public void init(IWorkbench workbench, IStructuredSelection selection) {
86 readPreferences(selection);
90 public void addPages() {
92 if (importModel != null) {
93 addPage(new DrawingTemplateImportPage(importModel));
95 addPage(new NoProjectPage("Import Diagram Template"));
100 public boolean performFinish() {
102 importModel.recentLocations.addFirst(importModel.importLocation.getAbsolutePath());
103 Preferences.removeDuplicates(importModel.recentLocations);
104 if (importModel.recentLocations.size() > MAX_RECENT_IMPORT_PATHS)
105 importModel.recentLocations.pollLast();
108 } catch (IOException e) {
109 ErrorLogger.defaultLogError("Failed to write preferences", e);
113 getContainer().run(true, true, new IRunnableWithProgress() {
115 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
117 Resource target = ResourceAdaptionUtils.toSingleResource(importModel.selection);
118 DiagramTemplates.importTemplate(monitor, importModel.sessionContext.getSession(), importModel.importLocation, target);
119 } catch (Exception e) {
120 throw new InvocationTargetException(e);
124 } catch (InvocationTargetException e) {
125 Throwable t = e.getTargetException();
126 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
127 if (t instanceof IOException) {
128 cp.setErrorMessage("An I/O problem occurred while importing a diagram template.\n\nMessage: " + e.getMessage());
130 ErrorLogger.defaultLogError(t);
132 } catch (InterruptedException e) {
133 ExceptionUtils.logAndShowError(e);