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.db.procedure.Listener;
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.IElement;
27 * @author Antti Villberg
29 public class ConnectionRequest extends BaseRequest2<Resource, IElement> {
31 final IDiagram diagram;
32 final Listener<IElement> loadListener;
33 final ErrorHandler errorHandler;
35 public ConnectionRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, ErrorHandler errorHandler, Listener<IElement> loadListener) {
36 super(canvas, resource);
37 this.diagram = diagram;
38 this.errorHandler = errorHandler;
39 this.loadListener = loadListener;
43 public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
45 graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
48 public void exception(AsyncReadGraph graph, Throwable throwable) {
49 procedure.exception(graph, throwable);
53 public void execute(AsyncReadGraph graph, Boolean result) {
56 procedure.execute(graph, null);
60 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
63 public void exception(AsyncReadGraph graph, Throwable throwable) {
64 errorHandler.error("Unexpected ElementFactory adaption failure", throwable);
65 procedure.execute(graph, null);
69 public void execute(AsyncReadGraph graph, final ElementFactory factory) {
71 graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
74 public void exception(AsyncReadGraph graph, Throwable throwable) {
75 errorHandler.error("Unexpected ElementClass creation failure", throwable);
76 procedure.execute(graph, null);
80 public void execute(AsyncReadGraph graph, final ElementClass ec) {
82 graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener<IElement>() {
85 public void exception(AsyncReadGraph graph, Throwable throwable) {
86 errorHandler.error("Unexpected SpawnRequest failure", throwable);
87 procedure.execute(graph, null);
91 public void execute(AsyncReadGraph graph, IElement element) {
93 procedure.execute(graph, element);
95 if (loadListener != null) {
96 //System.out.println("LoadRequest[" + (loadCounter++) + "] for " + data + ": " + element);
97 graph.asyncRequest(new LoadRequest(canvas, diagram, factory, ec, data), loadListener);
99 //System.out.println("Spawn[" + (spawnCounter++) + "] for " + data + ": " + element);
100 factory.load(graph, canvas, diagram, data, element, new AsyncProcedure<IElement>() {
102 public void exception(AsyncReadGraph graph, Throwable throwable) {
103 errorHandler.error("Unexpected ElementFactory.load failure", throwable);
106 public void execute(AsyncReadGraph graph, IElement result) {
107 // Loading complete, don't care.