]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramEditorStates.java
Configurable connection crossing styles
[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
45 /**
46  * @author Tuukka Lehtonen
47  */
48 public class DiagramEditorStates {
49
50     public static EditorState toEditorState(ICanvasContext ctx) {
51         return toEditorState(ctx, true, true, true);
52     }
53
54     public static EditorState toEditorState(ICanvasContext ctx, boolean saveTransform, boolean saveSelection, boolean saveToolMode) {
55         EditorState state = new EditorState();
56
57         if (saveTransform) {
58             state.viewTransform = ctx.getHintStack().getHint( Hints.KEY_CANVAS_TRANSFORM );
59             if (state.viewTransform != null && state.viewTransform.getDeterminant() == 0)
60                 state.viewTransform = null;
61         }
62
63         if (saveToolMode) {
64             IToolMode toolMode = ctx.getHintStack().getHint( Hints.KEY_TOOL );
65             if (toolMode != null)
66                 state.toolMode = toolMode.getId();
67         }
68
69         if (saveSelection) {
70             Set<IElement> selection = Collections.emptySet();
71             for (Selection s : ctx.getItemsByClass( Selection.class ))
72                 selection = s.getSelection(0);
73             state.selection = toResources( selection );
74         }
75
76         return state;
77     }
78
79     public static void saveEditorState(String virtualGraph, Resource diagram, ICanvasContext ctx, ListenerSupport support) {
80         saveEditorState( virtualGraph, diagram, toEditorState(ctx), support );
81     }
82
83     public static Set<Resource> toResources(Set<IElement> elements) {
84         Set<Resource> result = new HashSet<Resource>();
85         for (IElement e : elements) {
86             Object obj = ElementUtils.getObject(e);
87             if (obj instanceof Resource)
88                 result.add((Resource) obj);
89         }
90         return result;
91     }
92
93     public static Set<IElement> toElements(Set<Resource> selection, IDiagram diagram) {
94         Set<IElement> result = new HashSet<IElement>();
95         DataElementMap dem = diagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);
96         for (Resource selected : selection) {
97             IElement e = dem.getElement(diagram, selected);
98             if (e != null)
99                 result.add(e);
100         }
101         return result;
102     }
103
104     public static void saveEditorState(final String virtualGraph, final Resource diagram, final EditorState editorState, final ListenerSupport support) {
105         Session session = Simantics.getSession();
106
107         final VirtualGraph vg = virtualGraph == null ? null
108                 : session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraph);
109
110         session.asyncRequest(new WriteRequest() {
111             @Override
112             public void perform(WriteGraph graph) throws DatabaseException {
113                 // Prevent the state writing from failing in RemoverUtil.remove.
114                 if (Layer0Utils.isContainerPublished(graph, diagram))
115                     return;
116
117                 // Remove all previous editor states.
118                 DiagramResource DIA = DiagramResource.getInstance(graph);
119                 for (Resource state : graph.getObjects(diagram, DIA.HasEditorState))
120                     RemoverUtil.remove(graph, state);
121
122                 graph.syncRequest(new WriteRequest(vg) {
123                     @Override
124                     public void perform(WriteGraph graph) throws DatabaseException {
125                         newEditorState(graph, diagram, editorState);
126                     }
127                 });
128             }
129         }, e -> {
130             if (e != null)
131                 support.exception(e);
132         });
133     }
134
135     private static Resource newEditorState(WriteGraph graph, Resource stateOf, EditorState editorState) throws DatabaseException {
136         Layer0 L0 = Layer0.getInstance(graph);
137         G2DResource G2D = G2DResource.getInstance(graph);
138         DiagramResource DIA = DiagramResource.getInstance(graph);
139
140         Resource state = graph.newResource();
141         graph.claim(state, L0.InstanceOf, null, DIA.EditorState);
142         graph.claim(stateOf, DIA.HasEditorState, state);
143
144         if (editorState.viewTransform != null) {
145             double[] matrix = new double[6];
146             editorState.viewTransform.getMatrix(matrix);
147             graph.claimLiteral(state, DIA.EditorState_ViewTransform, G2D.Transform, matrix, Bindings.DOUBLE_ARRAY);
148         }
149
150         if (editorState.toolMode != null)
151             graph.claimLiteral(state, DIA.EditorState_ToolMode, L0.String, editorState.toolMode, Bindings.STRING);
152
153         for (Resource selected : editorState.selection)
154             graph.claim(state, DIA.EditorState_Selection, null, selected);
155
156         return state;
157     }
158
159     public static EditorState readEditorState(final Resource source) throws DatabaseException {
160         return Simantics.getSession().syncRequest(new ResourceRead<EditorState>(source) {
161             @Override
162             public EditorState perform(ReadGraph graph) throws DatabaseException {
163                 EditorState state = new EditorState();
164                 DiagramResource DIA = DiagramResource.getInstance(graph);
165                 for (Resource editorState : graph.getObjects(source, DIA.HasEditorState)) {
166                     state.viewTransform = DiagramGraphUtil.getAffineTransform(graph, editorState, DIA.EditorState_ViewTransform, false);
167                     state.toolMode = graph.getPossibleRelatedValue(editorState, DIA.EditorState_ToolMode, Bindings.STRING);
168                     state.selection = new HashSet<Resource>();
169                     for (Resource selected : graph.getObjects(editorState, DIA.EditorState_Selection)) {
170                         if (graph.isInstanceOf(selected, DIA.Element))
171                             state.selection.add(selected);
172                     }
173                     break;
174                 }
175                 return state;
176             }
177         });
178     }
179
180 }