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%2FNodeRequest.java;h=a6f2e0a77f4f354c146f98d52e38072a0f1cb7fd;hp=465614db57b4237493299fd25ba9fdd9192c4143;hb=2b0fe692f116091f8d65da664174c92591a077b8;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/NodeRequest.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/NodeRequest.java index 465614db5..a6f2e0a77 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/NodeRequest.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/NodeRequest.java @@ -1,171 +1,174 @@ -/******************************************************************************* - * 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 NodeRequest extends BaseRequest2 { - - final IDiagram diagram; - final Listener loadListener; - - public NodeRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, Listener loadListener) { - super(canvas, resource); - this.diagram = diagram; - this.loadListener = loadListener; - } - - @Override - public void perform(AsyncReadGraph graph, final AsyncProcedure procedure) { - // Keep this code from crashing the whole DB client by unexpected - // throwing of NPE's somewhere in the following code that leads to - // procedure not getting called properly. - if (diagram == null) { - procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data)); - return; - } - -// System.out.println("NodeRequest2 " + data); -// graph.asyncRequest(new SafeName(data), new Procedure() { -// @Override -// public void exception(Throwable t) { -// } -// @Override -// public void execute(String result) { -// System.out.println("NodeRequest2 " + result); -// } -// }); - - final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram); - - graph.forHasStatement(data, new AsyncProcedure() { - - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - eh.error("NodeRequest.forHasStatement failed", throwable); - procedure.execute(graph, null); - } - - @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) { - eh.error("NodeRequest.asyncRequest(Adapter) failed", throwable); - procedure.execute(graph, null); - } - - @Override - public void execute(AsyncReadGraph graph, final ElementFactory factory) { - -// graph.asyncRequest(new ResourceToURI(data), new Procedure() { -// @Override -// public void exception(Throwable t) { -// } -// @Override -// public void execute(String result) { -// System.out.println("NodeRequest2 factory for " + result + " -> " + factory); -// } -// }); - - graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener() { - - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - throwable.printStackTrace(); - procedure.execute(graph, null); - } - - @Override - public void execute(AsyncReadGraph graph, final ElementClass clazz) { - - graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener() { - - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - throwable.printStackTrace(); - 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, clazz, 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) { - // TODO: proper logging - throwable.printStackTrace(); - } - @Override - public void execute(AsyncReadGraph graph, IElement result) { - // Loading complete, don't care. - } - }); - } - } - - }); - - } - - }); - -// graph.asyncRequest(new SafeName(data), new Procedure() { -// @Override -// public void exception(Throwable t) { -// } -// @Override -// public void execute(String result) { -// System.out.println("NodeRequest2 factory " + result + " " + factory.getClass().getName()); -// } -// }); - - } - - }); - - } - - }); - - } - - static int loadCounter = 0; - static int spawnCounter = 0; - -} +/******************************************************************************* + * 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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Antti Villberg + */ +public class NodeRequest extends BaseRequest2 { + + private static final Logger LOGGER = LoggerFactory.getLogger(NodeRequest.class); + + final IDiagram diagram; + final Listener loadListener; + + public NodeRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, Listener loadListener) { + super(canvas, resource); + this.diagram = diagram; + this.loadListener = loadListener; + } + + @Override + public void perform(AsyncReadGraph graph, final AsyncProcedure procedure) { + // Keep this code from crashing the whole DB client by unexpected + // throwing of NPE's somewhere in the following code that leads to + // procedure not getting called properly. + if (diagram == null) { + procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data)); + return; + } + +// System.out.println("NodeRequest2 " + data); +// graph.asyncRequest(new SafeName(data), new Procedure() { +// @Override +// public void exception(Throwable t) { +// } +// @Override +// public void execute(String result) { +// System.out.println("NodeRequest2 " + result); +// } +// }); + + final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram); + + graph.forHasStatement(data, new AsyncProcedure() { + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + eh.error("NodeRequest.forHasStatement failed", throwable); + procedure.execute(graph, null); + } + + @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) { + eh.error("NodeRequest.asyncRequest(Adapter) failed", throwable); + procedure.execute(graph, null); + } + + @Override + public void execute(AsyncReadGraph graph, final ElementFactory factory) { + +// graph.asyncRequest(new ResourceToURI(data), new Procedure() { +// @Override +// public void exception(Throwable t) { +// } +// @Override +// public void execute(String result) { +// System.out.println("NodeRequest2 factory for " + result + " -> " + factory); +// } +// }); + + graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener() { + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + LOGGER.error("Unexpected error in GetElementClassRequest", throwable); + procedure.execute(graph, null); + } + + @Override + public void execute(AsyncReadGraph graph, final ElementClass clazz) { + + graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener() { + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + LOGGER.error("Unexpected error in SpawnRequest", 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, clazz, 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) { + LOGGER.error("Unexpected error in ElementFactory.load (factory=" + factory + ")", throwable); + } + @Override + public void execute(AsyncReadGraph graph, IElement result) { + // Loading complete, don't care. + } + }); + } + } + + }); + + } + + }); + +// graph.asyncRequest(new SafeName(data), new Procedure() { +// @Override +// public void exception(Throwable t) { +// } +// @Override +// public void execute(String result) { +// System.out.println("NodeRequest2 factory " + result + " " + factory.getClass().getName()); +// } +// }); + + } + + }); + + } + + }); + + } + + static int loadCounter = 0; + static int spawnCounter = 0; + +}