1 11 12 package org.eclipse.ui.views.bookmarkexplorer; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.core.resources.IMarker; 18 import org.eclipse.core.resources.IMarkerDelta; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.IResourceChangeEvent; 21 import org.eclipse.core.resources.IResourceChangeListener; 22 import org.eclipse.core.resources.IResourceDelta; 23 import org.eclipse.core.resources.IWorkspace; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.jface.viewers.IBasicPropertyConstants; 26 import org.eclipse.jface.viewers.IStructuredContentProvider; 27 import org.eclipse.jface.viewers.Viewer; 28 import org.eclipse.swt.widgets.Control; 29 30 33 class BookmarkContentProvider implements IStructuredContentProvider, 34 IResourceChangeListener, IBasicPropertyConstants { 35 36 private IResource input; 37 38 private Viewer viewer; 39 40 43 public BookmarkContentProvider(BookmarkNavigator bookmarksView) { 44 super(); 45 } 46 47 51 public void dispose() { 52 IResource resource = (IResource) viewer.getInput(); 53 if (resource != null) { 54 resource.getWorkspace().removeResourceChangeListener(this); 55 } 56 } 57 58 62 Object [] getBookmarks(IResource resource) { 63 try { 64 return resource.findMarkers(IMarker.BOOKMARK, true, 65 IResource.DEPTH_INFINITE); 66 } catch (CoreException e) { 67 return new Object [0]; 68 } 69 } 70 71 public Object [] getChildren(Object element) { 72 if (element instanceof IResource) { 75 return getBookmarks((IResource) element); 76 } else { 77 return new Object [0]; 78 } 79 } 80 81 public Object [] getElements(Object element) { 82 return getChildren(element); 83 } 84 85 90 void getMarkerDeltas(IResourceDelta delta, List additions, List removals, 91 List changes) { 92 IMarkerDelta[] markerDeltas = delta.getMarkerDeltas(); 93 for (int i = 0; i < markerDeltas.length; i++) { 94 IMarkerDelta markerDelta = markerDeltas[i]; 95 IMarker marker = markerDelta.getMarker(); 96 switch (markerDelta.getKind()) { 97 case IResourceDelta.ADDED: 98 if (markerDelta.isSubtypeOf(IMarker.BOOKMARK)) { 99 additions.add(marker); 100 } 101 break; 102 case IResourceDelta.REMOVED: 103 if (markerDelta.isSubtypeOf(IMarker.BOOKMARK)) { 104 removals.add(marker); 105 } 106 break; 107 case IResourceDelta.CHANGED: 108 if (markerDelta.isSubtypeOf(IMarker.BOOKMARK)) { 109 changes.add(marker); 110 } 111 break; 112 } 113 } 114 115 IResourceDelta[] children = delta.getAffectedChildren(); 117 for (int i = 0; i < children.length; i++) { 118 getMarkerDeltas(children[i], additions, removals, changes); 119 } 120 } 121 122 125 public Object getParent(Object element) { 126 return input; 127 } 128 129 132 public boolean hasChildren(Object element) { 133 if (element instanceof IWorkspace) { 134 return true; 135 } else { 136 return false; 137 } 138 } 139 140 public void inputChanged(Viewer newViewer, Object oldInput, Object newInput) { 141 if (oldInput == null) { 142 IResource resource = (IResource) newInput; 143 resource.getWorkspace().addResourceChangeListener(this); 144 } 145 this.viewer = newViewer; 146 this.input = (IResource) newInput; 147 } 148 149 155 public void resourceChanged(final IResourceChangeEvent event) { 156 157 final List additions = new ArrayList (); 161 final List removals = new ArrayList (); 162 final List changes = new ArrayList (); 163 164 IResourceDelta delta = event.getDelta(); 165 if (delta == null) { 166 return; 167 } 168 getMarkerDeltas(delta, additions, removals, changes); 169 170 if (additions.size() + removals.size() + changes.size() > 0) { 172 viewer.getControl().getDisplay().asyncExec(new Runnable () { 173 public void run() { 174 Control ctrl = viewer.getControl(); 177 if (ctrl == null || ctrl.isDisposed()) { 178 return; 179 } 180 181 viewer.refresh(); 182 } 183 }); 184 } 185 } 186 } 187 | Popular Tags |