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.diagram.synchronization.ErrorHandler;
22 import org.simantics.g2d.canvas.ICanvasContext;
23 import org.simantics.g2d.diagram.IDiagram;
24 import org.simantics.g2d.diagram.handler.SubstituteElementClass;
25 import org.simantics.g2d.element.ElementClass;
26 import org.simantics.g2d.element.IElement;
27 import org.simantics.scl.runtime.tuple.Tuple3;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * @author Antti Villberg
34 public class NodeRequest2 extends BaseRequest2<Resource, Tuple3> {
36 private static final Logger LOGGER = LoggerFactory.getLogger(NodeRequest2.class);
38 final IDiagram diagram;
40 public NodeRequest2(ICanvasContext canvas, IDiagram diagram, Resource resource) {
41 super(canvas, resource);
42 this.diagram = diagram;
46 public void perform(AsyncReadGraph graph, final AsyncProcedure<Tuple3> procedure) {
47 // Keep this code from crashing the whole DB client by unexpected
48 // throwing of NPE's somewhere in the following code that leads to
49 // procedure not getting called properly.
50 if (diagram == null) {
51 procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data));
55 final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram);
57 graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
60 public void exception(AsyncReadGraph graph, Throwable throwable) {
61 eh.error("NodeRequest.forHasStatement failed", throwable);
62 procedure.execute(graph, null);
66 public void execute(AsyncReadGraph graph, Boolean result) {
69 procedure.execute(graph, null);
73 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
76 public void exception(AsyncReadGraph graph, Throwable throwable) {
77 eh.error("NodeRequest.asyncRequest(Adapter<ElementFactory>) failed", throwable);
78 procedure.execute(graph, null);
82 public void execute(AsyncReadGraph graph, final ElementFactory factory) {
84 graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
87 public void exception(AsyncReadGraph graph, Throwable throwable) {
88 LOGGER.error("Unexpected error in GetElementClassRequest", throwable);
89 procedure.execute(graph, null);
93 public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
94 List<SubstituteElementClass> substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class);
95 for (SubstituteElementClass subs : substitutes) {
96 mutableClazz = subs.substitute(diagram, mutableClazz);
98 final ElementClass clazz = mutableClazz;
99 graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {
102 public void exception(AsyncReadGraph graph, Throwable throwable) {
103 LOGGER.error("Unexpected error in SpawnRequest", throwable);
104 procedure.execute(graph, null);
108 public void execute(AsyncReadGraph graph, IElement element) {
109 procedure.execute(graph, new Tuple3(element, clazz, factory));