]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/wizard/AnnotationTypeImportWizard.java
Remove usage of deprecated SimanticsUI-methods
[simantics/platform.git] / bundles / org.simantics.annotation.ui / src / org / simantics / annotation / ui / wizard / AnnotationTypeImportWizard.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.annotation.ui.wizard;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.Deque;
18 import java.util.HashMap;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.preferences.InstanceScope;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.eclipse.jface.preference.IPersistentPreferenceStore;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.wizard.Wizard;
27 import org.eclipse.jface.wizard.WizardPage;
28 import org.eclipse.ui.IImportWizard;
29 import org.eclipse.ui.IWorkbench;
30 import org.eclipse.ui.preferences.ScopedPreferenceStore;
31 import org.simantics.Simantics;
32 import org.simantics.annotation.ui.Activator;
33 import org.simantics.databoard.binding.Binding;
34 import org.simantics.databoard.container.DataContainer;
35 import org.simantics.databoard.container.DataContainers;
36 import org.simantics.databoard.container.DataFormatException;
37 import org.simantics.databoard.container.FormatHandler;
38 import org.simantics.databoard.serialization.SerializationException;
39 import org.simantics.db.Resource;
40 import org.simantics.db.Session;
41 import org.simantics.db.exception.DatabaseException;
42 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
43 import org.simantics.db.management.ISessionContext;
44 import org.simantics.graph.db.TransferableGraphs;
45 import org.simantics.graph.representation.TransferableGraph1;
46 import org.simantics.modeling.ui.utils.NoProjectPage;
47 import org.simantics.project.IProject;
48 import org.simantics.project.ProjectKeys;
49 import org.simantics.ui.utils.ResourceAdaptionUtils;
50 import org.simantics.utils.ui.ErrorLogger;
51 import org.simantics.utils.ui.ExceptionUtils;
52
53 /**
54  * @author Tuukka Lehtonen
55  */
56 public class AnnotationTypeImportWizard extends Wizard implements IImportWizard {
57
58     private static final int MAX_RECENT_IMPORT_PATHS = 10;
59
60     ImportPlan        importModel;
61
62     private boolean readPreferences(IStructuredSelection selection) {
63         IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
64
65         String recentPathsPref = store.getString(Preferences.RECENT_ANNOTATION_TYPE_IMPORT_LOCATIONS);
66         Deque<String> recentImportPaths = Preferences.decodePaths(recentPathsPref);
67
68         ISessionContext ctx = Simantics.getSessionContext();
69         if (ctx == null)
70             return false;
71         IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
72         if (project == null)
73             return false;
74
75         importModel = new ImportPlan(ctx, recentImportPaths);
76         importModel.project = project;
77         importModel.selection = selection.getFirstElement();
78
79         return true;
80     }
81
82     private void writePreferences() throws IOException {
83         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
84
85         store.putValue(Preferences.RECENT_ANNOTATION_TYPE_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));
86
87         if (store.needsSaving())
88             store.save();
89     }
90
91     public AnnotationTypeImportWizard() {
92         setWindowTitle("Import Annotation Type");
93         setNeedsProgressMonitor(true);
94     }
95
96     @Override
97     public void init(IWorkbench workbench, IStructuredSelection selection) {
98         readPreferences(selection);
99     }
100
101     @Override
102     public void addPages() {
103         super.addPages();
104         if (importModel != null) {
105             addPage(new AnnotationTypeImportPage(importModel));
106         } else {
107             addPage(new NoProjectPage("Import Annotation Type"));
108         }
109     }
110
111     @Override
112     public boolean performFinish() {
113         try {
114                 importModel.recentLocations.addFirst(importModel.importLocation.getAbsolutePath());
115             Preferences.removeDuplicates(importModel.recentLocations);
116             if (importModel.recentLocations.size() > MAX_RECENT_IMPORT_PATHS)
117                 importModel.recentLocations.pollLast();
118
119             writePreferences();
120         } catch (IOException e) {
121             ErrorLogger.defaultLogError("Failed to write preferences", e);
122         }
123
124         try {
125             getContainer().run(true, true, new IRunnableWithProgress() {
126                 @Override
127                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
128                     try {
129                         Resource target = ResourceAdaptionUtils.toSingleResource(importModel.selection);
130                         doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target);
131                     } catch (Exception e) {
132                         throw new InvocationTargetException(e);
133                     }
134                 }
135             });
136         } catch (InvocationTargetException e) {
137             Throwable t = e.getTargetException();
138             WizardPage cp = (WizardPage) getContainer().getCurrentPage();
139             if (t instanceof IOException) {
140                 cp.setErrorMessage("An I/O problem occurred while importing annotation type.\n\nMessage: " + e.getMessage());
141             }
142             ErrorLogger.defaultLogError(t);
143             return false;
144         } catch (InterruptedException e) {
145             ExceptionUtils.logAndShowError(e);
146             return false;
147         }
148
149         return true;
150     }
151
152
153     public static void doImport(final IProgressMonitor monitor, File modelFile, final Session session, final Resource target)
154     throws IOException, SerializationException, DatabaseException {
155         try {
156             final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(target);
157
158             monitor.beginTask("Loading annotation type from disk", 1000);
159             try {
160                 FormatHandler<Object> handler1 = new FormatHandler<Object>() {
161                     @Override
162                     public Binding getBinding() {
163                         return TransferableGraph1.BINDING;
164                     }
165
166                     @Override
167                     public Object process(DataContainer container)
168                             throws Exception {
169                         monitor.worked(100);
170                         monitor.setTaskName("Importing annotation type into database");
171                         TransferableGraphs.importGraph1(session, (TransferableGraph1)container.content.getValue(), 
172                                 advisor, null);
173                         monitor.worked(850);
174                         return null;
175                     }
176                 };
177
178                 HashMap<String, FormatHandler<Object>> handlers = new HashMap<String, FormatHandler<Object>>();
179                 handlers.put(Constants.ANNOTATION_TYPE_FORMAT_V1, handler1);
180
181                 try {
182                     DataContainers.readFile(modelFile, handlers);
183                 } catch(DataFormatException e) {
184                     throw new IOException(e);
185                 } catch(IOException e) {
186                     throw e;
187                 } catch(Exception e) {
188                     if(e instanceof RuntimeException)
189                         throw (RuntimeException)e;
190                     else
191                         throw new RuntimeException(e);
192                 }
193             } catch(IOException e) {
194             }
195
196             monitor.setTaskName("Postprocessing");
197             monitor.worked(50);
198
199         } catch (Throwable t) {
200             t.printStackTrace();
201         } finally {
202             monitor.done();
203         }
204     }
205
206 }