1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.adapter;
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;
25 * @author Tuukka Lehtonen
27 public class ElementFactoryAdapter implements ElementFactory {
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"));
35 protected Resource getElementClassBaseType(AsyncReadGraph graph) {
36 return graph.getService(DiagramResource.class).Element;
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>() {
45 public void exception(AsyncReadGraph graph, Throwable throwable) {
46 procedure.exception(graph, throwable);
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>() {
54 public void exception(AsyncReadGraph graph, Throwable t) {
55 procedure.exception(graph, t);
58 public void execute(AsyncReadGraph graph, ElementClass result) {
59 //System.out.println("RESULT ELEMENTCLASS: " + System.identityHashCode(result) + ": " + result);
60 procedure.execute(graph, result);
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"));