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.modeling.ui.sharedontology.wizard;
14 import java.io.IOException;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.Deque;
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;
45 * @author Tuukka Lehtonen
47 public class ModelImportWizard extends Wizard implements IImportWizard {
49 private static final int MAX_RECENT_IMPORT_PATHS = 10;
51 ImportPlan importModel;
53 private boolean readPreferences(IStructuredSelection selection) {
54 IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
56 String recentPathsPref = store.getString(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS);
57 Deque<String> recentImportPaths = Preferences.decodePaths(recentPathsPref);
59 ISessionContext ctx = SimanticsUI.getSessionContext();
62 IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
66 importModel = new ImportPlan(ctx, recentImportPaths);
67 importModel.project = project;
68 importModel.selection = selection.getFirstElement();
73 private void writePreferences() throws IOException {
74 IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
76 store.putValue(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));
78 if (store.needsSaving())
82 public ModelImportWizard() {
83 setWindowTitle("Import Model");
84 setNeedsProgressMonitor(true);
88 public void init(IWorkbench workbench, IStructuredSelection selection) {
89 readPreferences(selection);
93 public void addPages() {
95 if (importModel != null) {
96 addPage(new ModelImportPage(importModel));
98 addPage(new NoProjectPage("Import Model"));
103 public boolean performFinish() {
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();
111 } catch (IOException e) {
112 ErrorLogger.defaultLogError("Failed to write preferences", e);
116 MigratedImportResult[] result = { null };
117 getContainer().run(true, true, monitor -> {
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);
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)),
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);
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);
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);