]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbolcontribution/ElementSymbolItem.java
Set initial priority for GroupStyleProfileEntry
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbolcontribution / ElementSymbolItem.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.symbolcontribution;
13
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;
30
31 /**
32  * @author Tuukka Lehtonen
33  */
34 public class ElementSymbolItem extends SymbolItem {
35
36     public ElementSymbolItem(Resource item, String name, ISymbolGroup group) {
37         super(
38                 AdaptableTriple.make(
39                         item,
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
45                         name),
46                 name,
47                 group);
48     }
49
50     public ElementSymbolItem(Resource item, String name, String description, ISymbolGroup group) {
51         super(
52                 AdaptableTriple.make(
53                         item,
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
59                         name),
60                 name,
61                 description,
62                 group);
63     }
64
65     @Override
66     public ElementClass getElementClass(IHintObservable hints) {
67         Resource item = adapt(Resource.class);
68         RequestProcessor sgrp = Simantics.peekSession();
69         if (sgrp == null)
70             throw new ProvisionException("No RequestProcessor available for querying an ElementClass for resource " + item);
71         try {
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);
76             if (ec == null)
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");
80             return ec;
81         } catch (DatabaseException e) {
82             throw new ProvisionException(e);
83         }
84     }
85
86     private Read<ElementClass> createRequest(Resource item, final IHintObservable hints) {
87         return new ResourceRead<ElementClass>(item) {
88             @Override
89             public ElementClass perform(ReadGraph graph) throws DatabaseException {
90                 BasicResources.getInstance(graph);
91                 return graph.syncRequest( DiagramRequests.getElementClass(resource, hints) );
92             }
93         };
94     }
95
96 }