]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/wizard/DrawingTemplateImportWizard.java
ae1e0049bbb5fc912dcc84c3067daa130539c1d7
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / wizard / DrawingTemplateImportWizard.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.wizard;
13
14 import java.io.IOException;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.Deque;
17
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;
40
41 /**
42  * @author Tuukka Lehtonen
43  */
44 public class DrawingTemplateImportWizard extends Wizard implements IImportWizard {
45
46     private static final int MAX_RECENT_IMPORT_PATHS = 10;
47
48     ImportPlan        importModel;
49
50     private boolean readPreferences(IStructuredSelection selection) {
51         IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
52
53         String recentPathsPref = store.getString(Preferences.RECENT_DRAWING_TEMPLATE_IMPORT_LOCATIONS);
54         Deque<String> recentImportPaths = Preferences.decodePaths(recentPathsPref);
55
56         ISessionContext ctx = SimanticsUI.getSessionContext();
57         if (ctx == null)
58             return false;
59         IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
60         if (project == null)
61             return false;
62
63         importModel = new ImportPlan(ctx, recentImportPaths);
64         importModel.project = project;
65         importModel.selection = selection.getFirstElement();
66
67         return true;
68     }
69
70     private void writePreferences() throws IOException {
71         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
72
73         store.putValue(Preferences.RECENT_DRAWING_TEMPLATE_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));
74
75         if (store.needsSaving())
76             store.save();
77     }
78
79     public DrawingTemplateImportWizard() {
80         setWindowTitle("Import Diagram Template");
81         setNeedsProgressMonitor(true);
82     }
83
84     @Override
85     public void init(IWorkbench workbench, IStructuredSelection selection) {
86         readPreferences(selection);
87     }
88
89     @Override
90     public void addPages() {
91         super.addPages();
92         if (importModel != null) {
93             addPage(new DrawingTemplateImportPage(importModel));
94         } else {
95             addPage(new NoProjectPage("Import Diagram Template"));
96         }
97     }
98
99     @Override
100     public boolean performFinish() {
101         try {
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();
106
107             writePreferences();
108         } catch (IOException e) {
109             ErrorLogger.defaultLogError("Failed to write preferences", e);
110         }
111
112         try {
113             getContainer().run(true, true, new IRunnableWithProgress() {
114                 @Override
115                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
116                     try {
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);
121                     }
122                 }
123             });
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());
129             }
130             ErrorLogger.defaultLogError(t);
131             return false;
132         } catch (InterruptedException e) {
133             ExceptionUtils.logAndShowError(e);
134             return false;
135         }
136
137         return true;
138     }
139
140 }