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