]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ElementFactoryAdapter.java
Logger fixes after merge commit:fdbe8762
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / ElementFactoryAdapter.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.adapter;\r
13 \r
14 import org.simantics.db.AsyncReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
17 import org.simantics.db.procedure.AsyncProcedure;\r
18 import org.simantics.diagram.stubs.DiagramResource;\r
19 import org.simantics.g2d.canvas.ICanvasContext;\r
20 import org.simantics.g2d.diagram.IDiagram;\r
21 import org.simantics.g2d.element.ElementClass;\r
22 import org.simantics.g2d.element.IElement;\r
23 \r
24 /**\r
25  * @author Tuukka Lehtonen\r
26  */\r
27 public class ElementFactoryAdapter implements ElementFactory {\r
28 \r
29     @Override\r
30     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType,\r
31             AsyncProcedure<ElementClass> procedure) {\r
32         procedure.exception(graph, new UnsupportedOperationException(getClass().getName() + " does not support ElementFactory.create"));\r
33     }\r
34 \r
35     protected Resource getElementClassBaseType(AsyncReadGraph graph) {\r
36         return graph.getService(DiagramResource.class).Element;\r
37     }\r
38 \r
39     @Override\r
40     public void getClass(AsyncReadGraph graph, final ICanvasContext canvas, final IDiagram diagram,\r
41             Resource elementResource, final AsyncProcedure<ElementClass> procedure) {\r
42         // A most usual default implementation.\r
43         graph.forSingleType(elementResource, getElementClassBaseType(graph), new AsyncProcedure<Resource>() {\r
44             @Override\r
45             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
46                 procedure.exception(graph, throwable);\r
47             }\r
48             @Override\r
49             public void execute(AsyncReadGraph graph, Resource elementType) {\r
50                 // Implements caching of element class requests.\r
51                 graph.asyncRequest(new CreateElementClassRequest(ElementFactoryAdapter.this, elementType, canvas, diagram),\r
52                         new TransientCacheAsyncListener<ElementClass>() {\r
53                     @Override\r
54                     public void exception(AsyncReadGraph graph, Throwable t) {\r
55                         procedure.exception(graph, t);\r
56                     }\r
57                     @Override\r
58                     public void execute(AsyncReadGraph graph, ElementClass result) {\r
59                         //System.out.println("RESULT ELEMENTCLASS: " + System.identityHashCode(result) + ": " + result);\r
60                         procedure.execute(graph, result);\r
61                     }\r
62                 });\r
63             }\r
64         });\r
65     }\r
66 \r
67     @Override\r
68     public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementResource,\r
69             IElement element, AsyncProcedure<IElement> procedure) {\r
70         procedure.exception(graph, new UnsupportedOperationException(getClass().getName() + " does not support ElementFactory.load"));\r
71     }\r
72 \r
73 }\r