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.stubs.DiagramResource;
20 import org.simantics.diagram.synchronization.ErrorHandler;
21 import org.simantics.g2d.canvas.ICanvasContext;
22 import org.simantics.g2d.diagram.IDiagram;
23 import org.simantics.g2d.element.ElementClass;
24 import org.simantics.g2d.element.ElementHints;
25 import org.simantics.g2d.element.IElement;
26 import org.simantics.g2d.element.impl.Element;
29 * @author Antti Villberg
31 public class EdgeRequest extends BaseRequest2<EdgeResource, IElement> {
33 final ErrorHandler errorHandler;
34 final ListenerSupport listenerSupport;
35 final IDiagram diagram;
36 final ConnectionSegmentAdapter adapter;
38 public EdgeRequest(ICanvasContext canvas, ErrorHandler errorHandler, ListenerSupport listenerSupport, IDiagram diagram, ConnectionSegmentAdapter adapter, EdgeResource resource) {
39 super(canvas, resource);
41 assert(adapter != null);
43 this.errorHandler = errorHandler;
44 this.listenerSupport = listenerSupport;
45 this.diagram = diagram;
46 this.adapter = adapter;
50 public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
51 // final ITask task = ThreadLogger.getInstance().begin("NodeRequest2 " + data.getResourceId());
52 final DiagramResource dr = graph.getService(DiagramResource.class);
54 graph.forHasStatement(data.first(), new AsyncProcedure<Boolean>() {
57 public void exception(AsyncReadGraph graph, Throwable throwable) {
58 procedure.exception(graph, throwable);
62 public void execute(AsyncReadGraph graph, Boolean result) {
64 procedure.execute(graph, null);
68 graph.asyncRequest(new ConnectionInfoRequest(dr, data), new TransientCacheAsyncListener<ConnectionInfo>() {
71 public void exception(AsyncReadGraph graph, Throwable throwable) {
72 errorHandler.error("ConnectionInfoRequest failure", throwable);
76 public void execute(AsyncReadGraph graph, final ConnectionInfo info) {
77 adapter.getClass(graph, data, info, listenerSupport, canvas, diagram, new AsyncProcedure<ElementClass>() {
79 public void exception(AsyncReadGraph graph, Throwable throwable) {
80 errorHandler.error("ConnectionSegmentAdapter(" + adapter + ").getClass failure", throwable);
84 public void execute(AsyncReadGraph graph, final ElementClass clazz) {
86 procedure.execute(graph, null);
90 // final ITask task = ThreadLogger.getInstance().begin("SpawnEdge");
91 graph.asyncRequest(new SpawnEdgeRequest(canvas, clazz, data), new AsyncProcedure<IElement>() {
93 public void exception(AsyncReadGraph graph, Throwable throwable) {
94 errorHandler.error("SpawnEdgeRequest failure", throwable);
98 public void execute(AsyncReadGraph graph, IElement element) {
100 // Tell the procedure to use this element
101 procedure.execute(graph, element);
103 // Create a separate element for
104 // loading the edge which
105 // will then be updated into
106 // element by the listener in
107 // the connection segment adapter.
108 IElement loadingElement = Element.spawnNew(clazz);
109 loadingElement.setHint(ElementHints.KEY_OBJECT, data);
111 adapter.load(graph, data, info, listenerSupport, canvas, diagram, loadingElement);