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.List;
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.primitiverequest.Adapter;
19 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
20 import org.simantics.db.procedure.AsyncProcedure;
21 import org.simantics.db.procedure.Listener;
22 import org.simantics.diagram.synchronization.ErrorHandler;
23 import org.simantics.g2d.canvas.ICanvasContext;
24 import org.simantics.g2d.diagram.IDiagram;
25 import org.simantics.g2d.element.ElementClass;
26 import org.simantics.g2d.element.IElement;
29 * @author Antti Villberg
31 public class ConnectionRequest extends BaseRequest2<Resource, IElement> {
33 final IDiagram diagram;
34 final Listener<IElement> loadListener;
35 final ErrorHandler errorHandler;
37 public ConnectionRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, ErrorHandler errorHandler, Listener<IElement> loadListener) {
38 super(canvas, resource);
39 this.diagram = diagram;
40 this.errorHandler = errorHandler;
41 this.loadListener = loadListener;
45 public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
47 graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
50 public void exception(AsyncReadGraph graph, Throwable throwable) {
51 procedure.exception(graph, throwable);
55 public void execute(AsyncReadGraph graph, Boolean result) {
58 procedure.execute(graph, null);
62 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
65 public void exception(AsyncReadGraph graph, Throwable throwable) {
66 errorHandler.error("Unexpected ElementFactory adaption failure", throwable);
67 procedure.execute(graph, null);
71 public void execute(AsyncReadGraph graph, final ElementFactory factory) {
73 graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
76 public void exception(AsyncReadGraph graph, Throwable throwable) {
77 errorHandler.error("Unexpected ElementClass creation failure", throwable);
78 procedure.execute(graph, null);
82 public void execute(AsyncReadGraph graph, final ElementClass ec) {
84 graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener<IElement>() {
87 public void exception(AsyncReadGraph graph, Throwable throwable) {
88 errorHandler.error("Unexpected SpawnRequest failure", throwable);
89 procedure.execute(graph, null);
93 public void execute(AsyncReadGraph graph, IElement element) {
95 procedure.execute(graph, element);
97 if (loadListener != null) {
98 //System.out.println("LoadRequest[" + (loadCounter++) + "] for " + data + ": " + element);
99 graph.asyncRequest(new LoadRequest(canvas, diagram, factory, ec, data), loadListener);
101 //System.out.println("Spawn[" + (spawnCounter++) + "] for " + data + ": " + element);
102 factory.load(graph, canvas, diagram, data, element, new AsyncProcedure<IElement>() {
104 public void exception(AsyncReadGraph graph, Throwable throwable) {
105 errorHandler.error("Unexpected ElementFactory.load failure", throwable);
108 public void execute(AsyncReadGraph graph, IElement result) {
109 // Loading complete, don't care.