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.symbolcontribution;
14 import org.simantics.Simantics;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.RequestProcessor;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.request.ResourceRead;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.procedure.Listener;
21 import org.simantics.db.request.Read;
22 import org.simantics.diagram.query.DiagramRequests;
23 import org.simantics.diagram.symbollibrary.ISymbolGroup;
24 import org.simantics.diagram.symbollibrary.ISymbolItem;
25 import org.simantics.diagram.synchronization.graph.BasicResources;
26 import org.simantics.g2d.element.ElementClass;
27 import org.simantics.g2d.element.handler.StaticSymbol;
28 import org.simantics.utils.datastructures.cache.ProvisionException;
29 import org.simantics.utils.datastructures.hints.IHintObservable;
32 * @author Tuukka Lehtonen
34 public class ElementSymbolItem extends SymbolItem {
36 public ElementSymbolItem(Resource item, String name, ISymbolGroup group) {
40 // Prevents infinite recursion which will happen
41 // if SymbolGroup is used as group ID.
42 group instanceof IIdentifiedObject ? ((IIdentifiedObject) group).getId() : group,
43 // Name is in the identification so that name
44 // changes refresh the symbol library
50 public ElementSymbolItem(Resource item, String name, String description, ISymbolGroup group) {
54 // Prevents infinite recursion which will happen
55 // if SymbolGroup is used as group ID.
56 group instanceof IIdentifiedObject ? ((IIdentifiedObject) group).getId() : group,
57 // Name is in the identification so that name
58 // changes refresh the symbol library
66 public ElementClass getElementClass(IHintObservable hints) {
67 Resource item = adapt(Resource.class);
68 RequestProcessor sgrp = Simantics.peekSession();
70 throw new ProvisionException("No RequestProcessor available for querying an ElementClass for resource " + item);
72 Listener<ElementClass> listener = hints.getHint(ISymbolItem.KEY_ELEMENT_CLASS_LISTENER);
73 ElementClass ec = listener == null
74 ? sgrp.syncRequest(createRequest(item, hints))
75 : sgrp.syncRequest(createRequest(item, hints), listener);
77 throw new ProvisionException("ElementClass query failed, returned null");
78 if (!ec.containsClass(StaticSymbol.class))
79 throw new ProvisionException("ElementClass " + ec + " does not provide a StaticSymbol handler");
81 } catch (DatabaseException e) {
82 throw new ProvisionException(e);
86 private Read<ElementClass> createRequest(Resource item, final IHintObservable hints) {
87 return new ResourceRead<ElementClass>(item) {
89 public ElementClass perform(ReadGraph graph) throws DatabaseException {
90 BasicResources.getInstance(graph);
91 return graph.syncRequest( DiagramRequests.getElementClass(resource, hints) );