]> gerrit.simantics Code Review - simantics/platform.git/blob
bb0d6f68a7958832f4221bf1738a3523cd34df1b
[simantics/platform.git] /
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.ui.sharedontology.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.preferences.InstanceScope;
19 import org.eclipse.jface.preference.IPersistentPreferenceStore;
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.wizard.Wizard;
23 import org.eclipse.jface.wizard.WizardPage;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.ui.IImportWizard;
26 import org.eclipse.ui.IWorkbench;
27 import org.eclipse.ui.preferences.ScopedPreferenceStore;
28 import org.simantics.Simantics;
29 import org.simantics.databoard.util.URIStringUtils;
30 import org.simantics.db.Resource;
31 import org.simantics.db.layer0.migration.MigratedImportResult;
32 import org.simantics.db.management.ISessionContext;
33 import org.simantics.graph.db.MissingDependencyException;
34 import org.simantics.modeling.ui.Activator;
35 import org.simantics.modeling.ui.utils.NoProjectPage;
36 import org.simantics.project.IProject;
37 import org.simantics.project.ProjectKeys;
38 import org.simantics.ui.SimanticsUI;
39 import org.simantics.utils.strings.EString;
40 import org.simantics.utils.ui.ErrorLogger;
41 import org.simantics.utils.ui.ExceptionUtils;
42 import org.simantics.utils.ui.dialogs.InfoDialog;
43
44 /**
45  * @author Tuukka Lehtonen
46  */
47 public class ModelImportWizard extends Wizard implements IImportWizard {
48
49     private static final int MAX_RECENT_IMPORT_PATHS = 10;
50
51     ImportPlan        importModel;
52
53     private boolean readPreferences(IStructuredSelection selection) {
54         IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
55
56         String recentPathsPref = store.getString(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS);
57         Deque<String> recentImportPaths = Preferences.decodePaths(recentPathsPref);
58
59         ISessionContext ctx = SimanticsUI.getSessionContext();
60         if (ctx == null)
61             return false;
62         IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
63         if (project == null)
64             return false;
65
66         importModel = new ImportPlan(ctx, recentImportPaths);
67         importModel.project = project;
68         importModel.selection = selection.getFirstElement();
69
70         return true;
71     }
72
73     private void writePreferences() throws IOException {
74         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
75
76         store.putValue(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));
77
78         if (store.needsSaving())
79             store.save();
80     }
81
82     public ModelImportWizard() {
83         setWindowTitle("Import Model");
84         setNeedsProgressMonitor(true);
85     }
86
87     @Override
88     public void init(IWorkbench workbench, IStructuredSelection selection) {
89         readPreferences(selection);
90     }
91
92     @Override
93     public void addPages() {
94         super.addPages();
95         if (importModel != null) {
96             addPage(new ModelImportPage(importModel));
97         } else {
98             addPage(new NoProjectPage("Import Model"));
99         }
100     }
101
102     @Override
103     public boolean performFinish() {
104         try {
105             importModel.recentLocations.addFirst(importModel.importLocation.getAbsolutePath());
106             Preferences.removeDuplicates(importModel.recentLocations);
107             if (importModel.recentLocations.size() > MAX_RECENT_IMPORT_PATHS)
108                 importModel.recentLocations.pollLast();
109
110             writePreferences();
111         } catch (IOException e) {
112             ErrorLogger.defaultLogError("Failed to write preferences", e);
113         }
114
115         try {
116             MigratedImportResult[] result = { null };
117             getContainer().run(true, true, monitor -> {
118                 try {
119                     Resource target = Simantics.getProjectResource();
120                     importModel.sessionContext.getSession().markUndoPoint();
121                     result[0] = ModelImporter.doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target, importModel.includeDependencies);
122                 } catch (Exception e) {
123                     throw new InvocationTargetException(e);
124                 } finally {
125                     monitor.done();
126                 }
127             });
128
129             if (result[0].hasMissingExternals()) {
130                 InfoDialog.open(getShell(), "Missing Externals Created",
131                         "The system was unable to find some of the external entities referenced by the imported material. Place-holders have been created for the missing entities.\nThe missing entities are:\n"
132                                 + URIStringUtils.unescape(EString.implode(result[0].tgResult.missingExternals)),
133                         SWT.SHEET);
134             }
135         } catch (InvocationTargetException e) {
136             Throwable cause = e.getCause();
137             WizardPage cp = (WizardPage) getContainer().getCurrentPage();
138             if (cause instanceof MissingDependencyException) {
139                 cp.setErrorMessage("Failed to import model due to missing dependencies.\n" + cause.getMessage());
140                 ErrorLogger.defaultLogError("Model " + importModel.importLocation + " import failed due to missing database dependencies. See exception for details.", cause);
141                 ExceptionUtils.showError("Failed to import model due to missing dependencies.\n\n" + cause.getMessage(), null);
142             } else {
143                 cp.setErrorMessage("Unexpected problem importing model.\nMessage: " + cause.getMessage());
144                 ErrorLogger.defaultLogError("Model " + importModel.importLocation + " import failed unexpectedly. See exception for details.", cause);
145                 ExceptionUtils.showError("Unexpected problem importing model.\n\n" + cause.getMessage(), cause);
146             }
147             return false;
148         } catch (InterruptedException e) {
149             WizardPage cp = (WizardPage) getContainer().getCurrentPage();
150             cp.setErrorMessage("Import interrupted.\nMessage: " + e.getMessage());
151             ErrorLogger.defaultLogError("Model " + importModel.importLocation + " import interrupted.", e);
152             ExceptionUtils.showError("Model import was interrupted.", e);
153             return false;
154         }
155
156         return true;
157     }
158
159 }