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.annotation.ui.wizard;
15 import java.io.IOException;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.Deque;
18 import java.util.HashMap;
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;
54 * @author Tuukka Lehtonen
56 public class AnnotationTypeImportWizard extends Wizard implements IImportWizard {
58 private static final int MAX_RECENT_IMPORT_PATHS = 10;
60 ImportPlan importModel;
62 private boolean readPreferences(IStructuredSelection selection) {
63 IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
65 String recentPathsPref = store.getString(Preferences.RECENT_ANNOTATION_TYPE_IMPORT_LOCATIONS);
66 Deque<String> recentImportPaths = Preferences.decodePaths(recentPathsPref);
68 ISessionContext ctx = Simantics.getSessionContext();
71 IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
75 importModel = new ImportPlan(ctx, recentImportPaths);
76 importModel.project = project;
77 importModel.selection = selection.getFirstElement();
82 private void writePreferences() throws IOException {
83 IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
85 store.putValue(Preferences.RECENT_ANNOTATION_TYPE_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));
87 if (store.needsSaving())
91 public AnnotationTypeImportWizard() {
92 setWindowTitle("Import Annotation Type");
93 setNeedsProgressMonitor(true);
97 public void init(IWorkbench workbench, IStructuredSelection selection) {
98 readPreferences(selection);
102 public void addPages() {
104 if (importModel != null) {
105 addPage(new AnnotationTypeImportPage(importModel));
107 addPage(new NoProjectPage("Import Annotation Type"));
112 public boolean performFinish() {
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();
120 } catch (IOException e) {
121 ErrorLogger.defaultLogError("Failed to write preferences", e);
125 getContainer().run(true, true, new IRunnableWithProgress() {
127 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
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);
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());
142 ErrorLogger.defaultLogError(t);
144 } catch (InterruptedException e) {
145 ExceptionUtils.logAndShowError(e);
153 public static void doImport(final IProgressMonitor monitor, File modelFile, final Session session, final Resource target)
154 throws IOException, SerializationException, DatabaseException {
156 final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(target);
158 monitor.beginTask("Loading annotation type from disk", 1000);
160 FormatHandler<Object> handler1 = new FormatHandler<Object>() {
162 public Binding getBinding() {
163 return TransferableGraph1.BINDING;
167 public Object process(DataContainer container)
170 monitor.setTaskName("Importing annotation type into database");
171 TransferableGraphs.importGraph1(session, (TransferableGraph1)container.content.getValue(),
178 HashMap<String, FormatHandler<Object>> handlers = new HashMap<String, FormatHandler<Object>>();
179 handlers.put(Constants.ANNOTATION_TYPE_FORMAT_V1, handler1);
182 DataContainers.readFile(modelFile, handlers);
183 } catch(DataFormatException e) {
184 throw new IOException(e);
185 } catch(IOException e) {
187 } catch(Exception e) {
188 if(e instanceof RuntimeException)
189 throw (RuntimeException)e;
191 throw new RuntimeException(e);
193 } catch(IOException e) {
196 monitor.setTaskName("Postprocessing");
199 } catch (Throwable t) {