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;
27 * @author Antti Villberg
29 public class NodeRequest extends BaseRequest2<Resource, IElement> {
31 final IDiagram diagram;
32 final Listener<IElement> loadListener;
34 public NodeRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, Listener<IElement> loadListener) {
35 super(canvas, resource);
36 this.diagram = diagram;
37 this.loadListener = loadListener;
41 public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
42 // Keep this code from crashing the whole DB client by unexpected
43 // throwing of NPE's somewhere in the following code that leads to
44 // procedure not getting called properly.
45 if (diagram == null) {
46 procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data));
50 // System.out.println("NodeRequest2 " + data);
51 // graph.asyncRequest(new SafeName(data), new Procedure<String>() {
53 // public void exception(Throwable t) {
56 // public void execute(String result) {
57 // System.out.println("NodeRequest2 " + result);
61 final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram);
63 graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
66 public void exception(AsyncReadGraph graph, Throwable throwable) {
67 eh.error("NodeRequest.forHasStatement failed", throwable);
68 procedure.execute(graph, null);
72 public void execute(AsyncReadGraph graph, Boolean result) {
75 procedure.execute(graph, null);
79 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
82 public void exception(AsyncReadGraph graph, Throwable throwable) {
83 eh.error("NodeRequest.asyncRequest(Adapter<ElementFactory>) failed", throwable);
84 procedure.execute(graph, null);
88 public void execute(AsyncReadGraph graph, final ElementFactory factory) {
90 // graph.asyncRequest(new ResourceToURI(data), new Procedure<String>() {
92 // public void exception(Throwable t) {
95 // public void execute(String result) {
96 // System.out.println("NodeRequest2 factory for " + result + " -> " + factory);
100 graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
103 public void exception(AsyncReadGraph graph, Throwable throwable) {
104 throwable.printStackTrace();
105 procedure.execute(graph, null);
109 public void execute(AsyncReadGraph graph, final ElementClass clazz) {
111 graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {
114 public void exception(AsyncReadGraph graph, Throwable throwable) {
115 throwable.printStackTrace();
116 procedure.execute(graph, null);
120 public void execute(AsyncReadGraph graph, IElement element) {
121 procedure.execute(graph, element);
123 if (loadListener != null) {
124 //System.out.println("LoadRequest[" + (loadCounter++) + "] for " + data + ": " + element);
125 graph.asyncRequest(new LoadRequest(canvas, diagram, factory, clazz, data), loadListener);
127 //System.out.println("Spawn[" + (spawnCounter++) + "] for " + data + ": " + element);
128 factory.load(graph, canvas, diagram, data, element, new AsyncProcedure<IElement>() {
130 public void exception(AsyncReadGraph graph, Throwable throwable) {
131 // TODO: proper logging
132 throwable.printStackTrace();
135 public void execute(AsyncReadGraph graph, IElement result) {
136 // Loading complete, don't care.
148 // graph.asyncRequest(new SafeName(data), new Procedure<String>() {
150 // public void exception(Throwable t) {
153 // public void execute(String result) {
154 // System.out.println("NodeRequest2 factory " + result + " " + factory.getClass().getName());
168 static int loadCounter = 0;
169 static int spawnCounter = 0;