]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Display error messages when trying to import something that should not be imported.
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 6 May 2011 15:22:50 +0000 (15:22 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 6 May 2011 15:22:50 +0000 (15:22 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20809 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportFunctionLibrary.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModuleHandler.java

index 7f5a657750962c9ab74f002c52ac4fc2e06766ca..3b6bc8e1edcfced07e3d3796413a6b12a425dcea 100644 (file)
@@ -21,6 +21,7 @@ import org.eclipse.core.runtime.Platform;
 import org.eclipse.jface.viewers.ISelection;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.MessageBox;\r
 import org.eclipse.swt.widgets.Shell;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
 import org.simantics.databoard.Bindings;\r
@@ -30,8 +31,8 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.WriteOnlyGraph;\r
-import org.simantics.db.common.request.ReadRequest;\r
 import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.exception.ResourceNotFoundException;\r
 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
@@ -65,7 +66,7 @@ public class ImportFunctionLibrary  extends AbstractHandler {
         \r
         final Resource functionLibrary = r;\r
         \r
-               Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+               final Shell shell = HandlerUtil.getActiveShellChecked(event);\r
                FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
                fd.setText("Import Function Library");\r
                \r
@@ -85,9 +86,14 @@ public class ImportFunctionLibrary  extends AbstractHandler {
                        tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
                } catch (RuntimeBindingConstructionException e) {\r
                        e.printStackTrace();\r
+                       return null;\r
                } catch (IOException e) {\r
-                       e.printStackTrace();\r
-               }\r
+                       MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+                       mb.setText("Error");\r
+                       mb.setMessage("The imported file is not of type: Function Library");\r
+                       mb.open();\r
+                       return null;\r
+               } \r
                if(tg == null) return null;\r
 \r
        \r
@@ -132,11 +138,39 @@ public class ImportFunctionLibrary  extends AbstractHandler {
                        e.printStackTrace();\r
                }\r
                \r
-               SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
+               final Resource root = ia.getRoot();\r
+               \r
+               SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
                        \r
                        @Override\r
-                       public void run(ReadGraph graph) throws DatabaseException {\r
-                               FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary);\r
+                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                               if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) {\r
+                                       Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);\r
+                                       String type = "...";\r
+                                       if(instanceOf != null)\r
+                                               type = NameUtils.getSafeName(graph, instanceOf);\r
+                                       else {\r
+                                               Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);\r
+                                               if(inheritedFrom != null)\r
+                                                       type = NameUtils.getSafeName(graph, inheritedFrom);\r
+                                       }\r
+                                       final String ft = type; \r
+                                       graph.deny(root, Layer0.getInstance(graph).PartOf);\r
+                                       \r
+                                       shell.getDisplay().asyncExec(new Runnable() {\r
+                                               \r
+                                               @Override\r
+                                               public void run() {\r
+                                                       MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+                                                       mb.setText("Error");\r
+                                                       mb.setMessage("The imported file is not of type: Function Library (" + ft +")");\r
+                                                       mb.open();                                                              \r
+                                               }\r
+                                       });\r
+                               } else {\r
+                                       FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary);\r
+                               }\r
+\r
                        }\r
                });\r
                return null;\r
index 36be1fd5a097ae0d6e5a9a2a907e72cd9f5ede30..ddeaec1bc6b29a328a69554e45eedb916d74fc11 100644 (file)
@@ -21,6 +21,7 @@ import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.runtime.Platform;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.MessageBox;\r
 import org.eclipse.swt.widgets.Shell;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
 import org.simantics.databoard.Bindings;\r
@@ -29,6 +30,7 @@ import org.simantics.databoard.binding.error.RuntimeBindingConstructionException
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
@@ -53,7 +55,7 @@ public class ImportModelHandler extends AbstractHandler {
                Resource project = SimanticsUI.getProject().get();\r
                if(project == null) return null;\r
 \r
-               Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+               final Shell shell = HandlerUtil.getActiveShellChecked(event);\r
                FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
                fd.setText("Import Model");\r
                \r
@@ -81,7 +83,11 @@ public class ImportModelHandler extends AbstractHandler {
                        } catch (RuntimeBindingConstructionException e1) {\r
                                e1.printStackTrace();\r
                        } catch (IOException e1) {\r
-                               e1.printStackTrace();\r
+                               MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+                               mb.setText("Error");\r
+                               mb.setMessage("The imported file is not of type: Model");\r
+                               mb.open();\r
+                               return null;\r
                        }\r
                }\r
                if(tg == null) return null;\r
@@ -92,15 +98,43 @@ public class ImportModelHandler extends AbstractHandler {
                        DefaultPasteHandler.defaultExecute(tg, SimanticsUI.getProject().get(), ia);\r
 \r
                        \r
-                       // Fix changes made to old ontology versions\r
+                       // Check that imported resource was actually a model \r
+                       //and fix changes made to old ontology versions\r
                        final Resource root = ia.getRoot();\r
                        SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
                                \r
                                @Override\r
                                public void perform(WriteGraph graph) throws DatabaseException {\r
-                                       updateOldConfigurationToBaseRealization(graph, root);\r
-                                       addDefaultOntologyLinks(graph, root);\r
-                                       addURIsToDiagrams(graph, root);\r
+                                       \r
+                                       if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModel)) {\r
+                                               Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);\r
+                                               String type = "...";\r
+                                               if(instanceOf != null)\r
+                                                       type = NameUtils.getSafeName(graph, instanceOf);\r
+                                               else {\r
+                                                       Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);\r
+                                                       if(inheritedFrom != null)\r
+                                                               type = NameUtils.getSafeName(graph, inheritedFrom);\r
+                                               }\r
+                                               final String ft = type; \r
+                                               graph.deny(root, Layer0.getInstance(graph).PartOf);\r
+                                               \r
+                                               shell.getDisplay().asyncExec(new Runnable() {\r
+                                                       \r
+                                                       @Override\r
+                                                       public void run() {\r
+                                                               MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+                                                               mb.setText("Error");\r
+                                                               mb.setMessage("The imported file is not of type: System Dynamics Model (" + ft +")");\r
+                                                               mb.open();                                                              \r
+                                                       }\r
+                                               });\r
+\r
+                                       } else {\r
+                                               updateOldConfigurationToBaseRealization(graph, root);\r
+                                               addDefaultOntologyLinks(graph, root);\r
+                                               addURIsToDiagrams(graph, root);\r
+                                       }\r
                                }\r
                        });\r
                } catch (DatabaseException e) {\r
index 6c0d40fe44618f9cd66f4afd2fc0720e989482ae..acbfc95ac1f9d1a2e9140fab5749f6c135b45f46 100644 (file)
@@ -21,6 +21,7 @@ import org.eclipse.core.runtime.Platform;
 import org.eclipse.jface.viewers.ISelection;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.MessageBox;\r
 import org.eclipse.swt.widgets.Shell;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
 import org.simantics.browsing.ui.common.node.AbstractNode;\r
@@ -28,11 +29,18 @@ import org.simantics.databoard.Bindings;
 import org.simantics.databoard.Files;\r
 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
 import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
 import org.simantics.graph.db.MissingDependencyException;\r
 import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.ui.Activator;\r
+import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.utils.AdaptionUtils;\r
 \r
 public class ImportModuleHandler extends AbstractHandler {\r
@@ -50,7 +58,7 @@ public class ImportModuleHandler extends AbstractHandler {
 \r
         final Resource model = node.data;\r
         \r
-               Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+               final Shell shell = HandlerUtil.getActiveShellChecked(event);\r
                FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
                fd.setText("Import Module");\r
                \r
@@ -71,7 +79,11 @@ public class ImportModuleHandler extends AbstractHandler {
                } catch (RuntimeBindingConstructionException e) {\r
                        e.printStackTrace();\r
                } catch (IOException e) {\r
-                       e.printStackTrace();\r
+                       MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+                       mb.setText("Error");\r
+                       mb.setMessage("The imported file is not of type: Module Type");\r
+                       mb.open();\r
+                       return null;\r
                }\r
                if(tg == null) return null;\r
 \r
@@ -85,6 +97,38 @@ public class ImportModuleHandler extends AbstractHandler {
                        e.printStackTrace();\r
                }\r
                \r
+               final Resource root = ia.getRoot();\r
+               \r
+               SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+                       \r
+                       @Override\r
+                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                               if(!graph.isInheritedFrom(root, SysdynResource.getInstance(graph).Module)) {\r
+                                       Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);\r
+                                       String type = "...";\r
+                                       if(instanceOf != null)\r
+                                               type = NameUtils.getSafeName(graph, instanceOf);\r
+                                       else {\r
+                                               Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);\r
+                                               if(inheritedFrom != null)\r
+                                                       type = NameUtils.getSafeName(graph, inheritedFrom);\r
+                                       }\r
+                                       final String ft = type; \r
+                                       graph.deny(root, Layer0.getInstance(graph).PartOf);\r
+                                       \r
+                                       shell.getDisplay().asyncExec(new Runnable() {\r
+                                               \r
+                                               @Override\r
+                                               public void run() {\r
+                                                       MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+                                                       mb.setText("Error");\r
+                                                       mb.setMessage("The imported file is not of type: System Dynamics Module (" + ft +")");\r
+                                                       mb.open();                                                              \r
+                                               }\r
+                                       });\r
+                               } \r
+                       }\r
+               });\r
                return null;\r
        }\r
 \r