From 7d63fd8bc07799fe15569e55f3fc1be089110dfb Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 31 Aug 2010 08:02:45 +0000 Subject: [PATCH] Import and export of models without modules 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 | 4 +- org.simantics.sysdyn.ui/plugin.xml | 41 ++++ .../sysdyn/ui/editor/DiagramViewer.java | 217 ++++++++++-------- .../ui/handlers/ExportModelHandler.java | 100 ++++++++ .../ui/handlers/ImportModelHandler.java | 59 +++++ 5 files changed, 319 insertions(+), 102 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModelHandler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java diff --git a/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF b/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF index d3289154..8678ef2f 100644 --- a/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF @@ -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 diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index dbcacb59..33fdf8cb 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -273,6 +273,22 @@ + + + + + + + + @@ -363,6 +379,14 @@ id="org.simantics.sysdyn.ui.newExperiment" name="New Experiment"> + + + + @@ -441,6 +465,23 @@ + + + + + + + + + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java index 9245e4ec..5d108374 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java @@ -6,6 +6,7 @@ import java.util.Set; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.IWorkbenchPartSite; import org.simantics.browsing.ui.swt.AdaptableHintContext; import org.simantics.browsing.ui.swt.IPropertyPage; @@ -24,6 +25,7 @@ import org.simantics.g2d.canvas.impl.CanvasContext; import org.simantics.g2d.connection.IConnectionAdvisor; import org.simantics.g2d.diagram.DiagramHints; import org.simantics.g2d.diagram.participant.DeleteHandler; +import org.simantics.g2d.diagram.participant.ElementPainter; import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor; import org.simantics.g2d.element.ElementClassProviders; import org.simantics.g2d.element.ElementClasses; @@ -46,108 +48,121 @@ import org.simantics.utils.datastructures.hints.IHintContext; */ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.DiagramViewer { - protected String getPopupId() { - return "#SysdynDiagramPopup"; - } + protected String getPopupId() { + return "#SysdynDiagramPopup"; + } @Override - protected Set getPropertyPageContexts() { - return Collections.singleton("http://www.simantics.org/Sysdyn-1.0/Browser"); - } - - @Override - protected IPropertyPage createPropertyPage(IWorkbenchPartSite site, Set contexts) { - return new SysdynPropertyPage(site, contexts); - } - - @Override - protected IElementClassProvider createElementClassProvider(ReadGraph graph) { - DiagramResource dr = DiagramResource.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - - return ElementClassProviders.mappedProvider( - ElementClasses.CONNECTION, SysdynConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(sr.FlowConnection)), - ElementClasses.FLAG, FlagClassFactory.createFlagClass(dr.Flag), - ConnectionClasses.FLOW, SysdynConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(sr.FlowConnection)), - ConnectionClasses.DEPENDENCY, SysdynConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(sr.DependencyConnection)) - ); - - - } - - @Override - protected void onCreated() { - sourceDiagram.setHint(DiagramHints.KEY_ALLOW_ROUTE_POINTS, Boolean.FALSE); - } - - @Override - protected void addKeyBindingParticipants(CanvasContext ctx) { - ctx.add(new DeleteHandler(getEditorSite().getActionBars().getStatusLineManager())); - ctx.add(new CopyPasteHandler(getEditorSite().getActionBars().getStatusLineManager())); - ctx.add(new CreateVariablesShortcutParticipant(synchronizer)); - } - - @Override - protected void initializeSynchronizationContext(ReadGraph graph, IModifiableSynchronizationContext context) { - super.initializeSynchronizationContext(graph, context); - - // Make sure SysdynResource is available. - SysdynResource.getInstance(graph); - } - - @Override - protected void addStructureParticipants(ICanvasContext ctx) { - - ctx.add(new WorkbenchSelectionProvider(swt, getSite()) { - - @Override - protected ISelection constructAdaptableSelection(Iterable selection) { - ArrayList objects = new ArrayList(); - Resource runtime = sourceDiagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE); - for (Object o : selection) { - if (o instanceof IElement) { - IElement e = (IElement) o; - Object object = e.getHint(ElementHints.KEY_OBJECT); - if (object != null && runtime != null) { - IHintContext context = new AdaptableHintContext(SelectionHints.KEY_MAIN); - context.setHint(SelectionHints.KEY_MAIN, object); - if (runtime != null) - context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime); - objects.add(context); - } - } else { - System.out.println(" unrecognized selection: " + o.getClass() + ": " + o); - } - } - if(objects.isEmpty() && runtime != null) { - HintContext context = new HintContext(); - context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime); - objects.add(context); - } - return new StructuredSelection(objects); - } - - }); - // Add visual browsing capabilities for structural models - -// Remove double click handler, because it is not working properly -// ctx.add(new StructuralBrowsingHandler(getSite(), sessionContext, getResourceInput2())); - ctx.add(new LinkBrowsingHandler(getSite(), this, sessionContext)); - - } - - @Override - protected void addOtherParticipants(CanvasContext ctx) { - } - - @Override - protected PointerInteractor getPointerInteractor() { - return new org.simantics.sysdyn.ui.editor.participant.PointerInteractor(true, true, true, false, true, false, synchronizer.getElementClassProvider()); - } - - @Override - protected IConnectionAdvisor getConnectionAdvisor(IModelingRules modelingRules, Session session) { - return new SysdynConnectionAdvisor(modelingRules, sessionContext.getSession()); - } + public void createPartControl(Composite parent) { + super.createPartControl(parent); + + ElementPainter ep = canvasContext.getSingleItem(ElementPainter.class); + for(IElement e : sourceDiagram.getElements()) { + if(e.getElementClass().getId().contains("Connection")){ + ep.update(e); + } + } + scheduleZoomToFit(); + } + + @Override + protected Set getPropertyPageContexts() { + return Collections.singleton("http://www.simantics.org/Sysdyn-1.0/Browser"); + } + + @Override + protected IPropertyPage createPropertyPage(IWorkbenchPartSite site, Set contexts) { + return new SysdynPropertyPage(site, contexts); + } + + @Override + protected IElementClassProvider createElementClassProvider(ReadGraph graph) { + DiagramResource dr = DiagramResource.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + return ElementClassProviders.mappedProvider( + ElementClasses.CONNECTION, SysdynConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(sr.FlowConnection)), + ElementClasses.FLAG, FlagClassFactory.createFlagClass(dr.Flag), + ConnectionClasses.FLOW, SysdynConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(sr.FlowConnection)), + ConnectionClasses.DEPENDENCY, SysdynConnectionClass.CLASS.newClassWith(new StaticObjectAdapter(sr.DependencyConnection)) + ); + + + } + + @Override + protected void onCreated() { + sourceDiagram.setHint(DiagramHints.KEY_ALLOW_ROUTE_POINTS, Boolean.FALSE); + } + + @Override + protected void addKeyBindingParticipants(CanvasContext ctx) { + ctx.add(new DeleteHandler(getEditorSite().getActionBars().getStatusLineManager())); + ctx.add(new CopyPasteHandler(getEditorSite().getActionBars().getStatusLineManager())); + ctx.add(new CreateVariablesShortcutParticipant(synchronizer)); + } + + @Override + protected void initializeSynchronizationContext(ReadGraph graph, IModifiableSynchronizationContext context) { + super.initializeSynchronizationContext(graph, context); + + // Make sure SysdynResource is available. + SysdynResource.getInstance(graph); + } + + @Override + protected void addStructureParticipants(ICanvasContext ctx) { + + ctx.add(new WorkbenchSelectionProvider(swt, getSite()) { + + @Override + protected ISelection constructAdaptableSelection(Iterable selection) { + ArrayList objects = new ArrayList(); + Resource runtime = sourceDiagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE); + for (Object o : selection) { + if (o instanceof IElement) { + IElement e = (IElement) o; + Object object = e.getHint(ElementHints.KEY_OBJECT); + if (object != null && runtime != null) { + IHintContext context = new AdaptableHintContext(SelectionHints.KEY_MAIN); + context.setHint(SelectionHints.KEY_MAIN, object); + if (runtime != null) + context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime); + objects.add(context); + } + } else { + System.out.println(" unrecognized selection: " + o.getClass() + ": " + o); + } + } + if(objects.isEmpty() && runtime != null) { + HintContext context = new HintContext(); + context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime); + objects.add(context); + } + return new StructuredSelection(objects); + } + + }); + // Add visual browsing capabilities for structural models + + // Remove double click handler, because it is not working properly + // ctx.add(new StructuralBrowsingHandler(getSite(), sessionContext, getResourceInput2())); + ctx.add(new LinkBrowsingHandler(getSite(), this, sessionContext)); + + } + + @Override + protected void addOtherParticipants(CanvasContext ctx) { + } + + @Override + protected PointerInteractor getPointerInteractor() { + return new org.simantics.sysdyn.ui.editor.participant.PointerInteractor(true, true, true, false, true, false, synchronizer.getElementClassProvider()); + } + + @Override + protected IConnectionAdvisor getConnectionAdvisor(IModelingRules modelingRules, Session session) { + return new SysdynConnectionAdvisor(modelingRules, sessionContext.getSession()); + } } 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 index 00000000..3f65256c --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModelHandler.java @@ -0,0 +1,100 @@ +package org.simantics.sysdyn.ui.handlers; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +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.Shell; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.Files; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; +import org.simantics.databoard.binding.java.StringBindingDefault; +import org.simantics.databoard.serialization.SerializationException; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.primitiverequest.PossibleRelatedValue; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.TransferableGraphRequest; +import org.simantics.db.request.Read; +import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.layer0.Layer0; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.utils.ResourceAdaptionUtils; +import org.simantics.utils.datastructures.Pair; + +public class ExportModelHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + ISelection sel = HandlerUtil.getCurrentSelection(event); + final Resource model = ResourceAdaptionUtils.toSingleResource(sel); + if(model == null) return null; + + // FIXME: Model browser doesn't change its selection even if the selected object is removed, + // so you can try to export a removed model + boolean isRemoved = true; + try { + isRemoved = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public Boolean perform(ReadGraph graph) throws DatabaseException { + return !graph.hasStatement(model, Layer0.getInstance(graph).PartOf); + } + + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + if(isRemoved) return null; + + Shell shell = HandlerUtil.getActiveShellChecked(event); + FileDialog fd = new FileDialog(shell, SWT.SAVE); + fd.setText("Export.."); + fd.setFilterPath(Platform.getLocation().toOSString()); + String[] filterExt = {"*.tg"}; + fd.setFilterExtensions(filterExt); + final String selected = fd.open(); + if(selected == null) return null; + + + SimanticsUI.getSession().asyncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + String name = graph.syncRequest(new PossibleRelatedValue(model, l0.HasName, StringBindingDefault.INSTANCE )); + ArrayList> roots = new ArrayList>(); + roots.add(Pair.make(model, name)); + TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest(roots, model)); + + try { + Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg); + } catch (RuntimeBindingConstructionException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (SerializationException e) { + e.printStackTrace(); + } catch (BindingException e) { + e.printStackTrace(); + } + + + } + }); + + + 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 new file mode 100644 index 00000000..d72a940f --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java @@ -0,0 +1,59 @@ +package org.simantics.sysdyn.ui.handlers; + +import java.io.File; +import java.io.IOException; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +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.Shell; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.Files; +import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; +import org.simantics.databoard.serialization.SerializationException; +import org.simantics.db.Resource; +import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; +import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; +import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.ui.SimanticsUI; + +public class ImportModelHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + + Resource project = SimanticsUI.getProject().get(); + if(project == null) return null; + + Shell shell = HandlerUtil.getActiveShellChecked(event); + FileDialog fd = new FileDialog(shell, SWT.OPEN); + fd.setText("Import..."); + fd.setFilterPath(Platform.getLocation().toOSString()); + String[] filterExt = {"*.tg"}; + fd.setFilterExtensions(filterExt); + String selected = fd.open(); + if(selected == null) return null; + + TransferableGraph1 tg = null; + try { + tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class)); + } catch (RuntimeBindingConstructionException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (SerializationException e) { + e.printStackTrace(); + } + if(tg == null) return null; + + org.simantics.graph.db.IImportAdvisor ia = new DefaultPasteImportAdvisor(project); + DefaultPasteHandler.defaultExecute(tg, SimanticsUI.getProject().get(), ia); + + return null; + } + +} -- 2.47.1