--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.adapter;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.IElement;\r
+\r
+/**\r
+ * A synchronous graph request wrapper for {@link ElementFactory} and\r
+ * {@link ElementFactoryAdapter}. Using it will provide simpler but slower\r
+ * implementations.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SyncElementFactory extends ElementFactoryAdapter {\r
+\r
+ @Override\r
+ public void create(AsyncReadGraph graph, final ICanvasContext canvas, final IDiagram diagram,\r
+ final Resource elementType, final AsyncProcedure<ElementClass> procedure) {\r
+ graph.asyncRequest(new UniqueRead<ElementClass>() {\r
+ @Override\r
+ public ElementClass perform(ReadGraph graph) throws DatabaseException {\r
+ return create(graph, canvas, diagram, elementType);\r
+ }\r
+ }, procedure);\r
+ }\r
+\r
+ @Override\r
+ public void load(AsyncReadGraph graph, final ICanvasContext canvas, final IDiagram diagram,\r
+ final Resource elementResource, final IElement element, final AsyncProcedure<IElement> procedure) {\r
+ graph.asyncRequest(new UniqueRead<IElement>() {\r
+ @Override\r
+ public IElement perform(ReadGraph graph) throws DatabaseException {\r
+ if(isOk(graph, elementResource))\r
+ load(graph, canvas, diagram, elementResource, element);\r
+ return element;\r
+ }\r
+ }, procedure);\r
+ }\r
+\r
+ public ElementClass create(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType)\r
+ throws DatabaseException {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+ \r
+ public boolean isOk(ReadGraph graph, Resource elementResource) throws DatabaseException {\r
+ return graph.hasStatement(elementResource);\r
+ }\r
+\r
+ public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementResource, IElement element)\r
+ throws DatabaseException {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+}\r