]> gerrit.simantics Code Review - simantics/district.git/blob
1003a9b22b0a8cd55646c79f9dd4ad38520ed8bb
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;
2
3 import java.awt.geom.Point2D;
4
5 import org.simantics.db.AsyncReadGraph;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.procedure.AsyncProcedure;
10 import org.simantics.diagram.adapter.SyncElementFactory;
11 import org.simantics.diagram.stubs.DiagramResource;
12 import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
13 import org.simantics.diagram.synchronization.SynchronizationHints;
14 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
15 import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
16 import org.simantics.district.network.ontology.DistrictNetworkResource;
17 import org.simantics.district.network.ui.DistrictNetworkEdge;
18 import org.simantics.g2d.canvas.ICanvasContext;
19 import org.simantics.g2d.diagram.DiagramHints;
20 import org.simantics.g2d.diagram.IDiagram;
21 import org.simantics.g2d.element.ElementClass;
22 import org.simantics.g2d.element.ElementUtils;
23 import org.simantics.g2d.element.IElement;
24 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
25 import org.simantics.g2d.layers.ILayersEditor;
26 import org.simantics.maps.MapScalingTransform;
27
28 public class DistrictNetworkEdgeElementFactory extends SyncElementFactory {
29
30     public static final ElementClass CLASS = DistrictNetworkEdgeElement.CLASS;
31     
32     private DistrictNetworkResource DN;
33     private DiagramResource DIA;
34     
35     public DistrictNetworkEdgeElementFactory(ReadGraph graph) {
36         this.DN = DistrictNetworkResource.getInstance(graph);
37         this.DIA = DiagramResource.getInstance(graph);
38     }
39     
40     @Override
41     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure<ElementClass> procedure) {
42         procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
43     }
44     
45     @Override
46     protected Resource getElementClassBaseType(AsyncReadGraph graph) {
47         return DN.Edge;
48     }
49     
50     @Override
51     public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource edgeResource, IElement element) throws DatabaseException {
52         if (!graph.hasStatement(edgeResource))
53             return; // already deleted
54
55         Resource startVertex = graph.getSingleObject(edgeResource, DN.HasStartVertex);
56         Resource endVertex = graph.getSingleObject(edgeResource, DN.HasEndVertex);
57
58         // TODO: Find maybe a better way to apply the scaling 
59         double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation);
60         double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation);
61         DistrictNetworkEdge edge = new DistrictNetworkEdge(new Point2D.Double(startCoords[0], startCoords[1]), new Point2D.Double(endCoords[0], endCoords[1]));
62
63         Resource mapping = graph.getSingleObject(edgeResource, DistrictNetworkResource.getInstance(graph).HasMapping);
64         element.setHint(DistrictNetworkAdditionalColor.KEY_DN_MAPPING_RESOURCE, mapping);
65         
66         element.setHint(DistrictNetworkEdgeElement.KEY_DN_EDGE, edge);
67         
68         // set scaling transform
69         ElementUtils.setTransform(element, MapScalingTransform.INSTANCE);
70         
71         IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
72         GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
73         
74         ILayersEditor layersEditor = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
75         layerManager.loadLayersForElement(graph, layersEditor, element, edgeResource);
76         
77     }
78 }