]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/query/ElementClassQuery.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / query / ElementClassQuery.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.query;\r
13 \r
14 import org.simantics.db.AsyncReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.common.request.ResourceAsyncRead;\r
17 import org.simantics.db.procedure.AsyncProcedure;\r
18 import org.simantics.diagram.adapter.ElementFactory;\r
19 import org.simantics.g2d.canvas.impl.DisposedCanvasContext;\r
20 import org.simantics.g2d.diagram.IDiagram;\r
21 import org.simantics.g2d.element.ElementClass;\r
22 import org.simantics.utils.datastructures.hints.IHintObservable;\r
23 \r
24 public class ElementClassQuery extends ResourceAsyncRead<ElementClass> {\r
25 \r
26     /**\r
27      * External hints for loading an element class.\r
28      */\r
29     IHintObservable hints;\r
30 \r
31     public ElementClassQuery(Resource resource, IHintObservable hints) {\r
32         super(resource);\r
33         this.hints = hints;\r
34     }\r
35 \r
36     @Override\r
37     public void perform(AsyncReadGraph graph, AsyncProcedure<ElementClass> procedure) {\r
38         graph.forAdapted(resource , ElementFactory.class, createElementClass(procedure));\r
39     }\r
40 \r
41     private AsyncProcedure<ElementFactory> createElementClass(final AsyncProcedure<ElementClass> parent) {\r
42         return new AsyncProcedure<ElementFactory>() {\r
43             @Override\r
44             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
45                 parent.exception(graph, throwable);\r
46             }\r
47 \r
48             @Override\r
49             public void execute(AsyncReadGraph graph, ElementFactory factory) {\r
50                 // TODO: remove (IDiagram) cast\r
51                 factory.create(graph, DisposedCanvasContext.INSTANCE, (IDiagram) hints, resource, new AsyncProcedure<ElementClass>() {\r
52                     @Override\r
53                     public void execute(AsyncReadGraph graph, ElementClass result) {\r
54                         parent.execute(graph, result);\r
55                     }\r
56                     @Override\r
57                     public void exception(AsyncReadGraph graph, Throwable throwable) {\r
58                         parent.exception(graph, throwable);\r
59                     }\r
60                 });\r
61             }\r
62         };\r
63     }\r
64 \r
65 }\r