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