From 35844de9ccc8623b1d3eb145b22d1d025d462d50 Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 6 May 2011 15:22:50 +0000 Subject: [PATCH] Display error messages when trying to import something that should not be imported. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20809 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/handlers/ImportFunctionLibrary.java | 48 ++++++++++++++++--- .../ui/handlers/ImportModelHandler.java | 46 +++++++++++++++--- .../ui/handlers/ImportModuleHandler.java | 48 ++++++++++++++++++- 3 files changed, 127 insertions(+), 15 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportFunctionLibrary.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportFunctionLibrary.java index 7f5a6577..3b6bc8e1 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportFunctionLibrary.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportFunctionLibrary.java @@ -21,6 +21,7 @@ import org.eclipse.core.runtime.Platform; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.databoard.Bindings; @@ -30,8 +31,8 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.WriteOnlyGraph; -import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ResourceNotFoundException; import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; @@ -65,7 +66,7 @@ public class ImportFunctionLibrary extends AbstractHandler { final Resource functionLibrary = r; - Shell shell = HandlerUtil.getActiveShellChecked(event); + final Shell shell = HandlerUtil.getActiveShellChecked(event); FileDialog fd = new FileDialog(shell, SWT.OPEN); fd.setText("Import Function Library"); @@ -85,9 +86,14 @@ public class ImportFunctionLibrary extends AbstractHandler { tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class)); } catch (RuntimeBindingConstructionException e) { e.printStackTrace(); + return null; } catch (IOException e) { - e.printStackTrace(); - } + MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); + mb.setText("Error"); + mb.setMessage("The imported file is not of type: Function Library"); + mb.open(); + return null; + } if(tg == null) return null; @@ -132,11 +138,39 @@ public class ImportFunctionLibrary extends AbstractHandler { e.printStackTrace(); } - SimanticsUI.getSession().asyncRequest(new ReadRequest() { + final Resource root = ia.getRoot(); + + SimanticsUI.getSession().asyncRequest(new WriteRequest() { @Override - public void run(ReadGraph graph) throws DatabaseException { - FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary); + public void perform(WriteGraph graph) throws DatabaseException { + if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) { + Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf); + String type = "..."; + if(instanceOf != null) + type = NameUtils.getSafeName(graph, instanceOf); + else { + Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits); + if(inheritedFrom != null) + type = NameUtils.getSafeName(graph, inheritedFrom); + } + final String ft = type; + graph.deny(root, Layer0.getInstance(graph).PartOf); + + shell.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); + mb.setText("Error"); + mb.setMessage("The imported file is not of type: Function Library (" + ft +")"); + mb.open(); + } + }); + } else { + FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary); + } + } }); return null; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java index 36be1fd5..ddeaec1b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java @@ -21,6 +21,7 @@ import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.runtime.Platform; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.databoard.Bindings; @@ -29,6 +30,7 @@ import org.simantics.databoard.binding.error.RuntimeBindingConstructionException import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; @@ -53,7 +55,7 @@ public class ImportModelHandler extends AbstractHandler { Resource project = SimanticsUI.getProject().get(); if(project == null) return null; - Shell shell = HandlerUtil.getActiveShellChecked(event); + final Shell shell = HandlerUtil.getActiveShellChecked(event); FileDialog fd = new FileDialog(shell, SWT.OPEN); fd.setText("Import Model"); @@ -81,7 +83,11 @@ public class ImportModelHandler extends AbstractHandler { } catch (RuntimeBindingConstructionException e1) { e1.printStackTrace(); } catch (IOException e1) { - e1.printStackTrace(); + MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); + mb.setText("Error"); + mb.setMessage("The imported file is not of type: Model"); + mb.open(); + return null; } } if(tg == null) return null; @@ -92,15 +98,43 @@ public class ImportModelHandler extends AbstractHandler { DefaultPasteHandler.defaultExecute(tg, SimanticsUI.getProject().get(), ia); - // Fix changes made to old ontology versions + // Check that imported resource was actually a model + //and fix changes made to old ontology versions final Resource root = ia.getRoot(); SimanticsUI.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { - updateOldConfigurationToBaseRealization(graph, root); - addDefaultOntologyLinks(graph, root); - addURIsToDiagrams(graph, root); + + if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModel)) { + Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf); + String type = "..."; + if(instanceOf != null) + type = NameUtils.getSafeName(graph, instanceOf); + else { + Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits); + if(inheritedFrom != null) + type = NameUtils.getSafeName(graph, inheritedFrom); + } + final String ft = type; + graph.deny(root, Layer0.getInstance(graph).PartOf); + + shell.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); + mb.setText("Error"); + mb.setMessage("The imported file is not of type: System Dynamics Model (" + ft +")"); + mb.open(); + } + }); + + } else { + updateOldConfigurationToBaseRealization(graph, root); + addDefaultOntologyLinks(graph, root); + addURIsToDiagrams(graph, root); + } } }); } catch (DatabaseException e) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModuleHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModuleHandler.java index 6c0d40fe..acbfc95a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModuleHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModuleHandler.java @@ -21,6 +21,7 @@ import org.eclipse.core.runtime.Platform; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.browsing.ui.common.node.AbstractNode; @@ -28,11 +29,18 @@ import org.simantics.databoard.Bindings; import org.simantics.databoard.Files; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; import org.simantics.graph.db.MissingDependencyException; import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.Activator; +import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.AdaptionUtils; public class ImportModuleHandler extends AbstractHandler { @@ -50,7 +58,7 @@ public class ImportModuleHandler extends AbstractHandler { final Resource model = node.data; - Shell shell = HandlerUtil.getActiveShellChecked(event); + final Shell shell = HandlerUtil.getActiveShellChecked(event); FileDialog fd = new FileDialog(shell, SWT.OPEN); fd.setText("Import Module"); @@ -71,7 +79,11 @@ public class ImportModuleHandler extends AbstractHandler { } catch (RuntimeBindingConstructionException e) { e.printStackTrace(); } catch (IOException e) { - e.printStackTrace(); + MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); + mb.setText("Error"); + mb.setMessage("The imported file is not of type: Module Type"); + mb.open(); + return null; } if(tg == null) return null; @@ -85,6 +97,38 @@ public class ImportModuleHandler extends AbstractHandler { e.printStackTrace(); } + final Resource root = ia.getRoot(); + + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + if(!graph.isInheritedFrom(root, SysdynResource.getInstance(graph).Module)) { + Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf); + String type = "..."; + if(instanceOf != null) + type = NameUtils.getSafeName(graph, instanceOf); + else { + Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits); + if(inheritedFrom != null) + type = NameUtils.getSafeName(graph, inheritedFrom); + } + final String ft = type; + graph.deny(root, Layer0.getInstance(graph).PartOf); + + shell.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); + mb.setText("Error"); + mb.setMessage("The imported file is not of type: System Dynamics Module (" + ft +")"); + mb.open(); + } + }); + } + } + }); return null; } -- 2.47.1