1 11 package org.eclipse.jdt.internal.ui.viewsupport; 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.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 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 private HashMap fResourceToItem; 43 private Stack fReuseLists; 44 45 private IContentViewerAccessor fContentViewerAccess; 46 47 public ResourceToItemsMapper(IContentViewerAccessor viewer) { 48 fResourceToItem= new HashMap (); 49 fReuseLists= new Stack (); 50 51 fContentViewerAccess= viewer; 52 } 53 54 58 public void resourceChanged(IResource changedResource) { 59 Object obj= fResourceToItem.get(changedResource); 60 if (obj == null) { 61 } else if (obj instanceof Item) { 63 updateItem((Item) obj); 64 } else { List list= (List ) 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 83 public void addToMap(Object element, Item item) { 84 IResource resource= getCorrespondingResource(element); 85 if (resource != null) { 86 Object 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 list= getNewList(); 92 list.add(existingMapping); 93 list.add(item); 94 fResourceToItem.put(resource, list); 95 } 96 } else { List list= (List ) existingMapping; 98 if (!list.contains(item)) { 99 list.add(item); 100 } 101 } 102 } 103 } 104 105 110 public void removeFromMap(Object element, Item item) { 111 IResource resource= getCorrespondingResource(element); 112 if (resource != null) { 113 Object existingMapping= fResourceToItem.get(resource); 114 if (existingMapping == null) { 115 return; 116 } else if (existingMapping instanceof Item) { 117 fResourceToItem.remove(resource); 118 } else { List list= (List ) existingMapping; 120 list.remove(item); 121 if (list.isEmpty()) { 122 fResourceToItem.remove(list); 123 releaseList(list); 124 } 125 } 126 } 127 } 128 129 private List getNewList() { 130 if (!fReuseLists.isEmpty()) { 131 return (List ) fReuseLists.pop(); 132 } 133 return new ArrayList (2); 134 } 135 136 private void releaseList(List list) { 137 if (fReuseLists.size() < NUMBER_LIST_REUSE) { 138 fReuseLists.push(list); 139 } 140 } 141 142 145 public void clearMap() { 146 fResourceToItem.clear(); 147 } 148 149 153 public boolean isEmpty() { 154 return fResourceToItem.isEmpty(); 155 } 156 157 163 private static IResource getCorrespondingResource(Object 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 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 |