001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.views.bookmarkexplorer;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.core.resources.IMarker;
016: import org.eclipse.core.resources.IMarkerDelta;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.resources.IResourceChangeEvent;
019: import org.eclipse.core.resources.IResourceChangeListener;
020: import org.eclipse.core.resources.IResourceDelta;
021: import org.eclipse.core.resources.IWorkspace;
022: import org.eclipse.core.runtime.CoreException;
023: import org.eclipse.jface.viewers.IBasicPropertyConstants;
024: import org.eclipse.jface.viewers.IStructuredContentProvider;
025: import org.eclipse.jface.viewers.Viewer;
026: import org.eclipse.swt.widgets.Control;
027:
028: /**
029: * Provides content for the bookmark navigator
030: */
031: class BookmarkContentProvider implements IStructuredContentProvider,
032: IResourceChangeListener, IBasicPropertyConstants {
033:
034: private IResource input;
035:
036: private Viewer viewer;
037:
038: /**
039: * The constructor.
040: */
041: public BookmarkContentProvider(BookmarkNavigator bookmarksView) {
042: super ();
043: }
044:
045: /**
046: * The visual part that is using this content provider is about
047: * to be disposed. Deallocate all allocated SWT resources.
048: */
049: public void dispose() {
050: IResource resource = (IResource) viewer.getInput();
051: if (resource != null) {
052: resource.getWorkspace().removeResourceChangeListener(this );
053: }
054: }
055:
056: /**
057: * Returns all the bookmarks that should be shown for
058: * the current settings.
059: */
060: Object[] getBookmarks(IResource resource) {
061: try {
062: return resource.findMarkers(IMarker.BOOKMARK, true,
063: IResource.DEPTH_INFINITE);
064: } catch (CoreException e) {
065: return new Object[0];
066: }
067: }
068:
069: public Object[] getChildren(Object element) {
070: // If the input element is a workbench return a list
071: // of the existing bookmarks. Otherwise, return an empty list.
072: if (element instanceof IResource) {
073: return getBookmarks((IResource) element);
074: } else {
075: return new Object[0];
076: }
077: }
078:
079: public Object[] getElements(Object element) {
080: return getChildren(element);
081: }
082:
083: /**
084: * Recursively walks over the resource delta and gathers all marker deltas. Marker
085: * deltas are placed into one of the three given vectors depending on
086: * the type of delta (add, remove, or change).
087: */
088: void getMarkerDeltas(IResourceDelta delta, List additions,
089: List removals, List changes) {
090: IMarkerDelta[] markerDeltas = delta.getMarkerDeltas();
091: for (int i = 0; i < markerDeltas.length; i++) {
092: IMarkerDelta markerDelta = markerDeltas[i];
093: IMarker marker = markerDelta.getMarker();
094: switch (markerDelta.getKind()) {
095: case IResourceDelta.ADDED:
096: if (markerDelta.isSubtypeOf(IMarker.BOOKMARK)) {
097: additions.add(marker);
098: }
099: break;
100: case IResourceDelta.REMOVED:
101: if (markerDelta.isSubtypeOf(IMarker.BOOKMARK)) {
102: removals.add(marker);
103: }
104: break;
105: case IResourceDelta.CHANGED:
106: if (markerDelta.isSubtypeOf(IMarker.BOOKMARK)) {
107: changes.add(marker);
108: }
109: break;
110: }
111: }
112:
113: //recurse on child deltas
114: IResourceDelta[] children = delta.getAffectedChildren();
115: for (int i = 0; i < children.length; i++) {
116: getMarkerDeltas(children[i], additions, removals, changes);
117: }
118: }
119:
120: /* (non-Javadoc)
121: * Method declared on ITreeContentProvider,
122: */
123: public Object getParent(Object element) {
124: return input;
125: }
126:
127: /**
128: * hasChildren method comment.
129: */
130: public boolean hasChildren(Object element) {
131: if (element instanceof IWorkspace) {
132: return true;
133: } else {
134: return false;
135: }
136: }
137:
138: public void inputChanged(Viewer newViewer, Object oldInput,
139: Object newInput) {
140: if (oldInput == null) {
141: IResource resource = (IResource) newInput;
142: resource.getWorkspace().addResourceChangeListener(this );
143: }
144: this .viewer = newViewer;
145: this .input = (IResource) newInput;
146: }
147:
148: /**
149: * The workbench has changed. Process the delta and provide updates to the viewer,
150: * inside the UI thread.
151: *
152: * @see IResourceChangeListener#resourceChanged
153: */
154: public void resourceChanged(final IResourceChangeEvent event) {
155:
156: // gather all marker changes from the delta.
157: // be sure to do this in the calling thread,
158: // as the delta is destroyed when this method returns
159: final List additions = new ArrayList();
160: final List removals = new ArrayList();
161: final List changes = new ArrayList();
162:
163: IResourceDelta delta = event.getDelta();
164: if (delta == null) {
165: return;
166: }
167: getMarkerDeltas(delta, additions, removals, changes);
168:
169: // update the viewer based on the marker changes, in the UI thread
170: if (additions.size() + removals.size() + changes.size() > 0) {
171: viewer.getControl().getDisplay().asyncExec(new Runnable() {
172: public void run() {
173: // This method runs inside an asyncExec. The widget may have been destroyed
174: // by the time this is run. Check for this and do nothing if so.
175: Control ctrl = viewer.getControl();
176: if (ctrl == null || ctrl.isDisposed()) {
177: return;
178: }
179:
180: viewer.refresh();
181: }
182: });
183: }
184: }
185: }
|