]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/SyncElementFactory.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / SyncElementFactory.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.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.request.UniqueRead;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.g2d.canvas.ICanvasContext;
21 import org.simantics.g2d.diagram.IDiagram;
22 import org.simantics.g2d.element.ElementClass;
23 import org.simantics.g2d.element.IElement;
24
25 /**
26  * A synchronous graph request wrapper for {@link ElementFactory} and
27  * {@link ElementFactoryAdapter}. Using it will provide simpler but slower
28  * implementations.
29  * 
30  * @author Tuukka Lehtonen
31  */
32 public class SyncElementFactory extends ElementFactoryAdapter {
33
34     @Override
35     public void create(AsyncReadGraph graph, final ICanvasContext canvas, final IDiagram diagram,
36             final Resource elementType, final AsyncProcedure<ElementClass> procedure) {
37         graph.asyncRequest(new UniqueRead<ElementClass>() {
38             @Override
39             public ElementClass perform(ReadGraph graph) throws DatabaseException {
40                 return create(graph, canvas, diagram, elementType);
41             }
42         }, procedure);
43     }
44
45     @Override
46     public void load(AsyncReadGraph graph, final ICanvasContext canvas, final IDiagram diagram,
47             final Resource elementResource, final IElement element, final AsyncProcedure<IElement> procedure) {
48         graph.asyncRequest(new UniqueRead<IElement>() {
49             @Override
50             public IElement perform(ReadGraph graph) throws DatabaseException {
51                 if(isOk(graph, elementResource))
52                         load(graph, canvas, diagram, elementResource, element);
53                 return element;
54             }
55         }, procedure);
56     }
57
58     public ElementClass create(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType)
59     throws DatabaseException {
60         throw new UnsupportedOperationException("This method should be implemented by subclass " + getClass().getName());
61     }
62     
63     public boolean isOk(ReadGraph graph, Resource elementResource) throws DatabaseException {
64         return graph.hasStatement(elementResource);
65     }
66
67     public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementResource, IElement element)
68     throws DatabaseException {
69         throw new UnsupportedOperationException("This method should be implemented by subclass " + getClass().getName());
70     }
71
72 }