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();
69 importModel.includeDependencies = store.getBoolean(Preferences.IMPORT_INCLUDE_DEPENDENCIES);
74 private void writePreferences() throws IOException {
75 IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
77 store.putValue(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));
78 store.setValue(Preferences.IMPORT_INCLUDE_DEPENDENCIES, importModel.includeDependencies);
80 if (store.needsSaving())
84 public ModelImportWizard() {
85 setWindowTitle("Import Model");
86 setNeedsProgressMonitor(true);
90 public void init(IWorkbench workbench, IStructuredSelection selection) {
91 readPreferences(selection);
95 public void addPages() {
97 if (importModel != null) {
98 addPage(new ModelImportPage(importModel));
100 addPage(new NoProjectPage("Import Model"));
105 public boolean performFinish() {
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();
113 } catch (IOException e) {
114 ErrorLogger.defaultLogError("Failed to write preferences", e);
118 MigratedImportResult[] result = { null };
119 getContainer().run(true, true, monitor -> {
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);
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)),
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);
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);
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);