]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ElementFactoryUtil.java
Some enhancements to GraphLayer-related utilities for Diagram layers
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / ElementFactoryUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.diagram.adapter;
13
14 import java.awt.geom.AffineTransform;
15
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;
20 import org.simantics.db.procedure.AsyncProcedure;
21 import org.simantics.diagram.stubs.G2DResource;
22 import org.simantics.diagram.synchronization.ErrorHandler;
23 import org.simantics.diagram.synchronization.ISynchronizationContext;
24 import org.simantics.diagram.synchronization.SynchronizationHints;
25 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
26 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
27 import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
28 import org.simantics.g2d.diagram.DiagramHints;
29 import org.simantics.g2d.diagram.IDiagram;
30 import org.simantics.g2d.element.ElementUtils;
31 import org.simantics.g2d.element.IElement;
32 import org.simantics.g2d.layers.ILayersEditor;
33 import org.simantics.utils.datastructures.hints.IHintContext.Key;
34
35 public class ElementFactoryUtil {
36
37     public static void readTransform(AsyncReadGraph graph, final Resource resource, final IElement e) {
38         readTransform(graph, resource, e, new AsyncProcedureAdapter<IElement>() {
39             @Override
40             public void exception(AsyncReadGraph graph, Throwable t) {
41                 // Nowhere to log properly.
42                 t.printStackTrace();
43             }
44         });
45     }
46
47     public static void readTransform(AsyncReadGraph graph, final Resource resource, final IElement e, final AsyncProcedure<IElement> procedure) {
48         G2DResource G2D = graph.getService(G2DResource.class);
49         graph.forPossibleRelatedValue(resource, G2D.HasTransform, Bindings.DOUBLE_ARRAY, new AsyncProcedure<double[]>() {
50             @Override
51             public void exception(AsyncReadGraph graph, Throwable throwable) {
52                 procedure.exception(graph, throwable);
53             }
54
55             @Override
56             public void execute(AsyncReadGraph graph, double[] mat) {
57                 mat = DiagramGraphUtil.validateAffineTransform(resource, mat);
58                 AffineTransform tr = mat != null ? new AffineTransform(mat) : new AffineTransform();
59                 ElementUtils.setTransform(e, tr);
60                 procedure.execute(graph, e);
61             }
62         });
63     }
64
65     public static ISynchronizationContext getContext(IDiagram diagram) {
66         return diagram.getHint(SynchronizationHints.CONTEXT);
67     }
68
69     public static ISynchronizationContext getContextChecked(IDiagram diagram) {
70         ISynchronizationContext context = getContext(diagram);
71         if (context == null)
72             throw new IllegalStateException("diagram does not contain " + SynchronizationHints.CONTEXT + " hint");
73         return context;
74     }
75
76     public static ErrorHandler getErrorHandler(IDiagram diagram) {
77         ISynchronizationContext context = getContext(diagram);
78         if (context == null)
79             return null;
80         return context.get(SynchronizationHints.ERROR_HANDLER);
81     }
82
83     public static ErrorHandler getErrorHandlerChecked(IDiagram diagram) {
84         ISynchronizationContext context = getContextChecked(diagram);
85         ErrorHandler errorHandler = context.get(SynchronizationHints.ERROR_HANDLER);
86         if (errorHandler == null)
87             throw new IllegalStateException("synchronization context " + context + " does not contain " + SynchronizationHints.ERROR_HANDLER + " hint");
88         return errorHandler;
89     }
90
91     public static <T> T getContextHint(IDiagram diagram, Key key) {
92         ISynchronizationContext context = getContext(diagram);
93         if (context == null)
94             return null;
95         return context.get(key);
96     }
97
98     public static <T> T getContextHintChecked(IDiagram diagram, Key key) {
99         return getContextChecked(diagram).get(key);
100     }
101
102     public static void loadLayersForElement(AsyncReadGraph graph, IDiagram diagram, IElement e, Resource element,
103             AsyncProcedure<IElement> callback) {
104         final GraphLayerManager layerManager = getContextHint(diagram, GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
105         if (layerManager != null)
106             loadLayersForElement(graph, layerManager, diagram, e, element, callback);
107         else
108             callback.execute(graph, e);
109     }
110
111     public static void loadLayersForElement(AsyncReadGraph graph, GraphLayerManager layerManager, IDiagram diagram, IElement e, Resource element,
112             AsyncProcedure<IElement> callback) {
113         final ILayersEditor layers = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
114         if (layers != null)
115             layerManager.loadLayersForElement(graph, layers, e, element, callback);
116         else
117             callback.execute(graph, e);
118     }
119
120 }