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;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * @author Antti Villberg
31 public class NodeRequest extends BaseRequest2<Resource, IElement> {
33 private static final Logger LOGGER = LoggerFactory.getLogger(NodeRequest.class);
35 final IDiagram diagram;
36 final Listener<IElement> loadListener;
38 public NodeRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, Listener<IElement> loadListener) {
39 super(canvas, resource);
40 this.diagram = diagram;
41 this.loadListener = loadListener;
45 public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
46 // Keep this code from crashing the whole DB client by unexpected
47 // throwing of NPE's somewhere in the following code that leads to
48 // procedure not getting called properly.
49 if (diagram == null) {
50 procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data));
54 // System.out.println("NodeRequest2 " + data);
55 // graph.asyncRequest(new SafeName(data), new Procedure<String>() {
57 // public void exception(Throwable t) {
60 // public void execute(String result) {
61 // System.out.println("NodeRequest2 " + result);
65 final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram);
67 graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
70 public void exception(AsyncReadGraph graph, Throwable throwable) {
71 eh.error("NodeRequest.forHasStatement failed", throwable);
72 procedure.execute(graph, null);
76 public void execute(AsyncReadGraph graph, Boolean result) {
79 procedure.execute(graph, null);
83 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
86 public void exception(AsyncReadGraph graph, Throwable throwable) {
87 eh.error("NodeRequest.asyncRequest(Adapter<ElementFactory>) failed", throwable);
88 procedure.execute(graph, null);
92 public void execute(AsyncReadGraph graph, final ElementFactory factory) {
94 // graph.asyncRequest(new ResourceToURI(data), new Procedure<String>() {
96 // public void exception(Throwable t) {
99 // public void execute(String result) {
100 // System.out.println("NodeRequest2 factory for " + result + " -> " + factory);
104 graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
107 public void exception(AsyncReadGraph graph, Throwable throwable) {
108 LOGGER.error("Unexpected error in GetElementClassRequest", throwable);
109 procedure.execute(graph, null);
113 public void execute(AsyncReadGraph graph, final ElementClass clazz) {
115 graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {
118 public void exception(AsyncReadGraph graph, Throwable throwable) {
119 LOGGER.error("Unexpected error in SpawnRequest", throwable);
120 procedure.execute(graph, null);
124 public void execute(AsyncReadGraph graph, IElement element) {
125 procedure.execute(graph, element);
127 if (loadListener != null) {
128 //System.out.println("LoadRequest[" + (loadCounter++) + "] for " + data + ": " + element);
129 graph.asyncRequest(new LoadRequest(canvas, diagram, factory, clazz, data), loadListener);
131 //System.out.println("Spawn[" + (spawnCounter++) + "] for " + data + ": " + element);
132 factory.load(graph, canvas, diagram, data, element, new AsyncProcedure<IElement>() {
134 public void exception(AsyncReadGraph graph, Throwable throwable) {
135 LOGGER.error("Unexpected error in ElementFactory.load (factory=" + factory + ")", throwable);
138 public void execute(AsyncReadGraph graph, IElement result) {
139 // Loading complete, don't care.
151 // graph.asyncRequest(new SafeName(data), new Procedure<String>() {
153 // public void exception(Throwable t) {
156 // public void execute(String result) {
157 // System.out.println("NodeRequest2 factory " + result + " " + factory.getClass().getName());
171 static int loadCounter = 0;
172 static int spawnCounter = 0;