KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > ResourceToItemsMapper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.search.internal.ui;
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.graphics.Image;
21 import org.eclipse.swt.widgets.Item;
22
23 import org.eclipse.jface.viewers.ContentViewer;
24 import org.eclipse.jface.viewers.ILabelProvider;
25
26 import org.eclipse.search.ui.ISearchResultViewEntry;
27
28 /**
29  * Helper class for updating error markers and other decorators that work on resources.
30  * Items are mapped to their element's underlying resource.
31  * Method <code>resourceChanged</code> updates all items that are affected from the changed
32  * elements.
33  * @deprecated old search
34  */

35 class ResourceToItemsMapper {
36
37     private static final int NUMBER_LIST_REUSE= 10;
38
39     // map from resource to item
40
private HashMap JavaDoc fResourceToItem;
41     private Stack JavaDoc fReuseLists;
42     
43     private ContentViewer fContentViewer;
44
45     public ResourceToItemsMapper(ContentViewer viewer) {
46         fResourceToItem= new HashMap JavaDoc();
47         fReuseLists= new Stack JavaDoc();
48         
49         fContentViewer= viewer;
50     }
51
52     /**
53      * Must be called from the UI thread.
54      */

55     public void resourceChanged(IResource changedResource) {
56         Object JavaDoc obj= fResourceToItem.get(changedResource);
57         if (obj == null) {
58             // not mapped
59
} else if (obj instanceof Item) {
60             updateItem((Item) obj);
61         } else { // List of Items
62
List JavaDoc list= (List JavaDoc) obj;
63             for (int k= 0; k < list.size(); k++) {
64                 updateItem((Item) list.get(k));
65             }
66         }
67     }
68         
69     private void updateItem(Item item) {
70         if (!item.isDisposed()) { // defensive code
71
ILabelProvider lprovider= (ILabelProvider) fContentViewer.getLabelProvider();
72             
73             Object JavaDoc data= item.getData();
74
75             String JavaDoc oldText= item.getText();
76             String JavaDoc text= lprovider.getText(data);
77             if (text != null && !text.equals(oldText)) {
78                 item.setText(text);
79             }
80
81             Image oldImage= item.getImage();
82             Image image= lprovider.getImage(data);
83             if (image != null && !image.equals(oldImage)) {
84                 item.setImage(image);
85             }
86         }
87     }
88
89     /**
90      * Adds a new item to the map.
91      * @param element Element to map
92      * @param item The item used for the element
93      */

94     public void addToMap(Object JavaDoc element, Item item) {
95         IResource resource= ((ISearchResultViewEntry)element).getResource();
96         if (resource != null) {
97             Object JavaDoc existingMapping= fResourceToItem.get(resource);
98             if (existingMapping == null) {
99                 fResourceToItem.put(resource, item);
100             } else if (existingMapping instanceof Item) {
101                 if (existingMapping != item) {
102                     List JavaDoc list= getNewList();
103                     list.add(existingMapping);
104                     list.add(item);
105                     fResourceToItem.put(resource, list);
106                 }
107             } else { // List
108
List JavaDoc list= (List JavaDoc) existingMapping;
109                 if (!list.contains(item)) {
110                     list.add(item);
111                 }
112             }
113         }
114     }
115
116     /**
117      * Removes an element from the map.
118      */

119     public void removeFromMap(Object JavaDoc element, Item item) {
120         IResource resource= ((ISearchResultViewEntry)element).getResource();
121         if (resource != null) {
122             Object JavaDoc existingMapping= fResourceToItem.get(resource);
123             if (existingMapping == null) {
124                 return;
125             } else if (existingMapping instanceof Item) {
126                 fResourceToItem.remove(resource);
127             } else { // List
128
List JavaDoc list= (List JavaDoc) existingMapping;
129                 list.remove(item);
130                 if (list.isEmpty()) {
131                     fResourceToItem.remove(list);
132                     releaseList(list);
133                 }
134             }
135         }
136     }
137     
138     private List JavaDoc getNewList() {
139         if (!fReuseLists.isEmpty()) {
140             return (List JavaDoc) fReuseLists.pop();
141         }
142         return new ArrayList JavaDoc(2);
143     }
144     
145     private void releaseList(List JavaDoc list) {
146         if (fReuseLists.size() < NUMBER_LIST_REUSE) {
147             fReuseLists.push(list);
148         }
149     }
150     
151     /**
152      * Clears the map.
153      */

154     public void clearMap() {
155         fResourceToItem.clear();
156     }
157     
158     /**
159      * Clears the map.
160      */

161     public boolean isEmpty() {
162         return fResourceToItem.isEmpty();
163     }
164 }
165
Popular Tags