KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > ResourceToItemsMapper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.viewsupport;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Stack JavaDoc;
17
18 import org.eclipse.core.resources.IResource;
19
20 import org.eclipse.swt.widgets.Item;
21 import org.eclipse.swt.widgets.Widget;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IJavaElement;
25
26 /**
27  * Helper class for updating error markers and other decorators that work on resources.
28  * Items are mapped to their element's underlying resource.
29  * Method <code>resourceChanged</code> updates all items that are affected from the changed
30  * elements.
31  */

32 public class ResourceToItemsMapper {
33     
34     public static interface IContentViewerAccessor {
35         public void doUpdateItem(Widget item);
36     }
37     
38
39     private static final int NUMBER_LIST_REUSE= 10;
40
41     // map from resource to item
42
private HashMap JavaDoc fResourceToItem;
43     private Stack JavaDoc fReuseLists;
44     
45     private IContentViewerAccessor fContentViewerAccess;
46
47     public ResourceToItemsMapper(IContentViewerAccessor viewer) {
48         fResourceToItem= new HashMap JavaDoc();
49         fReuseLists= new Stack JavaDoc();
50         
51         fContentViewerAccess= viewer;
52     }
53
54     /**
55      * Must be called from the UI thread.
56      * @param changedResource Changed resource
57      */

58     public void resourceChanged(IResource changedResource) {
59         Object JavaDoc obj= fResourceToItem.get(changedResource);
60         if (obj == null) {
61             // not mapped
62
} else if (obj instanceof Item) {
63             updateItem((Item) obj);
64         } else { // List of Items
65
List JavaDoc list= (List JavaDoc) obj;
66             for (int k= 0; k < list.size(); k++) {
67                 updateItem((Item) list.get(k));
68             }
69         }
70     }
71         
72     private void updateItem(Item item) {
73         if (!item.isDisposed()) {
74             fContentViewerAccess.doUpdateItem(item);
75         }
76     }
77
78     /**
79      * Adds a new item to the map.
80      * @param element Element to map
81      * @param item The item used for the element
82      */

83     public void addToMap(Object JavaDoc element, Item item) {
84         IResource resource= getCorrespondingResource(element);
85         if (resource != null) {
86             Object JavaDoc existingMapping= fResourceToItem.get(resource);
87             if (existingMapping == null) {
88                 fResourceToItem.put(resource, item);
89             } else if (existingMapping instanceof Item) {
90                 if (existingMapping != item) {
91                     List JavaDoc list= getNewList();
92                     list.add(existingMapping);
93                     list.add(item);
94                     fResourceToItem.put(resource, list);
95                 }
96             } else { // List
97
List JavaDoc list= (List JavaDoc) existingMapping;
98                 if (!list.contains(item)) {
99                     list.add(item);
100                 }
101             }
102         }
103     }
104
105     /**
106      * Removes an element from the map.
107      * @param element The data element
108      * @param item The table or tree item
109      */

110     public void removeFromMap(Object JavaDoc element, Item item) {
111         IResource resource= getCorrespondingResource(element);
112         if (resource != null) {
113             Object JavaDoc existingMapping= fResourceToItem.get(resource);
114             if (existingMapping == null) {
115                 return;
116             } else if (existingMapping instanceof Item) {
117                 fResourceToItem.remove(resource);
118             } else { // List
119
List JavaDoc list= (List JavaDoc) existingMapping;
120                 list.remove(item);
121                 if (list.isEmpty()) {
122                     fResourceToItem.remove(list);
123                     releaseList(list);
124                 }
125             }
126         }
127     }
128     
129     private List JavaDoc getNewList() {
130         if (!fReuseLists.isEmpty()) {
131             return (List JavaDoc) fReuseLists.pop();
132         }
133         return new ArrayList JavaDoc(2);
134     }
135     
136     private void releaseList(List JavaDoc list) {
137         if (fReuseLists.size() < NUMBER_LIST_REUSE) {
138             fReuseLists.push(list);
139         }
140     }
141     
142     /**
143      * Clears the map.
144      */

145     public void clearMap() {
146         fResourceToItem.clear();
147     }
148     
149     /**
150      * Tests if the map is empty
151      * @return Returns if there are mappings
152      */

153     public boolean isEmpty() {
154         return fResourceToItem.isEmpty();
155     }
156     
157     /**
158      * Method that decides which elements can have error markers
159      * Returns null if an element can not have error markers.
160      * @param element The input element
161      * @return Returns the corresponding resource or null
162      */

163     private static IResource getCorrespondingResource(Object JavaDoc element) {
164         if (element instanceof IJavaElement) {
165             IJavaElement elem= (IJavaElement) element;
166             IResource res= elem.getResource();
167             if (res == null) {
168                 ICompilationUnit cu= (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
169                 if (cu != null) {
170                     // elements in compilation units are mapped to the underlying resource of the original cu
171
res= cu.getResource();
172                 }
173             }
174             return res;
175         } else if (element instanceof IResource) {
176             return (IResource) element;
177         }
178         return null;
179     }
180     
181 }
182
Popular Tags