1 11 package org.eclipse.search.internal.ui; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Stack ; 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 35 class ResourceToItemsMapper { 36 37 private static final int NUMBER_LIST_REUSE= 10; 38 39 private HashMap fResourceToItem; 41 private Stack fReuseLists; 42 43 private ContentViewer fContentViewer; 44 45 public ResourceToItemsMapper(ContentViewer viewer) { 46 fResourceToItem= new HashMap (); 47 fReuseLists= new Stack (); 48 49 fContentViewer= viewer; 50 } 51 52 55 public void resourceChanged(IResource changedResource) { 56 Object obj= fResourceToItem.get(changedResource); 57 if (obj == null) { 58 } else if (obj instanceof Item) { 60 updateItem((Item) obj); 61 } else { List list= (List ) 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()) { ILabelProvider lprovider= (ILabelProvider) fContentViewer.getLabelProvider(); 72 73 Object data= item.getData(); 74 75 String oldText= item.getText(); 76 String 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 94 public void addToMap(Object element, Item item) { 95 IResource resource= ((ISearchResultViewEntry)element).getResource(); 96 if (resource != null) { 97 Object 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 list= getNewList(); 103 list.add(existingMapping); 104 list.add(item); 105 fResourceToItem.put(resource, list); 106 } 107 } else { List list= (List ) existingMapping; 109 if (!list.contains(item)) { 110 list.add(item); 111 } 112 } 113 } 114 } 115 116 119 public void removeFromMap(Object element, Item item) { 120 IResource resource= ((ISearchResultViewEntry)element).getResource(); 121 if (resource != null) { 122 Object existingMapping= fResourceToItem.get(resource); 123 if (existingMapping == null) { 124 return; 125 } else if (existingMapping instanceof Item) { 126 fResourceToItem.remove(resource); 127 } else { List list= (List ) existingMapping; 129 list.remove(item); 130 if (list.isEmpty()) { 131 fResourceToItem.remove(list); 132 releaseList(list); 133 } 134 } 135 } 136 } 137 138 private List getNewList() { 139 if (!fReuseLists.isEmpty()) { 140 return (List ) fReuseLists.pop(); 141 } 142 return new ArrayList (2); 143 } 144 145 private void releaseList(List list) { 146 if (fReuseLists.size() < NUMBER_LIST_REUSE) { 147 fReuseLists.push(list); 148 } 149 } 150 151 154 public void clearMap() { 155 fResourceToItem.clear(); 156 } 157 158 161 public boolean isEmpty() { 162 return fResourceToItem.isEmpty(); 163 } 164 } 165 | Popular Tags |