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.simantics.scl.runtime.tuple.Tuple3;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * @author Antti Villberg
35 public class NodeRequest2 extends BaseRequest2<Resource, Tuple3> {
37 private static final Logger LOGGER = LoggerFactory.getLogger(NodeRequest2.class);
39 final IDiagram diagram;
41 public NodeRequest2(ICanvasContext canvas, IDiagram diagram, Resource resource) {
42 super(canvas, resource);
43 this.diagram = diagram;
47 public void perform(AsyncReadGraph graph, final AsyncProcedure<Tuple3> procedure) {
48 // Keep this code from crashing the whole DB client by unexpected
49 // throwing of NPE's somewhere in the following code that leads to
50 // procedure not getting called properly.
51 if (diagram == null) {
52 procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data));
56 final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram);
58 graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
61 public void exception(AsyncReadGraph graph, Throwable throwable) {
62 eh.error("NodeRequest.forHasStatement failed", throwable);
63 procedure.execute(graph, null);
67 public void execute(AsyncReadGraph graph, Boolean result) {
70 procedure.execute(graph, null);
74 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
77 public void exception(AsyncReadGraph graph, Throwable throwable) {
78 eh.error("NodeRequest.asyncRequest(Adapter<ElementFactory>) failed", throwable);
79 procedure.execute(graph, null);
83 public void execute(AsyncReadGraph graph, final ElementFactory factory) {
85 graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
88 public void exception(AsyncReadGraph graph, Throwable throwable) {
89 LOGGER.error("Unexpected error in GetElementClassRequest", throwable);
90 procedure.execute(graph, null);
94 public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
95 List<SubstituteElementClass> substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class);
96 for (SubstituteElementClass subs : substitutes) {
97 mutableClazz = subs.substitute(diagram, mutableClazz);
99 final ElementClass clazz = mutableClazz;
100 graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {
103 public void exception(AsyncReadGraph graph, Throwable throwable) {
104 LOGGER.error("Unexpected error in SpawnRequest", throwable);
105 procedure.execute(graph, null);
109 public void execute(AsyncReadGraph graph, IElement element) {
110 procedure.execute(graph, new Tuple3(element, clazz, factory));