]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Import and export of models without modules
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 31 Aug 2010 08:02:45 +0000 (08:02 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 31 Aug 2010 08:02:45 +0000 (08:02 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17709 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/META-INF/MANIFEST.MF
org.simantics.sysdyn.ui/plugin.xml
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModelHandler.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java [new file with mode: 0644]

index d32891547b999c88e0a65a31f44fd59db5ae9dac..8678ef2f4a0184ea897bd5ab7dad68413cde3cf4 100644 (file)
@@ -36,7 +36,9 @@ Require-Bundle: org.simantics.layer0.utils;bundle-version="0.6.2",
  org.simantics.message;bundle-version="0.9.0",
  org.simantics.structural2;bundle-version="1.0.0",
  org.simantics.layer0;bundle-version="1.0.0",
- org.simantics.diagram.ontology;bundle-version="1.0.0"
+ org.simantics.diagram.ontology;bundle-version="1.0.0",
+ org.simantics.graph;bundle-version="1.0.2",
+ org.simantics.graph.db;bundle-version="1.0.0"
 Bundle-Activator: org.simantics.sysdyn.ui.Activator
 Bundle-ActivationPolicy: lazy
 Export-Package: org.simantics.sysdyn.ui.browser.nodes
index dbcacb59576b493870de7a33fe20e8d888fa5f35..33fdf8cb01abe576bd620ef8231861232e3e96d3 100644 (file)
             <visibleWhen checkEnabled="true">\r
             </visibleWhen>\r
          </command>\r
+         <command\r
+               commandId="org.simantics.sysdyn.ui.exportModel"\r
+               label="Export..."\r
+               style="push">\r
+            <visibleWhen\r
+                  checkEnabled="true">\r
+            </visibleWhen>\r
+         </command>\r
+         <command\r
+               commandId="org.simantics.sysdyn.ui.importModel"\r
+               label="Import..."\r
+               style="push">\r
+            <visibleWhen\r
+                  checkEnabled="true">\r
+            </visibleWhen>\r
+         </command>\r
       </menuContribution>\r
       <menuContribution\r
             locationURI="popup:#SysdynDiagramPopup?after=wbStart">\r
             id="org.simantics.sysdyn.ui.newExperiment"\r
             name="New Experiment">\r
       </command>\r
+      <command\r
+            id="org.simantics.sysdyn.ui.exportModel"\r
+            name="Export...">\r
+      </command>\r
+      <command\r
+            id="org.simantics.sysdyn.ui.importModel"\r
+            name="Import...">\r
+      </command>\r
    </extension>\r
    <extension\r
          point="org.eclipse.ui.handlers">\r
             </with>\r
          </activeWhen>\r
       </handler>\r
+      <handler\r
+            class="org.simantics.sysdyn.ui.handlers.ImportModelHandler"\r
+            commandId="org.simantics.sysdyn.ui.importModel">\r
+      </handler>\r
+      <handler\r
+            class="org.simantics.sysdyn.ui.handlers.ExportModelHandler"\r
+            commandId="org.simantics.sysdyn.ui.exportModel">\r
+         <activeWhen>\r
+            <with\r
+                  variable="selection">\r
+               <test\r
+                     args="org.simantics.sysdyn.ui.browser.nodes.ModelNode"\r
+                     property="org.simantics.sysdyn.ui.nodeClass">\r
+               </test>\r
+            </with>\r
+         </activeWhen>\r
+      </handler>\r
    </extension>\r
    <extension\r
          point="org.eclipse.ui.cheatsheets.cheatSheetContent">\r
index 9245e4ec9f3bb493b9559a01a66c5411f433a880..5d10837450429003df76327c3274d2ffa3ef85e5 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Set;
 \r
 import org.eclipse.jface.viewers.ISelection;\r
 import org.eclipse.jface.viewers.StructuredSelection;\r
+import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.ui.IWorkbenchPartSite;\r
 import org.simantics.browsing.ui.swt.AdaptableHintContext;\r
 import org.simantics.browsing.ui.swt.IPropertyPage;\r
@@ -24,6 +25,7 @@ import org.simantics.g2d.canvas.impl.CanvasContext;
 import org.simantics.g2d.connection.IConnectionAdvisor;\r
 import org.simantics.g2d.diagram.DiagramHints;\r
 import org.simantics.g2d.diagram.participant.DeleteHandler;\r
+import org.simantics.g2d.diagram.participant.ElementPainter;\r
 import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor;\r
 import org.simantics.g2d.element.ElementClassProviders;\r
 import org.simantics.g2d.element.ElementClasses;\r
@@ -46,108 +48,121 @@ import org.simantics.utils.datastructures.hints.IHintContext;
  */\r
 public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.DiagramViewer {\r
 \r
-    protected String getPopupId() {\r
-        return "#SysdynDiagramPopup";\r
-    }\r
+       protected String getPopupId() {\r
+               return "#SysdynDiagramPopup";\r
+       }\r
 \r
        @Override\r
-    protected Set<String> getPropertyPageContexts() {\r
-        return Collections.singleton("http://www.simantics.org/Sysdyn-1.0/Browser");\r
-    }\r
-\r
-    @Override\r
-    protected IPropertyPage createPropertyPage(IWorkbenchPartSite site, Set<String> contexts) {\r
-        return new SysdynPropertyPage(site, contexts);\r
-    }\r
-\r
-    @Override\r
-    protected IElementClassProvider createElementClassProvider(ReadGraph graph) {\r
-        DiagramResource dr = DiagramResource.getInstance(graph);\r
-        SysdynResource sr = SysdynResource.getInstance(graph);\r
-\r
-        return ElementClassProviders.mappedProvider(\r
-                ElementClasses.CONNECTION, SysdynConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(sr.FlowConnection)),\r
-                ElementClasses.FLAG, FlagClassFactory.createFlagClass(dr.Flag),\r
-                ConnectionClasses.FLOW, SysdynConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(sr.FlowConnection)),\r
-                ConnectionClasses.DEPENDENCY, SysdynConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(sr.DependencyConnection))\r
-        );\r
-        \r
-        \r
-    }\r
-\r
-    @Override\r
-    protected void onCreated() {\r
-        sourceDiagram.setHint(DiagramHints.KEY_ALLOW_ROUTE_POINTS, Boolean.FALSE);\r
-    }\r
-\r
-    @Override\r
-    protected void addKeyBindingParticipants(CanvasContext ctx) {\r
-        ctx.add(new DeleteHandler(getEditorSite().getActionBars().getStatusLineManager()));\r
-        ctx.add(new CopyPasteHandler(getEditorSite().getActionBars().getStatusLineManager()));\r
-        ctx.add(new CreateVariablesShortcutParticipant(synchronizer));\r
-    }\r
-\r
-    @Override\r
-    protected void initializeSynchronizationContext(ReadGraph graph, IModifiableSynchronizationContext context) {\r
-        super.initializeSynchronizationContext(graph, context);\r
-\r
-        // Make sure SysdynResource is available.\r
-        SysdynResource.getInstance(graph);\r
-    }\r
-\r
-    @Override\r
-    protected void addStructureParticipants(ICanvasContext ctx) {\r
-\r
-        ctx.add(new WorkbenchSelectionProvider(swt, getSite()) {\r
-\r
-            @Override\r
-            protected ISelection constructAdaptableSelection(Iterable<?> selection) {\r
-                ArrayList<Object> objects = new ArrayList<Object>();\r
-                Resource runtime = sourceDiagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);\r
-                for (Object o : selection) {\r
-                    if (o instanceof IElement) {\r
-                        IElement e = (IElement) o;\r
-                        Object object = e.getHint(ElementHints.KEY_OBJECT);\r
-                        if (object != null && runtime != null) {\r
-                            IHintContext context = new AdaptableHintContext(SelectionHints.KEY_MAIN);\r
-                            context.setHint(SelectionHints.KEY_MAIN, object);\r
-                            if (runtime != null)\r
-                                context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime);\r
-                            objects.add(context);\r
-                        }\r
-                    } else {\r
-                        System.out.println("  unrecognized selection: " + o.getClass() + ": " + o);\r
-                    }\r
-                }\r
-                if(objects.isEmpty() && runtime != null) {\r
-                    HintContext context = new HintContext();\r
-                    context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime);\r
-                    objects.add(context);\r
-                }\r
-                return new StructuredSelection(objects);\r
-            }\r
-\r
-        });\r
-        // Add visual browsing capabilities for structural models\r
-        \r
-//        Remove double click handler, because it is not working properly\r
-//        ctx.add(new StructuralBrowsingHandler(getSite(), sessionContext, getResourceInput2()));\r
-        ctx.add(new LinkBrowsingHandler(getSite(), this, sessionContext));\r
-        \r
-    }\r
-\r
-    @Override\r
-    protected void addOtherParticipants(CanvasContext ctx) {\r
-    }\r
-    \r
-    @Override \r
-    protected PointerInteractor getPointerInteractor() {\r
-        return new org.simantics.sysdyn.ui.editor.participant.PointerInteractor(true, true, true, false, true, false, synchronizer.getElementClassProvider());\r
-    }\r
-    \r
-    @Override\r
-    protected IConnectionAdvisor getConnectionAdvisor(IModelingRules modelingRules, Session session) {\r
-        return new SysdynConnectionAdvisor(modelingRules, sessionContext.getSession());\r
-    }\r
+       public void createPartControl(Composite parent) {\r
+               super.createPartControl(parent);\r
+               \r
+               ElementPainter ep = canvasContext.getSingleItem(ElementPainter.class);\r
+               for(IElement e : sourceDiagram.getElements()) {\r
+                       if(e.getElementClass().getId().contains("Connection")){\r
+                               ep.update(e);\r
+                       }\r
+               }\r
+               scheduleZoomToFit();\r
+       } \r
+\r
+       @Override\r
+       protected Set<String> getPropertyPageContexts() {\r
+               return Collections.singleton("http://www.simantics.org/Sysdyn-1.0/Browser");\r
+       }\r
+\r
+       @Override\r
+       protected IPropertyPage createPropertyPage(IWorkbenchPartSite site, Set<String> contexts) {\r
+               return new SysdynPropertyPage(site, contexts);\r
+       }\r
+\r
+       @Override\r
+       protected IElementClassProvider createElementClassProvider(ReadGraph graph) {\r
+               DiagramResource dr = DiagramResource.getInstance(graph);\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+\r
+               return ElementClassProviders.mappedProvider(\r
+                               ElementClasses.CONNECTION, SysdynConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(sr.FlowConnection)),\r
+                               ElementClasses.FLAG, FlagClassFactory.createFlagClass(dr.Flag),\r
+                               ConnectionClasses.FLOW, SysdynConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(sr.FlowConnection)),\r
+                               ConnectionClasses.DEPENDENCY, SysdynConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(sr.DependencyConnection))\r
+               );\r
+\r
+\r
+       }\r
+\r
+       @Override\r
+       protected void onCreated() {\r
+               sourceDiagram.setHint(DiagramHints.KEY_ALLOW_ROUTE_POINTS, Boolean.FALSE);\r
+       }\r
+\r
+       @Override\r
+       protected void addKeyBindingParticipants(CanvasContext ctx) {\r
+               ctx.add(new DeleteHandler(getEditorSite().getActionBars().getStatusLineManager()));\r
+               ctx.add(new CopyPasteHandler(getEditorSite().getActionBars().getStatusLineManager()));\r
+               ctx.add(new CreateVariablesShortcutParticipant(synchronizer));\r
+       }\r
+\r
+       @Override\r
+       protected void initializeSynchronizationContext(ReadGraph graph, IModifiableSynchronizationContext context) {\r
+               super.initializeSynchronizationContext(graph, context);\r
+\r
+               // Make sure SysdynResource is available.\r
+               SysdynResource.getInstance(graph);\r
+       }\r
+\r
+       @Override\r
+       protected void addStructureParticipants(ICanvasContext ctx) {\r
+\r
+               ctx.add(new WorkbenchSelectionProvider(swt, getSite()) {\r
+\r
+                       @Override\r
+                       protected ISelection constructAdaptableSelection(Iterable<?> selection) {\r
+                               ArrayList<Object> objects = new ArrayList<Object>();\r
+                               Resource runtime = sourceDiagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);\r
+                               for (Object o : selection) {\r
+                                       if (o instanceof IElement) {\r
+                                               IElement e = (IElement) o;\r
+                                               Object object = e.getHint(ElementHints.KEY_OBJECT);\r
+                                               if (object != null && runtime != null) {\r
+                                                       IHintContext context = new AdaptableHintContext(SelectionHints.KEY_MAIN);\r
+                                                       context.setHint(SelectionHints.KEY_MAIN, object);\r
+                                                       if (runtime != null)\r
+                                                               context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime);\r
+                                                       objects.add(context);\r
+                                               }\r
+                                       } else {\r
+                                               System.out.println("  unrecognized selection: " + o.getClass() + ": " + o);\r
+                                       }\r
+                               }\r
+                               if(objects.isEmpty() && runtime != null) {\r
+                                       HintContext context = new HintContext();\r
+                                       context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime);\r
+                                       objects.add(context);\r
+                               }\r
+                               return new StructuredSelection(objects);\r
+                       }\r
+\r
+               });\r
+               // Add visual browsing capabilities for structural models\r
+\r
+               //        Remove double click handler, because it is not working properly\r
+               //        ctx.add(new StructuralBrowsingHandler(getSite(), sessionContext, getResourceInput2()));\r
+               ctx.add(new LinkBrowsingHandler(getSite(), this, sessionContext));\r
+\r
+       }\r
+\r
+       @Override\r
+       protected void addOtherParticipants(CanvasContext ctx) {\r
+       }\r
+\r
+       @Override \r
+       protected PointerInteractor getPointerInteractor() {\r
+               return new org.simantics.sysdyn.ui.editor.participant.PointerInteractor(true, true, true, false, true, false, synchronizer.getElementClassProvider());\r
+       }\r
+\r
+       @Override\r
+       protected IConnectionAdvisor getConnectionAdvisor(IModelingRules modelingRules, Session session) {\r
+               return new SysdynConnectionAdvisor(modelingRules, sessionContext.getSession());\r
+       }\r
 \r
 }\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModelHandler.java
new file mode 100644 (file)
index 0000000..3f65256
--- /dev/null
@@ -0,0 +1,100 @@
+package org.simantics.sysdyn.ui.handlers;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.runtime.Platform;\r
+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.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.BindingException;\r
+import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.databoard.binding.java.StringBindingDefault;\r
+import org.simantics.databoard.serialization.SerializationException;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.TransferableGraphRequest;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class ExportModelHandler extends AbstractHandler {\r
+\r
+       @Override\r
+       public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+        final Resource model = ResourceAdaptionUtils.toSingleResource(sel);\r
+        if(model == null) return null;\r
+        \r
+        // FIXME: Model browser doesn't change its selection even if the selected object is removed,\r
+        // so you can try to export a removed model \r
+               boolean isRemoved = true;\r
+               try {\r
+                       isRemoved = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
+\r
+                               @Override\r
+                               public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+                                       return !graph.hasStatement(model, Layer0.getInstance(graph).PartOf);\r
+                               }\r
+                               \r
+                       });\r
+               } catch (DatabaseException e1) {\r
+                       e1.printStackTrace();\r
+               }\r
+               if(isRemoved) return null;\r
+               \r
+               Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+               FileDialog fd = new FileDialog(shell, SWT.SAVE);\r
+               fd.setText("Export..");\r
+               fd.setFilterPath(Platform.getLocation().toOSString());\r
+               String[] filterExt = {"*.tg"};\r
+               fd.setFilterExtensions(filterExt);\r
+               final String selected = fd.open();\r
+               if(selected == null) return null;\r
+               \r
+               \r
+               SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
+                       \r
+                       @Override\r
+                       public void run(ReadGraph graph) throws DatabaseException {\r
+                               Layer0 l0 = Layer0.getInstance(graph);\r
+                               String name = graph.syncRequest(new PossibleRelatedValue<String>(model, l0.HasName, StringBindingDefault.INSTANCE ));\r
+                               ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();\r
+                               roots.add(Pair.make(model, name));\r
+                               TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest(roots, model));\r
+\r
+                               try {\r
+                                       Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
+                               } catch (RuntimeBindingConstructionException e) {\r
+                                       e.printStackTrace();\r
+                               } catch (IOException e) {\r
+                                       e.printStackTrace();\r
+                               } catch (SerializationException e) {\r
+                                       e.printStackTrace();\r
+                               } catch (BindingException e) {\r
+                                       e.printStackTrace();\r
+                               }\r
+\r
+                               \r
+                       }\r
+               });\r
+       \r
+\r
+               return null;\r
+       }\r
+\r
+}\r
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
new file mode 100644 (file)
index 0000000..d72a940
--- /dev/null
@@ -0,0 +1,59 @@
+package org.simantics.sysdyn.ui.handlers;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+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.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.serialization.SerializationException;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class ImportModelHandler extends AbstractHandler {\r
+\r
+       @Override\r
+       public Object execute(ExecutionEvent event) throws ExecutionException {\r
+               \r
+               Resource project = SimanticsUI.getProject().get();\r
+               if(project == null) return null;\r
+\r
+               Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+               FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
+               fd.setText("Import...");\r
+               fd.setFilterPath(Platform.getLocation().toOSString());\r
+               String[] filterExt = {"*.tg"};\r
+               fd.setFilterExtensions(filterExt);\r
+               String selected = fd.open();\r
+               if(selected == null) return null;\r
+\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
+                       e.printStackTrace();\r
+               } catch (SerializationException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               if(tg == null) return null;\r
+               \r
+               org.simantics.graph.db.IImportAdvisor ia = new DefaultPasteImportAdvisor(project);\r
+               DefaultPasteHandler.defaultExecute(tg, SimanticsUI.getProject().get(), ia);\r
+\r
+               return null;\r
+       }\r
+\r
+}\r