]> gerrit.simantics Code Review - simantics/platform.git/blob
011ce12ff538f1764d194ba5c2ef74c644382bc5
[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         importModel.includeDependencies = store.getBoolean(Preferences.IMPORT_INCLUDE_DEPENDENCIES);
70
71         return true;
72     }
73
74     private void writePreferences() throws IOException {
75         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
76
77         store.putValue(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));
78         store.setValue(Preferences.IMPORT_INCLUDE_DEPENDENCIES, importModel.includeDependencies);
79
80         if (store.needsSaving())
81             store.save();
82     }
83
84     public ModelImportWizard() {
85         setWindowTitle("Import Model");
86         setNeedsProgressMonitor(true);
87     }
88
89     @Override
90     public void init(IWorkbench workbench, IStructuredSelection selection) {
91         readPreferences(selection);
92     }
93
94     @Override
95     public void addPages() {
96         super.addPages();
97         if (importModel != null) {
98             addPage(new ModelImportPage(importModel));
99         } else {
100             addPage(new NoProjectPage("Import Model"));
101         }
102     }
103
104     @Override
105     public boolean performFinish() {
106         try {
107             importModel.recentLocations.addFirst(importModel.importLocation.getAbsolutePath());
108             Preferences.removeDuplicates(importModel.recentLocations);
109             if (importModel.recentLocations.size() > MAX_RECENT_IMPORT_PATHS)
110                 importModel.recentLocations.pollLast();
111
112             writePreferences();
113         } catch (IOException e) {
114             ErrorLogger.defaultLogError("Failed to write preferences", e);
115         }
116
117         try {
118             MigratedImportResult[] result = { null };
119             getContainer().run(true, true, monitor -> {
120                 try {
121                     Resource target = Simantics.getProjectResource();
122                     importModel.sessionContext.getSession().markUndoPoint();
123                     result[0] = ModelImporter.doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target, importModel.includeDependencies);
124                 } catch (Exception e) {
125                     throw new InvocationTargetException(e);
126                 } finally {
127                     monitor.done();
128                 }
129             });
130
131             if (result[0].hasMissingExternals()) {
132                 InfoDialog.open(getShell(), "Missing Externals Created",
133                         "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"
134                                 + URIStringUtils.unescape(EString.implode(result[0].tgResult.missingExternals)),
135                         SWT.SHEET);
136             }
137         } catch (InvocationTargetException e) {
138             Throwable cause = e.getCause();
139             WizardPage cp = (WizardPage) getContainer().getCurrentPage();
140             if (cause instanceof MissingDependencyException) {
141                 cp.setErrorMessage("Failed to import model due to missing dependencies.\n" + cause.getMessage());
142                 ErrorLogger.defaultLogError("Model " + importModel.importLocation + " import failed due to missing database dependencies. See exception for details.", cause);
143                 ExceptionUtils.showError("Failed to import model due to missing dependencies.\n\n" + cause.getMessage(), null);
144             } else {
145                 cp.setErrorMessage("Unexpected problem importing model.\nMessage: " + cause.getMessage());
146                 ErrorLogger.defaultLogError("Model " + importModel.importLocation + " import failed unexpectedly. See exception for details.", cause);
147                 ExceptionUtils.showError("Unexpected problem importing model.\n\n" + cause.getMessage(), cause);
148             }
149             return false;
150         } catch (InterruptedException e) {
151             WizardPage cp = (WizardPage) getContainer().getCurrentPage();
152             cp.setErrorMessage("Import interrupted.\nMessage: " + e.getMessage());
153             ErrorLogger.defaultLogError("Model " + importModel.importLocation + " import interrupted.", e);
154             ExceptionUtils.showError("Model import was interrupted.", e);
155             return false;
156         }
157
158         return true;
159     }
160
161 }