1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.diagram.adapter;
14 import java.util.concurrent.atomic.AtomicInteger;
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;
26 * @author Antti Villberg
29 public class ConnectionInfoRequest extends UnaryAsyncRead<EdgeResource, ConnectionInfo> {
31 static class SingleConnectionInfo {
32 public Resource connection;
33 public ConnectionSegmentEnd end;
36 final DiagramResource dr;
38 private SingleConnectionInfo first;
39 private SingleConnectionInfo second;
41 public ConnectionInfoRequest(DiagramResource dr, EdgeResource resource) {
46 public void fillPossibleType(AsyncReadGraph graph, Resource resource, final AsyncProcedure<ConnectionInfo> procedure, final ConnectionInfo result) {
47 if (resource == null) {
48 procedure.execute(graph, result);
50 graph.forSingleType(resource, dr.Connection, new AsyncProcedure<Resource>() {
52 public void exception(AsyncReadGraph graph, Throwable throwable) {
53 procedure.exception(graph, throwable);
57 public void execute(AsyncReadGraph graph, Resource type) {
58 result.connectionType = type;
59 procedure.execute(graph, result);
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);
74 public void isBranchPointOf(AsyncReadGraph graph, Resource resource, final AsyncProcedure<ConnectionInfo> procedure, final SingleConnectionInfo result, final AtomicInteger ready) {
76 graph.forPossibleObject(resource, dr.IsBranchPointOf, new AsyncProcedure<Resource>() {
78 public void exception(AsyncReadGraph graph, Throwable throwable) {
79 procedure.exception(graph, throwable);
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);
95 public void isConnectorOf(AsyncReadGraph graph, final Resource resource, final AsyncProcedure<ConnectionInfo> procedure, final SingleConnectionInfo result, final AtomicInteger ready) {
97 graph.forPossibleObject(resource, dr.IsConnectorOf, new AsyncProcedure<Resource>() {
99 public void exception(AsyncReadGraph graph, Throwable throwable) {
100 procedure.exception(graph, throwable);
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);
113 isBranchPointOf(graph, resource, procedure, result, ready);
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);