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