1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor;
14 import java.util.Collections;
15 import java.util.HashSet;
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;
47 * @author Tuukka Lehtonen
49 public class DiagramEditorStates {
51 public static EditorState toEditorState(ICanvasContext ctx) {
52 return toEditorState(ctx, true, true, true);
55 public static EditorState toEditorState(ICanvasContext ctx, boolean saveTransform, boolean saveSelection, boolean saveToolMode) {
56 EditorState state = new EditorState();
59 state.viewTransform = ctx.getHintStack().getHint( Hints.KEY_CANVAS_TRANSFORM );
60 if (state.viewTransform != null && state.viewTransform.getDeterminant() == 0)
61 state.viewTransform = null;
65 IToolMode toolMode = ctx.getHintStack().getHint( Hints.KEY_TOOL );
67 state.toolMode = toolMode.getId();
71 Set<IElement> selection = Collections.emptySet();
72 for (Selection s : ctx.getItemsByClass( Selection.class ))
73 selection = s.getSelection(0);
74 state.selection = toResources( selection );
80 public static void saveEditorState(String virtualGraph, Resource diagram, ICanvasContext ctx, ListenerSupport support) {
81 saveEditorState( virtualGraph, diagram, toEditorState(ctx), support );
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);
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);
105 public static void saveEditorState(final String virtualGraph, final Resource diagram, final EditorState editorState, final ListenerSupport support) {
106 Session session = Simantics.getSession();
108 final VirtualGraph vg = virtualGraph == null ? null
109 : session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraph);
111 session.asyncRequest(new WriteRequest() {
113 public void perform(WriteGraph graph) throws DatabaseException {
114 // Prevent the state writing from failing in RemoverUtil.remove.
115 if (Layer0Utils.isContainerPublished(graph, diagram))
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);
123 graph.syncRequest(new WriteRequest(vg) {
125 public void perform(WriteGraph graph) throws DatabaseException {
126 newEditorState(graph, diagram, editorState);
130 }, new Callback<DatabaseException>() {
132 public void run(DatabaseException parameter) {
133 if (parameter != null)
134 support.exception(parameter);
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);
144 Resource state = graph.newResource();
145 graph.claim(state, L0.InstanceOf, null, DIA.EditorState);
146 graph.claim(stateOf, DIA.HasEditorState, state);
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);
154 if (editorState.toolMode != null)
155 graph.claimLiteral(state, DIA.EditorState_ToolMode, L0.String, editorState.toolMode, Bindings.STRING);
157 for (Resource selected : editorState.selection)
158 graph.claim(state, DIA.EditorState_Selection, null, selected);
163 public static EditorState readEditorState(final Resource source) throws DatabaseException {
164 return Simantics.getSession().syncRequest(new ResourceRead<EditorState>(source) {
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);