1 11 12 package org.eclipse.jdt.internal.ui.viewsupport; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.core.resources.IResource; 18 19 import org.eclipse.swt.events.DisposeEvent; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Item; 22 import org.eclipse.swt.widgets.Tree; 23 import org.eclipse.swt.widgets.Widget; 24 25 import org.eclipse.jface.viewers.IBaseLabelProvider; 26 import org.eclipse.jface.viewers.LabelProviderChangedEvent; 27 import org.eclipse.jface.viewers.TreeViewer; 28 import org.eclipse.jface.viewers.ViewerFilter; 29 30 import org.eclipse.jdt.core.IMember; 31 32 import org.eclipse.jdt.ui.IWorkingCopyProvider; 33 import org.eclipse.jdt.ui.ProblemsLabelDecorator.ProblemsLabelChangedEvent; 34 35 36 41 public class ProblemTreeViewer extends TreeViewer implements ResourceToItemsMapper.IContentViewerAccessor { 42 43 protected ResourceToItemsMapper fResourceToItemsMapper; 44 45 48 public ProblemTreeViewer(Composite parent) { 49 super(parent); 50 initMapper(); 51 } 52 53 56 public ProblemTreeViewer(Composite parent, int style) { 57 super(parent, style); 58 initMapper(); 59 } 60 61 64 public ProblemTreeViewer(Tree tree) { 65 super(tree); 66 initMapper(); 67 } 68 69 72 public void doUpdateItem(Widget item) { 73 doUpdateItem(item, item.getData(), true); 74 } 75 76 private void initMapper() { 77 fResourceToItemsMapper= new ResourceToItemsMapper(this); 78 } 79 80 81 84 protected void mapElement(Object element, Widget item) { 85 super.mapElement(element, item); 86 if (item instanceof Item) { 87 fResourceToItemsMapper.addToMap(element, (Item) item); 88 } 89 } 90 91 94 protected void unmapElement(Object element, Widget item) { 95 if (item instanceof Item) { 96 fResourceToItemsMapper.removeFromMap(element, (Item) item); 97 } 98 super.unmapElement(element, item); 99 } 100 101 104 protected void unmapAllElements() { 105 fResourceToItemsMapper.clearMap(); 106 super.unmapAllElements(); 107 } 108 109 110 112 115 public void addFilter(ViewerFilter filter) { 116 if (filter instanceof JavaViewerFilter) { 117 ((JavaViewerFilter) filter).filteringStart(); 118 } 119 super.addFilter(filter); 120 } 121 122 125 public void removeFilter(ViewerFilter filter) { 126 super.removeFilter(filter); 127 if (filter instanceof JavaViewerFilter) { 128 ((JavaViewerFilter) filter).filteringEnd(); 129 } 130 } 131 132 135 public void setFilters(ViewerFilter[] filters) { 136 ViewerFilter[] oldFilters= getFilters(); 137 for (int i= 0; i < filters.length; i++) { 138 ViewerFilter curr= filters[i]; 139 if (curr instanceof JavaViewerFilter && !findAndRemove(oldFilters, curr)) { 140 ((JavaViewerFilter) curr).filteringStart(); 141 } 142 } 143 endFilterSessions(oldFilters); 144 super.setFilters(filters); 145 } 146 147 150 public void resetFilters() { 151 endFilterSessions(getFilters()); 152 super.resetFilters(); 153 } 154 155 private boolean findAndRemove(ViewerFilter[] filters, ViewerFilter filter) { 156 for (int i= 0; i < filters.length; i++) { 157 if (filters[i] == filter) { 158 filters[i]= null; 159 return true; 160 } 161 } 162 return false; 163 } 164 165 private void endFilterSessions(ViewerFilter[] filters) { 166 for (int i= 0; i < filters.length; i++) { 167 ViewerFilter curr= filters[i]; 168 if (curr instanceof JavaViewerFilter) { 169 ((JavaViewerFilter) curr).filteringEnd(); 170 } 171 } 172 } 173 174 177 protected void handleDispose(DisposeEvent event) { 178 endFilterSessions(getFilters()); 179 super.handleDispose(event); 180 } 181 182 183 186 protected void handleLabelProviderChanged(LabelProviderChangedEvent event) { 187 if (event instanceof ProblemsLabelChangedEvent) { 188 ProblemsLabelChangedEvent e= (ProblemsLabelChangedEvent) event; 189 if (!e.isMarkerChange() && canIgnoreChangesFromAnnotionModel()) { 190 return; 191 } 192 } 193 Object [] changed= addAditionalProblemParents(event.getElements()); 194 195 if (changed != null && !fResourceToItemsMapper.isEmpty()) { 196 ArrayList others= new ArrayList (); 197 for (int i= 0; i < changed.length; i++) { 198 Object curr= changed[i]; 199 if (curr instanceof IResource) { 200 fResourceToItemsMapper.resourceChanged((IResource) curr); 201 } else { 202 others.add(curr); 203 } 204 } 205 if (others.isEmpty()) { 206 return; 207 } 208 event= new LabelProviderChangedEvent((IBaseLabelProvider) event.getSource(), others.toArray()); 209 } else { 210 if (event.getElements() != changed) 212 event= new LabelProviderChangedEvent((IBaseLabelProvider) event.getSource(), changed); 213 } 214 super.handleLabelProviderChanged(event); 215 } 216 217 222 private boolean canIgnoreChangesFromAnnotionModel() { 223 Object contentProvider= getContentProvider(); 224 return contentProvider instanceof IWorkingCopyProvider && !((IWorkingCopyProvider)contentProvider).providesWorkingCopies(); 225 } 226 227 228 234 protected boolean evaluateExpandableWithFilters(Object parent) { 235 return parent instanceof IMember; 236 } 237 238 241 public boolean isExpandable(Object parent) { 242 if (hasFilters() && evaluateExpandableWithFilters(parent)) { 243 return hasFilteredChildren(parent); 245 } 246 return super.isExpandable(parent); 247 } 248 249 protected final boolean hasFilteredChildren(Object parent) { 250 Object [] rawChildren= getRawChildren(parent); 251 return containsNonFiltered(rawChildren, parent); 252 } 253 254 257 protected final Object [] getFilteredChildren(Object parent) { 258 return filter(getRawChildren(parent), parent); 259 } 260 261 private Object [] filter(Object [] elements, Object parent) { 262 if (!hasFilters() || elements.length == 0) { 263 return elements; 264 } 265 List list= new ArrayList (elements.length); 266 ViewerFilter[] filters = getFilters(); 267 for (int i = 0; i < elements.length; i++) { 268 Object object = elements[i]; 269 if (!isFiltered(object, parent, filters)) { 270 list.add(object); 271 } 272 } 273 return list.toArray(); 274 } 275 276 private boolean containsNonFiltered(Object [] elements, Object parent) { 277 if (elements.length == 0) { 278 return false; 279 } 280 if (!hasFilters()) { 281 return true; 282 } 283 ViewerFilter[] filters = getFilters(); 284 for (int i = 0; i < elements.length; i++) { 285 Object object = elements[i]; 286 if (!isFiltered(object, parent, filters)) { 287 return true; 288 } 289 } 290 return false; 291 } 292 293 302 protected boolean isFiltered(Object object, Object parent, ViewerFilter[] filters) { 303 for (int i = 0; i < filters.length; i++) { 304 ViewerFilter filter = filters[i]; 305 if (!filter.select(this, parent, object)) 306 return true; 307 } 308 return false; 309 } 310 311 314 protected final Object [] filter(Object [] elements) { 315 return filter(elements, getRoot()); 316 } 317 318 protected Object [] addAditionalProblemParents(Object [] elements) { 319 return elements; 320 } 321 322 328 public boolean isFiltered(Object object, Object parent) { 329 return isFiltered(object, parent, getFilters()); 330 } 331 } 332 333 | Popular Tags |