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 org.simantics.db.AsyncReadGraph;
15 import org.simantics.db.common.procedure.adapter.ListenerSupport;
16 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
17 import org.simantics.db.procedure.AsyncProcedure;
18 import org.simantics.diagram.content.EdgeResource;
19 import org.simantics.diagram.internal.DebugPolicy;
20 import org.simantics.diagram.stubs.DiagramResource;
21 import org.simantics.diagram.synchronization.ErrorHandler;
22 import org.simantics.g2d.canvas.ICanvasContext;
23 import org.simantics.g2d.diagram.IDiagram;
24 import org.simantics.g2d.element.ElementClass;
25 import org.simantics.g2d.element.ElementHints;
26 import org.simantics.g2d.element.IElement;
27 import org.simantics.g2d.element.impl.Element;
30 * @author Antti Villberg
32 public class EdgeRequest extends BaseRequest2<EdgeResource, IElement> {
34 final GraphToDiagramSynchronizer synchronizer;
35 final ErrorHandler errorHandler;
36 final ListenerSupport listenerSupport;
37 final IDiagram diagram;
38 final ConnectionSegmentAdapter adapter;
40 public EdgeRequest(GraphToDiagramSynchronizer synchronizer, ICanvasContext canvas, ErrorHandler errorHandler, ListenerSupport listenerSupport, IDiagram diagram, ConnectionSegmentAdapter adapter, EdgeResource resource) {
41 super(canvas, resource);
43 assert(adapter != null);
44 assert(synchronizer != null);
46 this.synchronizer = synchronizer;
47 this.errorHandler = errorHandler;
48 this.listenerSupport = listenerSupport;
49 this.diagram = diagram;
50 this.adapter = adapter;
54 public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
55 // final ITask task = ThreadLogger.getInstance().begin("NodeRequest2 " + data.getResourceId());
56 final DiagramResource dr = graph.getService(DiagramResource.class);
58 graph.forHasStatement(data.first(), new AsyncProcedure<Boolean>() {
61 public void exception(AsyncReadGraph graph, Throwable throwable) {
62 procedure.exception(graph, throwable);
66 public void execute(AsyncReadGraph graph, Boolean result) {
68 procedure.execute(graph, null);
72 graph.asyncRequest(new ConnectionInfoRequest(dr, data), new TransientCacheAsyncListener<ConnectionInfo>() {
75 public void exception(AsyncReadGraph graph, Throwable throwable) {
76 errorHandler.error("ConnectionInfoRequest failure", throwable);
80 public void execute(AsyncReadGraph graph, final ConnectionInfo info) {
81 adapter.getClass(graph, data, info, listenerSupport, canvas, diagram, new AsyncProcedure<ElementClass>() {
83 public void exception(AsyncReadGraph graph, Throwable throwable) {
84 errorHandler.error("ConnectionSegmentAdapter(" + adapter + ").getClass failure", throwable);
88 public void execute(AsyncReadGraph graph, final ElementClass clazz) {
90 procedure.execute(graph, null);
94 // final ITask task = ThreadLogger.getInstance().begin("SpawnEdge");
95 graph.asyncRequest(new SpawnEdgeRequest(canvas, clazz, data), new AsyncProcedure<IElement>() {
97 public void exception(AsyncReadGraph graph, Throwable throwable) {
98 errorHandler.error("SpawnEdgeRequest failure", throwable);
102 public void execute(AsyncReadGraph graph, IElement element) {
104 if (DebugPolicy.DEBUG_EDGE_LOAD)
105 System.out.println(" SPAWNED EDGE SEGMENT: " + element + " " + data);
107 // Register this for updates to work correctly
108 synchronizer.mapElementIfNew(data, element);
111 // Tell the procedure to use this element
112 procedure.execute(graph, element);
114 // Create a separate element for
115 // loading the edge which
116 // will then be updated into
117 // element by the listener in
118 // the connection segment adapter.
119 IElement loadingElement = Element.spawnNew(clazz);
120 loadingElement.setHint(ElementHints.KEY_OBJECT, data);
122 adapter.load(graph, data, info, listenerSupport, canvas, diagram, loadingElement);