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 java.util.List;
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.primitiverequest.Adapter;
19 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
20 import org.simantics.db.procedure.AsyncProcedure;
21 import org.simantics.db.procedure.Listener;
22 import org.simantics.diagram.synchronization.ErrorHandler;
23 import org.simantics.g2d.canvas.ICanvasContext;
24 import org.simantics.g2d.diagram.IDiagram;
25 import org.simantics.g2d.diagram.handler.SubstituteElementClass;
26 import org.simantics.g2d.element.ElementClass;
27 import org.simantics.g2d.element.IElement;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * @author Antti Villberg
34 public class NodeRequest extends BaseRequest2<Resource, IElement> {
36 private static final Logger LOGGER = LoggerFactory.getLogger(NodeRequest.class);
38 final IDiagram diagram;
39 final Listener<IElement> loadListener;
41 public NodeRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, Listener<IElement> loadListener) {
42 super(canvas, resource);
43 this.diagram = diagram;
44 this.loadListener = loadListener;
48 public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
49 // Keep this code from crashing the whole DB client by unexpected
50 // throwing of NPE's somewhere in the following code that leads to
51 // procedure not getting called properly.
52 if (diagram == null) {
53 procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data));
57 // System.out.println("NodeRequest2 " + data);
58 // graph.asyncRequest(new SafeName(data), new Procedure<String>() {
60 // public void exception(Throwable t) {
63 // public void execute(String result) {
64 // System.out.println("NodeRequest2 " + result);
68 final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram);
70 graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
73 public void exception(AsyncReadGraph graph, Throwable throwable) {
74 eh.error("NodeRequest.forHasStatement failed", throwable);
75 procedure.execute(graph, null);
79 public void execute(AsyncReadGraph graph, Boolean result) {
82 procedure.execute(graph, null);
86 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
89 public void exception(AsyncReadGraph graph, Throwable throwable) {
90 eh.error("NodeRequest.asyncRequest(Adapter<ElementFactory>) failed", throwable);
91 procedure.execute(graph, null);
95 public void execute(AsyncReadGraph graph, final ElementFactory factory) {
97 // graph.asyncRequest(new ResourceToURI(data), new Procedure<String>() {
99 // public void exception(Throwable t) {
102 // public void execute(String result) {
103 // System.out.println("NodeRequest2 factory for " + result + " -> " + factory);
107 graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
110 public void exception(AsyncReadGraph graph, Throwable throwable) {
111 LOGGER.error("Unexpected error in GetElementClassRequest", throwable);
112 procedure.execute(graph, null);
116 public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
117 List<SubstituteElementClass> substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class);
118 for (SubstituteElementClass subs : substitutes) {
119 mutableClazz = subs.substitute(diagram, mutableClazz);
121 final ElementClass clazz = mutableClazz;
122 graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {
125 public void exception(AsyncReadGraph graph, Throwable throwable) {
126 LOGGER.error("Unexpected error in SpawnRequest", throwable);
127 procedure.execute(graph, null);
131 public void execute(AsyncReadGraph graph, IElement element) {
132 procedure.execute(graph, element);
134 if (loadListener != null) {
135 //System.out.println("LoadRequest[" + (loadCounter++) + "] for " + data + ": " + element);
136 graph.asyncRequest(new LoadRequest(canvas, diagram, factory, clazz, data), loadListener);
138 //System.out.println("Spawn[" + (spawnCounter++) + "] for " + data + ": " + element);
139 factory.load(graph, canvas, diagram, data, element, new AsyncProcedure<IElement>() {
141 public void exception(AsyncReadGraph graph, Throwable throwable) {
142 LOGGER.error("Unexpected error in ElementFactory.load (factory=" + factory + ")", throwable);
145 public void execute(AsyncReadGraph graph, IElement result) {
146 // Loading complete, don't care.
158 // graph.asyncRequest(new SafeName(data), new Procedure<String>() {
160 // public void exception(Throwable t) {
163 // public void execute(String result) {
164 // System.out.println("NodeRequest2 factory " + result + " " + factory.getClass().getName());
178 static int loadCounter = 0;
179 static int spawnCounter = 0;