]> gerrit.simantics Code Review - simantics/platform.git/blob
5e97febdaf77a11a2ab28bcba7505c027cd71af4
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2020 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.participant;
13
14 import org.simantics.Simantics;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.request.UnaryRead;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.procedure.Listener;
20 import org.simantics.diagram.connection.rendering.ConnectionCrossings;
21 import org.simantics.diagram.stubs.DiagramResource;
22 import org.simantics.g2d.canvas.ICanvasContext;
23 import org.simantics.g2d.canvas.SGDesignation;
24 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;
25 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
26 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
27 import org.simantics.scenegraph.g2d.G2DParentNode;
28 import org.simantics.scenegraph.g2d.nodes.ConnectionCrossingsNode;
29 import org.simantics.utils.datastructures.Pair;
30 import org.simantics.utils.ui.ErrorLogger;
31
32 public class ConnectionCrossingsParticipant extends AbstractDiagramParticipant {
33     private static final String CONNECTION_CROSSINGS_NODE_KEY = "connection-crossings";
34
35     private ConnectionCrossingsNode ccNode;
36     private final ConnectionCrossings crossings = new ConnectionCrossings();
37     private ConnectionCrossingStyleListener listener;
38     private Resource diagram;
39     
40     public ConnectionCrossingsParticipant(Resource diagram) {
41         this.diagram = diagram;
42     }
43     public ConnectionCrossingsParticipant(double width, ConnectionCrossings.Type type) {
44         crossings.setWidth(width);
45         crossings.setType(type);
46     }
47
48     @SGInit(designation = SGDesignation.CONTROL)
49     public void initSG(G2DParentNode parent) {
50         ccNode = parent.addNode(CONNECTION_CROSSINGS_NODE_KEY, ConnectionCrossingsNode.class);
51         ccNode.setCrossings(crossings);
52         ccNode.setZIndex(Integer.MIN_VALUE / 4);
53     }
54
55     @SGCleanup
56     public void cleanupSG() {
57         if (ccNode != null) {
58             ccNode.remove();
59             ccNode = null;
60         }
61     }
62
63     @Override
64     public void addedToContext(ICanvasContext ctx) {
65         super.addedToContext(ctx);
66
67         if (diagram != null) {
68             listener = new ConnectionCrossingStyleListener(ctx);
69             Simantics.getSession().async(new UnaryRead<Resource, Pair<Double, ConnectionCrossings.Type>>(diagram){
70     
71                 @Override
72                 public Pair<Double, ConnectionCrossings.Type> perform(ReadGraph graph) throws DatabaseException {
73                     DiagramResource DIA = DiagramResource.getInstance(graph);
74                     Double gap = graph.getPossibleRelatedValue(diagram, DIA.ConnectionCrossingStyle_Width);
75                     Resource typeRes = graph.getPossibleObject(diagram, DIA.ConnectionCrossingStyle_HasType);
76                     ConnectionCrossings.Type type;
77                     if (DIA.ConnectionCrossingStyle_Type_Gap.equals(typeRes)) {
78                         type = ConnectionCrossings.Type.GAP;
79                     } else if (DIA.ConnectionCrossingStyle_Type_Arc.equals(typeRes)) {
80                         type = ConnectionCrossings.Type.ARC;
81                     } else if (DIA.ConnectionCrossingStyle_Type_Square.equals(typeRes)) {
82                         type = ConnectionCrossings.Type.SQUARE;
83                     } else { 
84                         type = ConnectionCrossings.Type.NONE;
85                     }
86                     return new Pair<>(gap, type);
87                 }
88                 
89             }, listener);
90         }
91     }
92
93     @Override
94     public void removedFromContext(ICanvasContext ctx) {
95         if (listener != null) {
96             listener.dispose();
97             listener = null;
98         }
99         super.removedFromContext(ctx);
100     }
101
102     class ConnectionCrossingStyleListener implements Listener<Pair<Double, ConnectionCrossings.Type>> {
103         ICanvasContext context;
104         public ConnectionCrossingStyleListener(ICanvasContext context) {
105             this.context = context;
106         }
107         @Override
108         public void execute(final Pair<Double, ConnectionCrossings.Type> result) {
109             context.getThreadAccess().asyncExec(new Runnable() {
110                 @Override
111                 public void run() {
112                     ICanvasContext ctx = context;
113                     if (ctx == null)
114                         return;
115                     if (ctx.isDisposed())
116                         return;
117                     crossings.setWidth(result.first != null ? result.first : 0.0);
118                     crossings.setType(result.second);
119                     ccNode.repaint();
120                 }
121             });
122         }
123         public void dispose() {
124             context = null;
125         }
126         @Override
127         public boolean isDisposed() {
128             return context == null || context.isDisposed();
129         }
130         @Override
131         public void exception(Throwable t) {
132             ErrorLogger.defaultLogError(t);
133         }
134     }
135     
136     public ConnectionCrossings getCrossings() {
137                 return crossings;
138         }
139
140 }