]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramEditorStates.java
baf9cc3a2564d6309a1e919960c4562e4c07fb91
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / DiagramEditorStates.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor;
13
14 import java.util.Collections;
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.simantics.Simantics;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.Session;
23 import org.simantics.db.VirtualGraph;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.common.procedure.adapter.ListenerSupport;
26 import org.simantics.db.common.request.ResourceRead;
27 import org.simantics.db.common.request.WriteRequest;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.layer0.util.Layer0Utils;
30 import org.simantics.db.layer0.util.RemoverUtil;
31 import org.simantics.db.service.VirtualGraphSupport;
32 import org.simantics.diagram.stubs.DiagramResource;
33 import org.simantics.diagram.stubs.G2DResource;
34 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
35 import org.simantics.g2d.canvas.Hints;
36 import org.simantics.g2d.canvas.ICanvasContext;
37 import org.simantics.g2d.canvas.IToolMode;
38 import org.simantics.g2d.diagram.IDiagram;
39 import org.simantics.g2d.diagram.handler.DataElementMap;
40 import org.simantics.g2d.diagram.participant.Selection;
41 import org.simantics.g2d.element.ElementUtils;
42 import org.simantics.g2d.element.IElement;
43 import org.simantics.layer0.Layer0;
44 import org.simantics.utils.datastructures.Callback;
45
46 /**
47  * @author Tuukka Lehtonen
48  */
49 public class DiagramEditorStates {
50
51     public static EditorState toEditorState(ICanvasContext ctx) {
52         return toEditorState(ctx, true, true, true);
53     }
54
55     public static EditorState toEditorState(ICanvasContext ctx, boolean saveTransform, boolean saveSelection, boolean saveToolMode) {
56         EditorState state = new EditorState();
57
58         if (saveTransform) {
59             state.viewTransform = ctx.getHintStack().getHint( Hints.KEY_CANVAS_TRANSFORM );
60             if (state.viewTransform != null && state.viewTransform.getDeterminant() == 0)
61                 state.viewTransform = null;
62         }
63
64         if (saveToolMode) {
65             IToolMode toolMode = ctx.getHintStack().getHint( Hints.KEY_TOOL );
66             if (toolMode != null)
67                 state.toolMode = toolMode.getId();
68         }
69
70         if (saveSelection) {
71             Set<IElement> selection = Collections.emptySet();
72             for (Selection s : ctx.getItemsByClass( Selection.class ))
73                 selection = s.getSelection(0);
74             state.selection = toResources( selection );
75         }
76
77         return state;
78     }
79
80     public static void saveEditorState(String virtualGraph, Resource diagram, ICanvasContext ctx, ListenerSupport support) {
81         saveEditorState( virtualGraph, diagram, toEditorState(ctx), support );
82     }
83
84     public static Set<Resource> toResources(Set<IElement> elements) {
85         Set<Resource> result = new HashSet<Resource>();
86         for (IElement e : elements) {
87             Object obj = ElementUtils.getObject(e);
88             if (obj instanceof Resource)
89                 result.add((Resource) obj);
90         }
91         return result;
92     }
93
94     public static Set<IElement> toElements(Set<Resource> selection, IDiagram diagram) {
95         Set<IElement> result = new HashSet<IElement>();
96         DataElementMap dem = diagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);
97         for (Resource selected : selection) {
98             IElement e = dem.getElement(diagram, selected);
99             if (e != null)
100                 result.add(e);
101         }
102         return result;
103     }
104
105     public static void saveEditorState(final String virtualGraph, final Resource diagram, final EditorState editorState, final ListenerSupport support) {
106         Session session = Simantics.getSession();
107
108         final VirtualGraph vg = virtualGraph == null ? null
109                 : session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraph);
110
111         session.asyncRequest(new WriteRequest() {
112             @Override
113             public void perform(WriteGraph graph) throws DatabaseException {
114                 // Prevent the state writing from failing in RemoverUtil.remove.
115                 if (Layer0Utils.isContainerPublished(graph, diagram))
116                     return;
117
118                 // Remove all previous editor states.
119                 DiagramResource DIA = DiagramResource.getInstance(graph);
120                 for (Resource state : graph.getObjects(diagram, DIA.HasEditorState))
121                     RemoverUtil.remove(graph, state);
122
123                 graph.syncRequest(new WriteRequest(vg) {
124                     @Override
125                     public void perform(WriteGraph graph) throws DatabaseException {
126                         newEditorState(graph, diagram, editorState);
127                     }
128                 });
129             }
130         }, new Callback<DatabaseException>() {
131             @Override
132             public void run(DatabaseException parameter) {
133                 if (parameter != null)
134                     support.exception(parameter);
135             }
136         });
137     }
138
139     private static Resource newEditorState(WriteGraph graph, Resource stateOf, EditorState editorState) throws DatabaseException {
140         Layer0 L0 = Layer0.getInstance(graph);
141         G2DResource G2D = G2DResource.getInstance(graph);
142         DiagramResource DIA = DiagramResource.getInstance(graph);
143
144         Resource state = graph.newResource();
145         graph.claim(state, L0.InstanceOf, null, DIA.EditorState);
146         graph.claim(stateOf, DIA.HasEditorState, state);
147
148         if (editorState.viewTransform != null) {
149             double[] matrix = new double[6];
150             editorState.viewTransform.getMatrix(matrix);
151             graph.claimLiteral(state, DIA.EditorState_ViewTransform, G2D.Transform, matrix, Bindings.DOUBLE_ARRAY);
152         }
153
154         if (editorState.toolMode != null)
155             graph.claimLiteral(state, DIA.EditorState_ToolMode, L0.String, editorState.toolMode, Bindings.STRING);
156
157         for (Resource selected : editorState.selection)
158             graph.claim(state, DIA.EditorState_Selection, null, selected);
159
160         return state;
161     }
162
163     public static EditorState readEditorState(final Resource source) throws DatabaseException {
164         return Simantics.getSession().syncRequest(new ResourceRead<EditorState>(source) {
165             @Override
166             public EditorState perform(ReadGraph graph) throws DatabaseException {
167                 EditorState state = new EditorState();
168                 DiagramResource DIA = DiagramResource.getInstance(graph);
169                 for (Resource editorState : graph.getObjects(source, DIA.HasEditorState)) {
170                     state.viewTransform = DiagramGraphUtil.getAffineTransform(graph, editorState, DIA.EditorState_ViewTransform, false);
171                     state.toolMode = graph.getPossibleRelatedValue(editorState, DIA.EditorState_ToolMode, Bindings.STRING);
172                     state.selection = new HashSet<Resource>();
173                     for (Resource selected : graph.getObjects(editorState, DIA.EditorState_Selection)) {
174                         if (graph.isInstanceOf(selected, DIA.Element))
175                             state.selection.add(selected);
176                     }
177                     break;
178                 }
179                 return state;
180             }
181         });
182     }
183
184 }