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.Resource;
16 import org.simantics.db.common.primitiverequest.Adapter;
17 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.diagram.synchronization.ErrorHandler;
20 import org.simantics.g2d.canvas.ICanvasContext;
21 import org.simantics.g2d.diagram.IDiagram;
22 import org.simantics.g2d.element.ElementClass;
23 import org.simantics.g2d.element.IElement;
24 import org.simantics.scl.runtime.tuple.Tuple3;
27 * @author Antti Villberg
29 public class ConnectionRequest2 extends BaseRequest2<Resource, Tuple3> {
31 final IDiagram diagram;
32 final ErrorHandler errorHandler;
34 public ConnectionRequest2(ICanvasContext canvas, IDiagram diagram, Resource resource, ErrorHandler errorHandler) {
35 super(canvas, resource);
36 this.diagram = diagram;
37 this.errorHandler = errorHandler;
41 public void perform(AsyncReadGraph graph, final AsyncProcedure<Tuple3> procedure) {
43 graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
46 public void exception(AsyncReadGraph graph, Throwable throwable) {
47 procedure.exception(graph, throwable);
51 public void execute(AsyncReadGraph graph, Boolean result) {
54 procedure.execute(graph, null);
58 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
61 public void exception(AsyncReadGraph graph, Throwable throwable) {
62 errorHandler.error("Unexpected ElementFactory adaption failure", throwable);
63 procedure.execute(graph, null);
67 public void execute(AsyncReadGraph graph, final ElementFactory factory) {
69 graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
72 public void exception(AsyncReadGraph graph, Throwable throwable) {
73 errorHandler.error("Unexpected ElementClass creation failure", throwable);
74 procedure.execute(graph, null);
78 public void execute(AsyncReadGraph graph, final ElementClass ec) {
80 graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener<IElement>() {
83 public void exception(AsyncReadGraph graph, Throwable throwable) {
84 errorHandler.error("Unexpected SpawnRequest failure", throwable);
85 procedure.execute(graph, null);
89 public void execute(AsyncReadGraph graph, IElement element) {
90 procedure.execute(graph, new Tuple3(element, ec, factory));