1 | package de.uka.ipd.sdq.pcmbench.ui.provider.categoryaware; |
2 | |
3 | |
4 | import java.util.ArrayList; |
5 | import java.util.Collection; |
6 | import java.util.HashMap; |
7 | import java.util.Iterator; |
8 | |
9 | import org.eclipse.emf.common.notify.AdapterFactory; |
10 | import org.eclipse.emf.ecore.EObject; |
11 | import org.eclipse.emf.edit.provider.IDisposable; |
12 | import org.eclipse.emf.edit.provider.IEditingDomainItemProvider; |
13 | import org.eclipse.emf.edit.provider.IItemLabelProvider; |
14 | import org.eclipse.emf.edit.provider.IItemPropertySource; |
15 | import org.eclipse.emf.edit.provider.IItemProviderDecorator; |
16 | import org.eclipse.emf.edit.provider.IStructuredItemContentProvider; |
17 | import org.eclipse.emf.edit.provider.ITreeItemContentProvider; |
18 | import org.eclipse.emf.edit.provider.ItemProviderDecorator; |
19 | |
20 | /** |
21 | * @author Snowball |
22 | * This class implements an IItemProvider which is capable of inserting pseudo- |
23 | * nodes into the tree based view of its items. These pseudo nodes are used to |
24 | * categorise the elements of the root nodes (e.g, Component->{Provided Roles, RequiredRoles, ...}. |
25 | * Categories are configurable using |
26 | * EMF Refelection and CategoryDescriptors. This class uses GenericCategoryItemProvider |
27 | * as child provider to actually render the pseudo nodes. It decorated the item provider |
28 | * of the model object which should be displayed using categories. |
29 | */ |
30 | public class CategoryAwareItemProvider extends ItemProviderDecorator |
31 | implements IEditingDomainItemProvider, IStructuredItemContentProvider, |
32 | ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource, |
33 | IItemProviderDecorator, IDisposable { |
34 | |
35 | /* (non-Javadoc) |
36 | * @see org.eclipse.emf.edit.provider.ItemProviderDecorator#dispose() |
37 | * Remember also to send the dispose() signal to our child item providers for the |
38 | * categories |
39 | */ |
40 | @Override |
41 | public void dispose() { |
42 | for (Iterator<Collection> childCacheIterator = childCache.values().iterator(); childCacheIterator.hasNext(); ) |
43 | { |
44 | for(Iterator<GenericCategoryItemProvider> itemProviderIterator = childCacheIterator.next().iterator(); itemProviderIterator.hasNext();) |
45 | { |
46 | itemProviderIterator.next().dispose(); |
47 | } |
48 | } |
49 | super.dispose(); |
50 | } |
51 | |
52 | private ICategoryDescriptions categories; |
53 | |
54 | private HashMap<EObject, Collection> childCache = new HashMap<EObject, Collection>(); |
55 | |
56 | public CategoryAwareItemProvider(AdapterFactory adapterFactory, |
57 | ICategoryDescriptions categories) { |
58 | super(adapterFactory); |
59 | this.categories = categories; |
60 | } |
61 | |
62 | public CategoryAwareItemProvider( |
63 | CategoryAwareItemProviderAdapterFactory factory, |
64 | ICategoryDescriptions categories) { |
65 | super(factory); |
66 | this.categories = categories; |
67 | } |
68 | |
69 | /* |
70 | * (non-Javadoc) |
71 | * |
72 | * @see org.eclipse.emf.edit.provider.ItemProviderDecorator#getChildren(java.lang.Object) |
73 | * Get children return now pseudo item providers (GenericCategoryItemProvider) which only |
74 | * render the categories and contain themselfs the children of our node - sorted now by |
75 | * categories |
76 | */ |
77 | @Override |
78 | public Collection getChildren(Object object) { |
79 | if (object instanceof EObject) { |
80 | EObject eObject = (EObject) object; |
81 | if (categories.hasCategoriesForObject(eObject)) { |
82 | if (!childCache.containsKey(eObject)) { |
83 | ArrayList children = new ArrayList(); |
84 | Collection<CategoryDescriptor> childDescriptors = categories |
85 | .getCategoriesForObject(eObject); |
86 | for (Iterator<CategoryDescriptor> iterator = childDescriptors |
87 | .iterator(); iterator.hasNext();) { |
88 | children.add(new GenericCategoryItemProvider( |
89 | adapterFactory, iterator.next(), eObject)); |
90 | } |
91 | childCache.put(eObject, children); |
92 | } |
93 | return childCache.get(eObject); |
94 | } |
95 | else |
96 | return super.getChildren(object); |
97 | } else |
98 | return super.getChildren(object); |
99 | } |
100 | } |