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