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.diagram.synchronization.ErrorHandler;
22 import org.simantics.g2d.canvas.ICanvasContext;
23 import org.simantics.g2d.diagram.IDiagram;
24 import org.simantics.g2d.diagram.handler.SubstituteElementClass;
25 import org.simantics.g2d.element.ElementClass;
26 import org.simantics.g2d.element.IElement;
27 import org.simantics.scl.runtime.tuple.Tuple3;
30 * @author Antti Villberg
32 public class ConnectionRequest2 extends BaseRequest2<Resource, Tuple3> {
34 final IDiagram diagram;
35 final ErrorHandler errorHandler;
37 public ConnectionRequest2(ICanvasContext canvas, IDiagram diagram, Resource resource, ErrorHandler errorHandler) {
38 super(canvas, resource);
39 this.diagram = diagram;
40 this.errorHandler = errorHandler;
44 public void perform(AsyncReadGraph graph, final AsyncProcedure<Tuple3> procedure) {
46 graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
49 public void exception(AsyncReadGraph graph, Throwable throwable) {
50 procedure.exception(graph, throwable);
54 public void execute(AsyncReadGraph graph, Boolean result) {
57 procedure.execute(graph, null);
61 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
64 public void exception(AsyncReadGraph graph, Throwable throwable) {
65 errorHandler.error("Unexpected ElementFactory adaption failure", throwable);
66 procedure.execute(graph, null);
70 public void execute(AsyncReadGraph graph, final ElementFactory factory) {
72 graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
75 public void exception(AsyncReadGraph graph, Throwable throwable) {
76 errorHandler.error("Unexpected ElementClass creation failure", throwable);
77 procedure.execute(graph, null);
81 public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
83 List<SubstituteElementClass> substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class);
84 for (SubstituteElementClass subs : substitutes) {
85 mutableClazz = subs.substitute(diagram, mutableClazz);
87 final ElementClass ec = mutableClazz;
89 graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener<IElement>() {
92 public void exception(AsyncReadGraph graph, Throwable throwable) {
93 errorHandler.error("Unexpected SpawnRequest failure", throwable);
94 procedure.execute(graph, null);
98 public void execute(AsyncReadGraph graph, IElement element) {
99 procedure.execute(graph, new Tuple3(element, ec, factory));