]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest.java
Merge "(refs #7178) Validator for build.properties"
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / ConnectionRequest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.adapter;
13
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;
25
26 /**
27  * @author Antti Villberg
28  */
29 public class ConnectionRequest extends BaseRequest2<Resource, IElement> {
30
31     final IDiagram diagram;
32     final Listener<IElement> loadListener;
33     final ErrorHandler errorHandler;
34
35     public ConnectionRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, ErrorHandler errorHandler, Listener<IElement> loadListener) {
36         super(canvas, resource);
37         this.diagram = diagram;
38         this.errorHandler = errorHandler;
39         this.loadListener = loadListener;
40     }
41
42     @Override
43     public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
44
45         graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
46
47             @Override
48             public void exception(AsyncReadGraph graph, Throwable throwable) {
49                 procedure.exception(graph, throwable);
50             }
51
52             @Override
53             public void execute(AsyncReadGraph graph, Boolean result) {
54
55                 if (!result) {
56                     procedure.execute(graph, null);
57                     return;
58                 }
59
60                 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
61
62                     @Override
63                     public void exception(AsyncReadGraph graph, Throwable throwable) {
64                         errorHandler.error("Unexpected ElementFactory adaption failure", throwable);
65                         procedure.execute(graph, null);
66                     }
67
68                     @Override
69                     public void execute(AsyncReadGraph graph, final ElementFactory factory) {
70
71                         graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
72
73                             @Override
74                             public void exception(AsyncReadGraph graph, Throwable throwable) {
75                                 errorHandler.error("Unexpected ElementClass creation failure", throwable);
76                                 procedure.execute(graph, null);
77                             }
78
79                             @Override
80                             public void execute(AsyncReadGraph graph, final ElementClass ec) {
81
82                                 graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener<IElement>() {
83
84                                     @Override
85                                     public void exception(AsyncReadGraph graph, Throwable throwable) {
86                                         errorHandler.error("Unexpected SpawnRequest failure", throwable);
87                                         procedure.execute(graph, null);
88                                     }
89
90                                     @Override
91                                     public void execute(AsyncReadGraph graph, IElement element) {
92
93                                         procedure.execute(graph, element);
94
95                                         if (loadListener != null) {
96                                             //System.out.println("LoadRequest[" + (loadCounter++) + "] for " + data + ": " + element);
97                                             graph.asyncRequest(new LoadRequest(canvas, diagram, factory, ec, data), loadListener);
98                                         } else {
99                                             //System.out.println("Spawn[" + (spawnCounter++) + "] for " + data + ": " + element);
100                                             factory.load(graph, canvas, diagram, data, element, new AsyncProcedure<IElement>() {
101                                                 @Override
102                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
103                                                     errorHandler.error("Unexpected ElementFactory.load failure", throwable);
104                                                 }
105                                                 @Override
106                                                 public void execute(AsyncReadGraph graph, IElement result) {
107                                                     // Loading complete, don't care.
108                                                 }
109                                             });
110                                         }
111                                     }
112                                 });
113                             }
114                         });
115                     }
116                 });
117             }
118         });
119     }
120
121 }