X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fadapter%2FConnectionRequest.java;fp=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fadapter%2FConnectionRequest.java;h=2529e43d125f5a025743281e5be3f971c3491431;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest.java new file mode 100644 index 000000000..2529e43d1 --- /dev/null +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest.java @@ -0,0 +1,121 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.diagram.adapter; + +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.primitiverequest.Adapter; +import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener; +import org.simantics.db.procedure.AsyncProcedure; +import org.simantics.db.procedure.Listener; +import org.simantics.diagram.synchronization.ErrorHandler; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.IElement; + +/** + * @author Antti Villberg + */ +public class ConnectionRequest extends BaseRequest2 { + + final IDiagram diagram; + final Listener loadListener; + final ErrorHandler errorHandler; + + public ConnectionRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, ErrorHandler errorHandler, Listener loadListener) { + super(canvas, resource); + this.diagram = diagram; + this.errorHandler = errorHandler; + this.loadListener = loadListener; + } + + @Override + public void perform(AsyncReadGraph graph, final AsyncProcedure procedure) { + + graph.forHasStatement(data, new AsyncProcedure() { + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + procedure.exception(graph, throwable); + } + + @Override + public void execute(AsyncReadGraph graph, Boolean result) { + + if (!result) { + procedure.execute(graph, null); + return; + } + + graph.asyncRequest(new Adapter(data, ElementFactory.class), new TransientCacheAsyncListener() { + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + errorHandler.error("Unexpected ElementFactory adaption failure", throwable); + procedure.execute(graph, null); + } + + @Override + public void execute(AsyncReadGraph graph, final ElementFactory factory) { + + graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener() { + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + errorHandler.error("Unexpected ElementClass creation failure", throwable); + procedure.execute(graph, null); + } + + @Override + public void execute(AsyncReadGraph graph, final ElementClass ec) { + + graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener() { + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + errorHandler.error("Unexpected SpawnRequest failure", throwable); + procedure.execute(graph, null); + } + + @Override + public void execute(AsyncReadGraph graph, IElement element) { + + procedure.execute(graph, element); + + if (loadListener != null) { + //System.out.println("LoadRequest[" + (loadCounter++) + "] for " + data + ": " + element); + graph.asyncRequest(new LoadRequest(canvas, diagram, factory, ec, data), loadListener); + } else { + //System.out.println("Spawn[" + (spawnCounter++) + "] for " + data + ": " + element); + factory.load(graph, canvas, diagram, data, element, new AsyncProcedure() { + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + errorHandler.error("Unexpected ElementFactory.load failure", throwable); + } + @Override + public void execute(AsyncReadGraph graph, IElement result) { + // Loading complete, don't care. + } + }); + } + } + }); + } + }); + } + }); + } + }); + } + +}