]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionInfoRequest.java
Combination of Simantics-platform related changes and fixes for district
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / ConnectionInfoRequest.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.util.concurrent.atomic.AtomicInteger;
15
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.request.UnaryAsyncRead;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.diagram.connection.ConnectionSegmentEnd;
21 import org.simantics.diagram.connection.ConnectionSegmentType;
22 import org.simantics.diagram.content.EdgeResource;
23 import org.simantics.diagram.stubs.DiagramResource;
24
25 /**
26  * @author Antti Villberg
27  *
28  */
29 public class ConnectionInfoRequest extends UnaryAsyncRead<EdgeResource, ConnectionInfo> {
30
31     static class SingleConnectionInfo {
32         public Resource connection;
33         public ConnectionSegmentEnd end;
34     }
35
36     final DiagramResource dr;
37
38     private SingleConnectionInfo first;
39     private SingleConnectionInfo second;
40
41     public ConnectionInfoRequest(DiagramResource dr, EdgeResource resource) {
42         super(resource);
43         this.dr = dr;
44     }
45
46     public void fillPossibleType(AsyncReadGraph graph, Resource resource, final AsyncProcedure<ConnectionInfo> procedure, final ConnectionInfo result) {
47         if (resource == null) {
48             procedure.execute(graph, result);
49         } else {
50             graph.forSingleType(resource, dr.Connection, new AsyncProcedure<Resource>() {
51                 @Override
52                 public void exception(AsyncReadGraph graph, Throwable throwable) {
53                     procedure.exception(graph, throwable);
54                 }
55
56                 @Override
57                 public void execute(AsyncReadGraph graph, Resource type) {
58                     result.connectionType = type;
59                     procedure.execute(graph, result);
60                 }
61             });
62         }
63     }
64
65     ConnectionInfo createConnectionInfo(Resource connection) {
66         ConnectionInfo info = new ConnectionInfo();
67         info.connection = connection;
68         info.firstEnd = first.end;
69         info.secondEnd = second.end;
70         info.segmentType = ConnectionSegmentType.toSegmentType(first.end, second.end);
71         return info;
72     }
73
74     public void isBranchPointOf(AsyncReadGraph graph, Resource resource, final AsyncProcedure<ConnectionInfo> procedure, final SingleConnectionInfo result, final AtomicInteger ready) {
75
76         graph.forPossibleObject(resource, dr.IsBranchPointOf, new AsyncProcedure<Resource>() {
77             @Override
78             public void exception(AsyncReadGraph graph, Throwable throwable) {
79                 procedure.exception(graph, throwable);
80             }
81
82             @Override
83             public void execute(AsyncReadGraph graph, Resource connection) {
84                 result.end = ConnectionSegmentEnd.BRANCH;
85                 result.connection = connection;
86                 if (ready.decrementAndGet() == 0) {
87                     ConnectionInfo info = createConnectionInfo(connection);
88                     fillPossibleType(graph, connection, procedure, info);
89                 }
90             }
91         });
92
93     }
94
95     public void isConnectorOf(AsyncReadGraph graph, final Resource resource, final AsyncProcedure<ConnectionInfo> procedure, final SingleConnectionInfo result, final AtomicInteger ready) {
96
97         graph.forPossibleObject(resource, dr.IsConnectorOf, new AsyncProcedure<Resource>() {
98             @Override
99             public void exception(AsyncReadGraph graph, Throwable throwable) {
100                 procedure.exception(graph, throwable);
101             }
102
103             @Override
104             public void execute(AsyncReadGraph graph, Resource connection) {
105                 if (connection != null) {
106                     result.end = ConnectionSegmentEnd.CONNECTOR;
107                     result.connection = connection;
108                     if (ready.decrementAndGet() == 0) {
109                         ConnectionInfo info = createConnectionInfo(connection);
110                         fillPossibleType(graph, connection, procedure, info);
111                     }
112                 } else {
113                     isBranchPointOf(graph, resource, procedure, result, ready);
114                 }
115             }
116         });
117
118     }
119
120     @Override
121     public void perform(AsyncReadGraph graph, AsyncProcedure<ConnectionInfo> procedure) {
122         AtomicInteger ready = new AtomicInteger(2);
123         first = new SingleConnectionInfo();
124         second = new SingleConnectionInfo();
125         isConnectorOf(graph, parameter.first(), procedure, first, ready);
126         isConnectorOf(graph, parameter.second(), procedure, second, ready);
127     }
128
129 }