--- /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.symbolcontribution;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.RequestProcessor;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.diagram.query.DiagramRequests;\r
+import org.simantics.diagram.symbollibrary.ISymbolGroup;\r
+import org.simantics.diagram.symbollibrary.ISymbolItem;\r
+import org.simantics.diagram.synchronization.graph.BasicResources;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.handler.StaticSymbol;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.datastructures.cache.ProvisionException;\r
+import org.simantics.utils.datastructures.hints.IHintObservable;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ElementSymbolItem extends SymbolItem {\r
+\r
+ public ElementSymbolItem(Resource item, String name, ISymbolGroup group) {\r
+ super(\r
+ AdaptableTriple.make(\r
+ item,\r
+ // Prevents infinite recursion which will happen\r
+ // if SymbolGroup is used as group ID.\r
+ group instanceof IIdentifiedObject ? ((IIdentifiedObject) group).getId() : group,\r
+ // Name is in the identification so that name\r
+ // changes refresh the symbol library\r
+ name),\r
+ name,\r
+ group);\r
+ }\r
+\r
+ public ElementSymbolItem(Resource item, String name, String description, ISymbolGroup group) {\r
+ super(\r
+ AdaptableTriple.make(\r
+ item,\r
+ // Prevents infinite recursion which will happen\r
+ // if SymbolGroup is used as group ID.\r
+ group instanceof IIdentifiedObject ? ((IIdentifiedObject) group).getId() : group,\r
+ // Name is in the identification so that name\r
+ // changes refresh the symbol library\r
+ name),\r
+ name,\r
+ description,\r
+ group);\r
+ }\r
+\r
+ @Override\r
+ public ElementClass getElementClass(IHintObservable hints) {\r
+ Resource item = adapt(Resource.class);\r
+ RequestProcessor sgrp = SimanticsUI.peekSession();\r
+ if (sgrp == null)\r
+ throw new ProvisionException("No RequestProcessor available for querying an ElementClass for resource " + item);\r
+ try {\r
+ Listener<ElementClass> listener = hints.getHint(ISymbolItem.KEY_ELEMENT_CLASS_LISTENER);\r
+ ElementClass ec = listener == null\r
+ ? sgrp.syncRequest(createRequest(item, hints))\r
+ : sgrp.syncRequest(createRequest(item, hints), listener);\r
+ if (ec == null)\r
+ throw new ProvisionException("ElementClass query failed, returned null");\r
+ if (!ec.containsClass(StaticSymbol.class))\r
+ throw new ProvisionException("ElementClass " + ec + " does not provide a StaticSymbol handler");\r
+ return ec;\r
+ } catch (DatabaseException e) {\r
+ throw new ProvisionException(e);\r
+ }\r
+ }\r
+\r
+ private Read<ElementClass> createRequest(Resource item, final IHintObservable hints) {\r
+ return new ResourceRead<ElementClass>(item) {\r
+ @Override\r
+ public ElementClass perform(ReadGraph graph) throws DatabaseException {\r
+ BasicResources.getInstance(graph);\r
+ return graph.syncRequest( DiagramRequests.getElementClass(resource, hints) );\r
+ }\r
+ };\r
+ }\r
+\r
+}\r