]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Import and export of modules and function libraries (refs #3414)
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 21 May 2012 09:42:22 +0000 (09:42 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 21 May 2012 09:42:22 +0000 (09:42 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25023 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportFunctionLibrary.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModuleHandler.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportFunctionLibrary.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModuleHandler.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variability.java

index 0b65cfd8e4ed6270dd7ee0e30aebb47454527935..0784dd7c6ddeb00cec3f64a4b39bd7c8291be731 100644 (file)
@@ -25,8 +25,10 @@ import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.swt.widgets.Shell;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
 import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.Files;\r
 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.container.DataContainer;\r
+import org.simantics.databoard.container.DataContainers;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
@@ -107,7 +109,7 @@ public class ExportFunctionLibrary  extends AbstractHandler {
                                TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, functionLibrary));\r
 \r
                                try {\r
-                                       Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
+                                       DataContainers.writeFile(new File(selected), new DataContainer("sysdynFunctionLibrary", 1, new Variant(TransferableGraph1.BINDING, tg)));\r
                                } catch (RuntimeBindingConstructionException e) {\r
                                        e.printStackTrace();\r
                                } catch (IOException e) {\r
index 7b12325e14b17408ecf437ab3ba6e9d5fad37db7..34958a0e82fcad94454adebf741be3bee4a14ef1 100644 (file)
@@ -27,8 +27,10 @@ import org.eclipse.swt.widgets.MessageBox;
 import org.eclipse.swt.widgets.Shell;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
 import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.Files;\r
 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.container.DataContainer;\r
+import org.simantics.databoard.container.DataContainers;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
@@ -246,7 +248,7 @@ public class ExportModuleHandler extends AbstractHandler {
                                graph.deny(replacement.first, sr.Redeclaration_replacedEnumeration_Inverse, replacement.second);\r
                        \r
                        TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, component));\r
-                       Files.createFile(new File(path), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
+                       DataContainers.writeFile(new File(path), new DataContainer("sysdynModule", 1, new Variant(TransferableGraph1.BINDING, tg)));\r
                        \r
                        for(Pair<Resource,Resource> replacement : replacements)\r
                                graph.claim(replacement.first, sr.Redeclaration_replacedEnumeration_Inverse, replacement.second);\r
index b20206c43b08102b72e9f0aa61576ed3b37101c3..dad7c060cbbad5568fb7096168701c171e679936 100644 (file)
@@ -13,6 +13,7 @@ package org.simantics.sysdyn.ui.handlers.imports;
 \r
 import java.io.File;\r
 import java.io.IOException;\r
+import java.util.HashMap;\r
 \r
 import org.eclipse.core.commands.AbstractHandler;\r
 import org.eclipse.core.commands.ExecutionEvent;\r
@@ -25,8 +26,11 @@ import org.eclipse.swt.widgets.MessageBox;
 import org.eclipse.swt.widgets.Shell;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
 import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.Files;\r
-import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.container.DataContainer;\r
+import org.simantics.databoard.container.DataContainers;\r
+import org.simantics.databoard.container.DataFormatException;\r
+import org.simantics.databoard.container.FormatHandler;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
@@ -91,127 +95,113 @@ public class ImportFunctionLibrary  extends AbstractHandler {
 \r
                Activator.getDefault().getPreferenceStore().setValue(IMPORTFUNCTIONLIBRARYPATH, (new File(selected)).getParent());\r
                \r
-               // Load the transferable graph\r
-               TransferableGraph1 tg = null;\r
-               try {\r
-                       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
-                       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
-               // Make sure the "http://SharedOntologies resource exists\r
-               try {\r
-                       Boolean hasSharedOntologies;\r
-                       hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
+               // Ensure that shared functions ontology exists\r
+               ensureSharedOntologies();\r
+               \r
+               // Handler for importing function libraries\r
+               HashMap<String, FormatHandler<Object>> handlers = new HashMap<String, FormatHandler<Object>>();\r
+               handlers.put("sysdynFunctionLibrary:1", new FormatHandler<Object>() {\r
+                       @Override\r
+                       public Binding getBinding() {\r
+                               return TransferableGraph1.BINDING;\r
+                       }\r
 \r
-                               @Override\r
-                               public Boolean perform(ReadGraph graph) throws DatabaseException {\r
-                                       try {\r
-                                               graph.getResource("http://SharedOntologies");\r
-                                       } catch (ResourceNotFoundException e) {\r
-                                               return false;\r
-                                       }               \r
-                                       return true;\r
+                       @Override\r
+                       public Object process(DataContainer container) throws DataFormatException {\r
+                               SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(functionLibrary);\r
+                               try {\r
+                                       DefaultPasteHandler.defaultExecute((TransferableGraph1)container.content.getValue(), functionLibrary, ia);\r
+                                       return ia.getRoot();\r
+                               } catch (Exception e) {\r
+                                       e.printStackTrace();\r
+                                       return null;\r
                                }\r
-                       });\r
-\r
-                       if(!hasSharedOntologies) {\r
-                               SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
-\r
-                                       @Override\r
-                                       public void perform(WriteGraph graph) throws DatabaseException {\r
-                                               Layer0 l0 = Layer0.getInstance(graph);\r
-                                               GraphUtils.create2(graph, l0.Library, \r
-                                                               l0.HasName, "SharedOntologies",\r
-                                                               l0.PartOf, graph.getResource("http:/"));\r
-                                       }\r
-                               });\r
-\r
                        }\r
-               } catch (DatabaseException e) {\r
-                       e.printStackTrace();\r
-                       return null;\r
-               }\r
+               });\r
 \r
-               \r
-               // Import a function library from the transferable graph\r
-               SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(functionLibrary);\r
+               // Read function library file\r
+               Object result = null;\r
                try {\r
-                       DefaultPasteHandler.defaultExecute(tg, functionLibrary, ia);\r
-               } catch (Exception e) {\r
+                       File file = new File(selected);\r
+                       if(!file.isFile()) {\r
+                               displayErrorMessage(shell, "File " + selected + " not found");\r
+                               return null;\r
+                       } else {        \r
+                               result = DataContainers.readFile(file, handlers);\r
+                       }\r
+               } catch(DataFormatException e) {\r
+                       e.printStackTrace();\r
+               } catch(IOException e) {\r
                        e.printStackTrace();\r
+               } catch(Exception e) {\r
+                       if(e instanceof RuntimeException)\r
+                               throw (RuntimeException)e;\r
+                       else\r
+                               throw new RuntimeException(e);\r
                }\r
                \r
-               final Resource root = ia.getRoot(); // Root of the library\r
-               \r
-               // Link the imported library to the selected resource (functionLibrary)\r
-               SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
-                       \r
-                   /**\r
-                    * Link the imported library to the selected resource (functionLibrary)\r
-                    * The imported library can be either SysdynModelicaFunctionLibrary or SysdynFunctionOntology\r
-                    */\r
-                       @Override\r
-                       public void perform(WriteGraph graph) throws DatabaseException {\r
-                           Layer0 l0 = Layer0.getInstance(graph);\r
-                           // Case: SharedFunctionOntology. Link to SharedOntologies\r
-                           if(graph.isInstanceOf(root, SysdynResource.getInstance(graph).SharedFunctionOntology)) {\r
-                               Resource library = graph.getResource("http://SharedOntologies");\r
-                               if(!graph.hasStatement(library, l0.ConsistsOf, root)) {\r
-                                   graph.claim(library, l0.ConsistsOf, root);\r
-                               }\r
+               if(result == null || !(result instanceof Resource)) {\r
+                       displayErrorMessage(shell, "Function library could not be imported.");\r
+               } else {\r
+                       final Resource FunctionLibraryRoot = (Resource) result;\r
+                       // Link the imported library to the selected resource (functionLibrary)\r
+                       SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
 \r
-                               // Link model to the shared library\r
-                               SysdynResource sr = SysdynResource.getInstance(graph);\r
-                               Resource model = functionLibrary;\r
-                               while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology))\r
-                                   model = graph.getSingleObject(model, l0.PartOf);\r
-                               if(graph.isInstanceOf(model, sr.SysdynModel)) {\r
-                                   graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, root);\r
-                               }\r
+                               /**\r
+                                * Link the imported library to the selected resource (functionLibrary)\r
+                                * The imported library can be either SysdynModelicaFunctionLibrary or SysdynFunctionOntology\r
+                                */\r
+                               @Override\r
+                               public void perform(WriteGraph graph) throws DatabaseException {\r
+                                       Layer0 l0 = Layer0.getInstance(graph);\r
+                                       // Case: SharedFunctionOntology. Link to SharedOntologies\r
+                                       if(graph.isInstanceOf(FunctionLibraryRoot, SysdynResource.getInstance(graph).SharedFunctionOntology)) {\r
+                                               Resource library = graph.getResource("http://SharedOntologies");\r
+                                               if(!graph.hasStatement(library, l0.ConsistsOf, FunctionLibraryRoot)) {\r
+                                                       graph.claim(library, l0.ConsistsOf, FunctionLibraryRoot);\r
+                                               }\r
 \r
-                               // Case: not SharedFunctionOntology or SysdynModelicaFunctionLibrary. \r
-                           } else if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) {\r
-                                       Resource instanceOf = graph.getPossibleObject(root,l0.InstanceOf);\r
-                                       String type = "...";\r
-                                       if(instanceOf != null)\r
-                                               type = NameUtils.getSafeName(graph, instanceOf);\r
-                                       else {\r
-                                               Resource inheritedFrom = graph.getPossibleObject(root, l0.Inherits);\r
-                                               if(inheritedFrom != null)\r
-                                                       type = NameUtils.getSafeName(graph, inheritedFrom);\r
-                                       }\r
-                                       final String ft = type; \r
-                                       \r
-                                       // Remove the functionLibrary ConsistsOf root relation\r
-                                       graph.deny(root, l0.PartOf);\r
-                                       \r
-                                       // Display error message\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
+                                               // Link model to the shared library\r
+                                               SysdynResource sr = SysdynResource.getInstance(graph);\r
+                                               Resource model = functionLibrary;\r
+                                               while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology))\r
+                                                       model = graph.getSingleObject(model, l0.PartOf);\r
+                                               if(graph.isInstanceOf(model, sr.SysdynModel)) {\r
+                                                       graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, FunctionLibraryRoot);\r
                                                }\r
-                                       });\r
-                               } else {\r
-                                   // imported library is already in the right place. Update function library files.\r
-                                       FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary);\r
-                               }\r
 \r
-                       }\r
-               });\r
+                                               // Case: not SharedFunctionOntology or SysdynModelicaFunctionLibrary. \r
+                                       } else if(!graph.isInstanceOf(FunctionLibraryRoot, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) {\r
+                                               Resource instanceOf = graph.getPossibleObject(FunctionLibraryRoot,l0.InstanceOf);\r
+                                               String type = "...";\r
+                                               if(instanceOf != null)\r
+                                                       type = NameUtils.getSafeName(graph, instanceOf);\r
+                                               else {\r
+                                                       Resource inheritedFrom = graph.getPossibleObject(FunctionLibraryRoot, l0.Inherits);\r
+                                                       if(inheritedFrom != null)\r
+                                                               type = NameUtils.getSafeName(graph, inheritedFrom);\r
+                                               }\r
+                                               final String ft = type; \r
+\r
+                                               // Remove the functionLibrary ConsistsOf root relation\r
+                                               graph.deny(FunctionLibraryRoot, l0.PartOf);\r
+\r
+                                               // Display error message\r
+                                               shell.getDisplay().asyncExec(new Runnable() {\r
+\r
+                                                       @Override\r
+                                                       public void run() {\r
+                                                               displayErrorMessage(shell, "The imported file is not of type: Function Library (" + ft +")");                                                   \r
+                                                       }\r
+                                               });\r
+                                       } else {\r
+                                               // imported library is already in the right place. Update function library files.\r
+                                               FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary);\r
+                                       }\r
+\r
+                               }\r
+                       });\r
+               }\r
                return null;\r
        }\r
        \r
@@ -252,5 +242,48 @@ public class ImportFunctionLibrary  extends AbstractHandler {
                }\r
                        \r
        }\r
+       \r
+       private void ensureSharedOntologies() {\r
+               // Make sure the "http://SharedOntologies resource exists\r
+               try {\r
+                       Boolean hasSharedOntologies;\r
+                       hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
+\r
+                               @Override\r
+                               public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+                                       try {\r
+                                               graph.getResource("http://SharedOntologies");\r
+                                       } catch (ResourceNotFoundException e) {\r
+                                               return false;\r
+                                       }               \r
+                                       return true;\r
+                               }\r
+                       });\r
+\r
+                       if(!hasSharedOntologies) {\r
+                               SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+\r
+                                       @Override\r
+                                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                                               Layer0 l0 = Layer0.getInstance(graph);\r
+                                               GraphUtils.create2(graph, l0.Library, \r
+                                                               l0.HasName, "SharedOntologies",\r
+                                                               l0.PartOf, graph.getResource("http:/"));\r
+                                       }\r
+                               });\r
+\r
+                       }\r
+               } catch (DatabaseException e) {\r
+                       e.printStackTrace();\r
+               }\r
+\r
+       }\r
+       \r
+       private void displayErrorMessage(Shell shell, String message) {\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
+       }\r
 \r
 }\r
index 03c8bc65a072ae2585d37347adaa8c10e82d7777..64c06a70f81e7be49041ebd056e37017f80f1426 100644 (file)
@@ -87,7 +87,7 @@ public class ImportModelHandler extends AbstractHandler {
                        public Object process(DataContainer container) throws DataFormatException {\r
                                DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(project);\r
                                try {\r
-                                       DefaultPasteHandler.defaultExecute((TransferableGraph1)container.content.getValue(), SimanticsUI.getProject().get(), ia);\r
+                                       DefaultPasteHandler.defaultExecute((TransferableGraph1)container.content.getValue(), project, ia);\r
                                        return ia.getRoot();\r
 \r
                                } catch (Exception e) {\r
@@ -144,10 +144,7 @@ public class ImportModelHandler extends AbstractHandler {
 \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
+                                                                       displayErrorMessage(shell, "The imported file is not of type: System Dynamics Model (" + ft +")");\r
                                                                }\r
                                                        });\r
 \r
index 9a0cfceb746289371ad1def87b7832c49076d0f8..cf42242512b7b42a3bce196a002d8747ab9e965c 100644 (file)
@@ -13,6 +13,7 @@ package org.simantics.sysdyn.ui.handlers.imports;
 \r
 import java.io.File;\r
 import java.io.IOException;\r
+import java.util.HashMap;\r
 \r
 import org.eclipse.core.commands.AbstractHandler;\r
 import org.eclipse.core.commands.ExecutionEvent;\r
@@ -25,9 +26,11 @@ import org.eclipse.swt.widgets.MessageBox;
 import org.eclipse.swt.widgets.Shell;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
 import org.simantics.browsing.ui.common.node.AbstractNode;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.Files;\r
-import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.container.DataContainer;\r
+import org.simantics.databoard.container.DataContainers;\r
+import org.simantics.databoard.container.DataFormatException;\r
+import org.simantics.databoard.container.FormatHandler;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.request.WriteRequest;\r
@@ -35,7 +38,6 @@ import org.simantics.db.common.utils.NameUtils;
 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
@@ -51,95 +53,122 @@ import org.simantics.ui.utils.AdaptionUtils;
  */\r
 public class ImportModuleHandler extends AbstractHandler {\r
 \r
-    public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH";\r
-\r
-    @Override\r
-    public Object execute(ExecutionEvent event) throws ExecutionException {\r
-        ISelection sel = HandlerUtil.getCurrentSelection(event);\r
-\r
-        @SuppressWarnings("unchecked")\r
-        AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
-        if (node == null)\r
-            return null;\r
-\r
-        final Resource model = node.data;\r
-\r
-        // Get imported transferable graph file using FileDialog\r
-        final Shell shell = HandlerUtil.getActiveShellChecked(event);\r
-        FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
-        fd.setText("Import Module");\r
-\r
-        String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODULETPATH);\r
-        if(path.isEmpty() || !(new File(path).exists()))\r
-            path = Platform.getLocation().toOSString();\r
-        fd.setFilterPath(path);\r
-        String[] filterExt = {"*.tg"};\r
-        fd.setFilterExtensions(filterExt);\r
-        String selected = fd.open();\r
-        if(selected == null) return null;\r
-\r
-        Activator.getDefault().getPreferenceStore().setValue(IMPORTMODULETPATH, (new File(selected)).getParent());\r
-\r
-        // Get the transferable graph from file\r
-        TransferableGraph1 tg = null;\r
-        try {\r
-            tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
-        } catch (RuntimeBindingConstructionException e) {\r
-            e.printStackTrace();\r
-        } catch (IOException e) {\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
-\r
-        // Import the module to model\r
-        DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(model);\r
-        try {\r
-            DefaultPasteHandler.defaultExecute(tg, model, ia);\r
-        } catch (MissingDependencyException e) {\r
-            e.printStackTrace();\r
-        } catch (Exception e) {\r
-            e.printStackTrace();\r
-        }\r
-\r
-        final Resource root = ia.getRoot();\r
-\r
-        // Check that the imported file actually was a module. Display error message otherwise.\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
+       public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH";\r
+\r
+       @Override\r
+       public Object execute(ExecutionEvent event) throws ExecutionException {\r
+               ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+\r
+               @SuppressWarnings("unchecked")\r
+               AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
+               if (node == null)\r
+                       return null;\r
+\r
+               final Resource model = node.data;\r
+\r
+               // Get imported transferable graph file using FileDialog\r
+               final Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+               FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
+               fd.setText("Import Module");\r
+\r
+               String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODULETPATH);\r
+               if(path.isEmpty() || !(new File(path).exists()))\r
+                       path = Platform.getLocation().toOSString();\r
+               fd.setFilterPath(path);\r
+               String[] filterExt = {"*.tg"};\r
+               fd.setFilterExtensions(filterExt);\r
+               String selected = fd.open();\r
+               if(selected == null) return null;\r
+\r
+               Activator.getDefault().getPreferenceStore().setValue(IMPORTMODULETPATH, (new File(selected)).getParent());\r
+\r
+               // Handler for importing modules\r
+               HashMap<String, FormatHandler<Object>> handlers = new HashMap<String, FormatHandler<Object>>();\r
+               handlers.put("sysdynModule:1", new FormatHandler<Object>() {\r
+                       @Override\r
+                       public Binding getBinding() {\r
+                               return TransferableGraph1.BINDING;\r
+                       }\r
+\r
+                       @Override\r
+                       public Object process(DataContainer container) throws DataFormatException {\r
+                               DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(model);\r
+                               try {\r
+                                       DefaultPasteHandler.defaultExecute((TransferableGraph1)container.content.getValue(), model, ia);\r
+                                       return ia.getRoot();\r
+\r
+                               } catch (Exception e) {\r
+                                       e.printStackTrace();\r
+                                       return null;\r
+                               }\r
+                       }\r
+               });\r
+\r
+               // Read module file\r
+               Object result = null;\r
+               try {\r
+                       File file = new File(selected);\r
+                       if(!file.isFile()) {\r
+                               displayErrorMessage(shell, "File " + selected + " not found");\r
+                               return null;\r
+                       } else {        \r
+                               result = DataContainers.readFile(file, handlers);\r
+                       }\r
+               } catch(DataFormatException e) {\r
+                       e.printStackTrace();\r
+               } catch(IOException e) {\r
+                       e.printStackTrace();\r
+               } catch(Exception e) {\r
+                       if(e instanceof RuntimeException)\r
+                               throw (RuntimeException)e;\r
+                       else\r
+                               throw new RuntimeException(e);\r
+               }\r
+\r
+\r
+               if(result == null || !(result instanceof Resource)) {\r
+                       displayErrorMessage(shell, "Module could not be imported.");\r
+               } else {\r
+                       final Resource ModuleRoot = (Resource) result;\r
+                       // Check that the imported file actually was a module. Display error message otherwise.\r
+                       SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+\r
+                               @Override\r
+                               public void perform(WriteGraph graph) throws DatabaseException {\r
+                                       if(!graph.isInheritedFrom(ModuleRoot, SysdynResource.getInstance(graph).Module)) {\r
+                                               Resource instanceOf = graph.getPossibleObject(ModuleRoot, Layer0.getInstance(graph).InstanceOf);\r
+                                               String type = "...";\r
+                                               if(instanceOf != null)\r
+                                                       type = NameUtils.getSafeName(graph, instanceOf);\r
+                                               else {\r
+                                                       Resource inheritedFrom = graph.getPossibleObject(ModuleRoot, Layer0.getInstance(graph).Inherits);\r
+                                                       if(inheritedFrom != null)\r
+                                                               type = NameUtils.getSafeName(graph, inheritedFrom);\r
+                                               }\r
+                                               final String ft = type; \r
+                                               graph.deny(ModuleRoot, Layer0.getInstance(graph).PartOf);\r
+\r
+                                               shell.getDisplay().asyncExec(new Runnable() {\r
+\r
+                                                       @Override\r
+                                                       public void run() {\r
+                                                               displayErrorMessage(shell, "The imported file is not of type: System Dynamics Module (" + ft +")");\r
+                                                       }\r
+                                               });\r
+                                       } \r
+                               }\r
+                       });\r
+\r
+               }\r
+               return null;\r
+       }\r
+\r
+\r
+       private void displayErrorMessage(Shell shell, String message) {\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
+       }\r
 \r
 }\r
index 2b85df431297a5b0d7d59dad7bb5be35e7ed4a66..7a5f165bd69f9eecca3a98b6ba16f228d388a7db 100644 (file)
@@ -18,6 +18,7 @@ import java.util.Set;
 \r
 import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
 import org.simantics.sysdyn.expressionParser.ParseException;\r
+import org.simantics.sysdyn.expressionParser.Token;\r
 import org.simantics.sysdyn.representation.utils.FormatUtils;\r
 import org.simantics.sysdyn.representation.utils.RepresentationUtils;\r
 \r
@@ -229,6 +230,12 @@ public enum Variability {
                 return Variability.PARAMETER;\r
             } else {\r
                 Set<String> references = parser.getReferences().keySet();\r
+                \r
+                if(parser.getForIndices()!=null) {\r
+                       for(Token t : parser.getForIndices().keySet()) {\r
+                               references.remove(t.image);\r
+                       }\r
+                }\r
 \r
                 // Go through each reference\r
                 for(String reference : references) {\r