1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.adapter;
\r
14 import org.simantics.db.AsyncReadGraph;
\r
15 import org.simantics.db.Resource;
\r
16 import org.simantics.db.common.primitiverequest.Adapter;
\r
17 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
\r
18 import org.simantics.db.procedure.AsyncProcedure;
\r
19 import org.simantics.db.procedure.Listener;
\r
20 import org.simantics.diagram.synchronization.ErrorHandler;
\r
21 import org.simantics.g2d.canvas.ICanvasContext;
\r
22 import org.simantics.g2d.diagram.IDiagram;
\r
23 import org.simantics.g2d.element.ElementClass;
\r
24 import org.simantics.g2d.element.IElement;
\r
27 * @author Antti Villberg
\r
29 public class NodeRequest extends BaseRequest2<Resource, IElement> {
\r
31 final IDiagram diagram;
\r
32 final Listener<IElement> loadListener;
\r
34 public NodeRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, Listener<IElement> loadListener) {
\r
35 super(canvas, resource);
\r
36 this.diagram = diagram;
\r
37 this.loadListener = loadListener;
\r
41 public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
\r
42 // Keep this code from crashing the whole DB client by unexpected
\r
43 // throwing of NPE's somewhere in the following code that leads to
\r
44 // procedure not getting called properly.
\r
45 if (diagram == null) {
\r
46 procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data));
\r
50 // System.out.println("NodeRequest2 " + data);
\r
51 // graph.asyncRequest(new SafeName(data), new Procedure<String>() {
\r
53 // public void exception(Throwable t) {
\r
56 // public void execute(String result) {
\r
57 // System.out.println("NodeRequest2 " + result);
\r
61 final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram);
\r
63 graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
\r
66 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
67 eh.error("NodeRequest.forHasStatement failed", throwable);
\r
68 procedure.execute(graph, null);
\r
72 public void execute(AsyncReadGraph graph, Boolean result) {
\r
75 procedure.execute(graph, null);
\r
79 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
\r
82 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
83 eh.error("NodeRequest.asyncRequest(Adapter<ElementFactory>) failed", throwable);
\r
84 procedure.execute(graph, null);
\r
88 public void execute(AsyncReadGraph graph, final ElementFactory factory) {
\r
90 // graph.asyncRequest(new ResourceToURI(data), new Procedure<String>() {
\r
92 // public void exception(Throwable t) {
\r
95 // public void execute(String result) {
\r
96 // System.out.println("NodeRequest2 factory for " + result + " -> " + factory);
\r
100 graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
\r
103 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
104 throwable.printStackTrace();
\r
105 procedure.execute(graph, null);
\r
109 public void execute(AsyncReadGraph graph, final ElementClass clazz) {
\r
111 graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {
\r
114 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
115 throwable.printStackTrace();
\r
116 procedure.execute(graph, null);
\r
120 public void execute(AsyncReadGraph graph, IElement element) {
\r
121 procedure.execute(graph, element);
\r
123 if (loadListener != null) {
\r
124 //System.out.println("LoadRequest[" + (loadCounter++) + "] for " + data + ": " + element);
\r
125 graph.asyncRequest(new LoadRequest(canvas, diagram, factory, clazz, data), loadListener);
\r
127 //System.out.println("Spawn[" + (spawnCounter++) + "] for " + data + ": " + element);
\r
128 factory.load(graph, canvas, diagram, data, element, new AsyncProcedure<IElement>() {
\r
130 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
131 // TODO: proper logging
\r
132 throwable.printStackTrace();
\r
135 public void execute(AsyncReadGraph graph, IElement result) {
\r
136 // Loading complete, don't care.
\r
148 // graph.asyncRequest(new SafeName(data), new Procedure<String>() {
\r
150 // public void exception(Throwable t) {
\r
153 // public void execute(String result) {
\r
154 // System.out.println("NodeRequest2 factory " + result + " " + factory.getClass().getName());
\r
168 static int loadCounter = 0;
\r
169 static int spawnCounter = 0;
\r