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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.adapter;
\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
25 * @author Tuukka Lehtonen
\r
27 public class ElementFactoryAdapter implements ElementFactory {
\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
35 protected Resource getElementClassBaseType(AsyncReadGraph graph) {
\r
36 return graph.getService(DiagramResource.class).Element;
\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
45 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
46 procedure.exception(graph, throwable);
\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
54 public void exception(AsyncReadGraph graph, Throwable t) {
\r
55 procedure.exception(graph, t);
\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
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