From 22c71c71b4aaaef55155b5e7de6abf46eea5d36b Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Tue, 10 Jun 2014 11:09:33 +0000 Subject: [PATCH] refs #4938 Enhancing error handling in Sysdyn Model and Shared Library imports if dependencies are missing from database (e.g. user tries to import a Shared Library with references to Simupedia) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29608 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../handlers/imports/ImportModelHandler.java | 18 +++++++++++++- .../imports/ImportSharedLibraryHandler.java | 20 +++++++++++++--- .../ui/utils/imports/ImportUtilsUI.java | 2 +- .../models/WizardModelsImportPage.java | 24 +++++++++++++------ .../sysdyn/utils/DocumentationUtils.java | 20 +++++++++------- .../sysdyn/utils/imports/ImportUtils.java | 5 ++-- 6 files changed, 67 insertions(+), 22 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java index 48daeb98..2849101c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java @@ -18,6 +18,7 @@ import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; @@ -26,6 +27,8 @@ import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.DatabaseJob; import org.simantics.sysdyn.ui.Activator; import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI; +import org.simantics.utils.ui.ErrorLogger; +import org.simantics.utils.ui.ExceptionUtils; /** * Imports models from exported transferable graph files. @@ -60,7 +63,20 @@ public class ImportModelHandler extends AbstractHandler { @Override protected IStatus run(IProgressMonitor monitor) { - IStatus status = ImportUtilsUI.importModelFile(selected, monitor); + monitor.beginTask("Importing Model " + selected, 3); + IStatus status = null; + try { + monitor.worked(1); + status = ImportUtilsUI.importModelFile(selected, monitor); + monitor.worked(2); + } catch (Exception e) { + ExceptionUtils.logAndShowError("Model import failed, see exception for details", e); + ErrorLogger.defaultLogError("Model import failed, see exception for details", e); + return Status.OK_STATUS; + } finally { + monitor.done(); + } + return status; } }; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportSharedLibraryHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportSharedLibraryHandler.java index 4190860d..8165e545 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportSharedLibraryHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportSharedLibraryHandler.java @@ -25,9 +25,13 @@ import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.DatabaseJob; +import org.simantics.db.common.utils.Logger; +import org.simantics.graph.db.MissingDependencyException; import org.simantics.modeling.ModelingUtils; import org.simantics.sysdyn.ui.Activator; import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI; +import org.simantics.utils.ui.ErrorLogger; +import org.simantics.utils.ui.ExceptionUtils; public class ImportSharedLibraryHandler extends AbstractHandler { @@ -37,7 +41,7 @@ public class ImportSharedLibraryHandler extends AbstractHandler { // Get imported transferable graph file using FileDialog Shell shell = HandlerUtil.getActiveShellChecked(event); FileDialog fd = new FileDialog(shell, SWT.OPEN); - fd.setText("Import Model"); + fd.setText("Import Shared Library"); String path = Activator.getDefault().getPreferenceStore().getString(ImportUtilsUI.IMPORTMODELTPATH); if(path.isEmpty() || !(new File(path).exists())){ @@ -52,11 +56,21 @@ public class ImportSharedLibraryHandler extends AbstractHandler { final String selected = fd.open(); if(selected == null) return null; - Job job = new DatabaseJob("Import model") { + Job job = new DatabaseJob("Import Shared Library") { @Override protected IStatus run(IProgressMonitor monitor) { - ModelingUtils.importSharedOntology(selected); + monitor.beginTask("Importing Shared Library " + selected, 3); + try { + monitor.worked(1); + ModelingUtils.importSharedOntology(selected); + monitor.worked(2); + } catch (Exception e) { + ExceptionUtils.logAndShowError(e); + ErrorLogger.defaultLogError("Shared Library import failed, see exception for details.", e); + } finally { + monitor.done(); + } return Status.OK_STATUS; } }; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/ImportUtilsUI.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/ImportUtilsUI.java index 3e0eed57..9aa85f83 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/ImportUtilsUI.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/ImportUtilsUI.java @@ -35,7 +35,7 @@ public class ImportUtilsUI { /* ********************************* */ /* ******* IMPORT MODEL ********* */ /* ********************************* */ - public static IStatus importModelFile(String path, IProgressMonitor monitor) { + public static IStatus importModelFile(String path, IProgressMonitor monitor) throws Exception { Activator.getDefault().getPreferenceStore().setValue(IMPORTMODELTPATH, (new File(path)).getParent()); return ImportUtils.importModelFile(path, monitor); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsImportPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsImportPage.java index e91798ed..a4c69b9c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsImportPage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsImportPage.java @@ -187,8 +187,18 @@ public class WizardModelsImportPage extends WizardPage{ setErrorMessage("No file selected"); return false; } - - IStatus status = ImportUtilsUI.importModelFile(selected, null); + + try { + IStatus status = ImportUtilsUI.importModelFile(selected, null); + if(status == null || !status.equals(Status.OK_STATUS)) { + setErrorMessage(status.getMessage()); + return false; + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } /* TransferableGraph1 tg = null; @@ -257,11 +267,11 @@ public class WizardModelsImportPage extends WizardPage{ return false; } */ - if(status == null || !status.equals(Status.OK_STATUS)) { - setErrorMessage(status.getMessage()); - return false; - } - return true; +// if(status == null || !status.equals(Status.OK_STATUS)) { +// setErrorMessage(status.getMessage()); +// return false; +// } +// return true; } /** diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java index 2247106a..0d5c6131 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java @@ -481,15 +481,19 @@ public class DocumentationUtils { */ public static Resource importModel(String path) { final DataContainer m = new DataContainer(); - ImportUtils.importModelFile(path, new NullProgressMonitor(), new FunctionImpl1() { + try { + ImportUtils.importModelFile(path, new NullProgressMonitor(), new FunctionImpl1() { - @Override - public WriteRequest apply(Resource model) { - m.set(model); - return null; - } - - }); + @Override + public WriteRequest apply(Resource model) { + m.set(model); + return null; + } + + }); + } catch (Exception e) { + e.printStackTrace(); + } return m.get(); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/imports/ImportUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/imports/ImportUtils.java index 75a83b4b..405fc79f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/imports/ImportUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/imports/ImportUtils.java @@ -135,10 +135,10 @@ public class ImportUtils { /* ********************************* */ /* ******* IMPORT MODEL ********* */ /* ********************************* */ - public static IStatus importModelFile(String path, final IProgressMonitor monitor) { + public static IStatus importModelFile(String path, final IProgressMonitor monitor) throws Exception { return importModelFile(path, monitor, null); } - public static IStatus importModelFile(String path, final IProgressMonitor monitor, Function1 callback) { + public static IStatus importModelFile(String path, final IProgressMonitor monitor, Function1 callback) throws Exception { final Resource project = Simantics.getProject().get(); if(project == null) return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model: project not found", null); @@ -153,6 +153,7 @@ public class ImportUtils { result = MigrationUtils.importMigrated(monitor, Simantics.getSession(), new File(path), state, new DefaultPasteImportAdvisor(project), project); } catch (Exception e1) { e1.printStackTrace(); + throw e1; } if(result == null || !(result instanceof Resource)) { -- 2.47.1