]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramEditorStates.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / DiagramEditorStates.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramEditorStates.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramEditorStates.java
new file mode 100644 (file)
index 0000000..c366e15
--- /dev/null
@@ -0,0 +1,184 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.diagramEditor;\r
+\r
+import java.util.Collections;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import org.simantics.Simantics;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.VirtualGraph;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.procedure.adapter.ListenerSupport;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.db.layer0.util.RemoverUtil;\r
+import org.simantics.db.service.VirtualGraphSupport;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.diagram.stubs.G2DResource;\r
+import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
+import org.simantics.g2d.canvas.Hints;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.canvas.IToolMode;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.diagram.handler.DataElementMap;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.datastructures.Callback;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DiagramEditorStates {\r
+\r
+    public static EditorState toEditorState(ICanvasContext ctx) {\r
+        return toEditorState(ctx, true, true, true);\r
+    }\r
+\r
+    public static EditorState toEditorState(ICanvasContext ctx, boolean saveTransform, boolean saveSelection, boolean saveToolMode) {\r
+        EditorState state = new EditorState();\r
+\r
+        if (saveTransform) {\r
+            state.viewTransform = ctx.getHintStack().getHint( Hints.KEY_CANVAS_TRANSFORM );\r
+            if (state.viewTransform != null && state.viewTransform.getDeterminant() == 0)\r
+                state.viewTransform = null;\r
+        }\r
+\r
+        if (saveToolMode) {\r
+            IToolMode toolMode = ctx.getHintStack().getHint( Hints.KEY_TOOL );\r
+            if (toolMode != null)\r
+                state.toolMode = toolMode.getId();\r
+        }\r
+\r
+        if (saveSelection) {\r
+            Set<IElement> selection = Collections.emptySet();\r
+            for (Selection s : ctx.getItemsByClass( Selection.class ))\r
+                selection = s.getSelection(0);\r
+            state.selection = toResources( selection );\r
+        }\r
+\r
+        return state;\r
+    }\r
+\r
+    public static void saveEditorState(String virtualGraph, Resource diagram, ICanvasContext ctx, ListenerSupport support) {\r
+        saveEditorState( virtualGraph, diagram, toEditorState(ctx), support );\r
+    }\r
+\r
+    public static Set<Resource> toResources(Set<IElement> elements) {\r
+        Set<Resource> result = new HashSet<Resource>();\r
+        for (IElement e : elements) {\r
+            Object obj = ElementUtils.getObject(e);\r
+            if (obj instanceof Resource)\r
+                result.add((Resource) obj);\r
+        }\r
+        return result;\r
+    }\r
+\r
+    public static Set<IElement> toElements(Set<Resource> selection, IDiagram diagram) {\r
+        Set<IElement> result = new HashSet<IElement>();\r
+        DataElementMap dem = diagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);\r
+        for (Resource selected : selection) {\r
+            IElement e = dem.getElement(diagram, selected);\r
+            if (e != null)\r
+                result.add(e);\r
+        }\r
+        return result;\r
+    }\r
+\r
+    public static void saveEditorState(final String virtualGraph, final Resource diagram, final EditorState editorState, final ListenerSupport support) {\r
+        Session session = Simantics.getSession();\r
+\r
+        final VirtualGraph vg = virtualGraph == null ? null\r
+                : session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraph);\r
+\r
+        session.asyncRequest(new WriteRequest() {\r
+            @Override\r
+            public void perform(WriteGraph graph) throws DatabaseException {\r
+                // Prevent the state writing from failing in RemoverUtil.remove.\r
+                if (Layer0Utils.isContainerPublished(graph, diagram))\r
+                    return;\r
+\r
+                // Remove all previous editor states.\r
+                DiagramResource DIA = DiagramResource.getInstance(graph);\r
+                for (Resource state : graph.getObjects(diagram, DIA.HasEditorState))\r
+                    RemoverUtil.remove(graph, state);\r
+\r
+                graph.syncRequest(new WriteRequest(vg) {\r
+                    @Override\r
+                    public void perform(WriteGraph graph) throws DatabaseException {\r
+                        newEditorState(graph, diagram, editorState);\r
+                    }\r
+                });\r
+            }\r
+        }, new Callback<DatabaseException>() {\r
+            @Override\r
+            public void run(DatabaseException parameter) {\r
+                if (parameter != null)\r
+                    support.exception(parameter);\r
+            }\r
+        });\r
+    }\r
+\r
+    private static Resource newEditorState(WriteGraph graph, Resource stateOf, EditorState editorState) throws DatabaseException {\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        G2DResource G2D = G2DResource.getInstance(graph);\r
+        DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+        Resource state = graph.newResource();\r
+        graph.claim(state, L0.InstanceOf, null, DIA.EditorState);\r
+        graph.claim(stateOf, DIA.HasEditorState, state);\r
+\r
+        if (editorState.viewTransform != null) {\r
+            double[] matrix = new double[6];\r
+            editorState.viewTransform.getMatrix(matrix);\r
+            graph.claimLiteral(state, DIA.EditorState_ViewTransform, G2D.Transform, matrix, Bindings.DOUBLE_ARRAY);\r
+        }\r
+\r
+        if (editorState.toolMode != null)\r
+            graph.claimLiteral(state, DIA.EditorState_ToolMode, L0.String, editorState.toolMode, Bindings.STRING);\r
+\r
+        for (Resource selected : editorState.selection)\r
+            graph.claim(state, DIA.EditorState_Selection, null, selected);\r
+\r
+        return state;\r
+    }\r
+\r
+    public static EditorState readEditorState(final Resource source) throws DatabaseException {\r
+        return Simantics.getSession().syncRequest(new ResourceRead<EditorState>(source) {\r
+            @Override\r
+            public EditorState perform(ReadGraph graph) throws DatabaseException {\r
+                EditorState state = new EditorState();\r
+                DiagramResource DIA = DiagramResource.getInstance(graph);\r
+                for (Resource editorState : graph.getObjects(source, DIA.HasEditorState)) {\r
+                    state.viewTransform = DiagramGraphUtil.getAffineTransform(graph, editorState, DIA.EditorState_ViewTransform, false);\r
+                    state.toolMode = graph.getPossibleRelatedValue(editorState, DIA.EditorState_ToolMode, Bindings.STRING);\r
+                    state.selection = new HashSet<Resource>();\r
+                    for (Resource selected : graph.getObjects(editorState, DIA.EditorState_Selection)) {\r
+                        if (graph.isInstanceOf(selected, DIA.Element))\r
+                            state.selection.add(selected);\r
+                    }\r
+                    break;\r
+                }\r
+                return state;\r
+            }\r
+        });\r
+    }\r
+\r
+}\r