KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > bookmarkexplorer > BookmarkContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.views.bookmarkexplorer;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
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 /**
31  * Provides content for the bookmark navigator
32  */

33 class BookmarkContentProvider implements IStructuredContentProvider,
34         IResourceChangeListener, IBasicPropertyConstants {
35
36     private IResource input;
37
38     private Viewer viewer;
39
40     /**
41      * The constructor.
42      */

43     public BookmarkContentProvider(BookmarkNavigator bookmarksView) {
44         super();
45     }
46
47     /**
48      * The visual part that is using this content provider is about
49      * to be disposed. Deallocate all allocated SWT resources.
50      */

51     public void dispose() {
52         IResource resource = (IResource) viewer.getInput();
53         if (resource != null) {
54             resource.getWorkspace().removeResourceChangeListener(this);
55         }
56     }
57
58     /**
59      * Returns all the bookmarks that should be shown for
60      * the current settings.
61      */

62     Object JavaDoc[] getBookmarks(IResource resource) {
63         try {
64             return resource.findMarkers(IMarker.BOOKMARK, true,
65                     IResource.DEPTH_INFINITE);
66         } catch (CoreException e) {
67             return new Object JavaDoc[0];
68         }
69     }
70
71     public Object JavaDoc[] getChildren(Object JavaDoc element) {
72         // If the input element is a workbench return a list
73
// of the existing bookmarks. Otherwise, return an empty list.
74
if (element instanceof IResource) {
75             return getBookmarks((IResource) element);
76         } else {
77             return new Object JavaDoc[0];
78         }
79     }
80
81     public Object JavaDoc[] getElements(Object JavaDoc element) {
82         return getChildren(element);
83     }
84
85     /**
86      * Recursively walks over the resource delta and gathers all marker deltas. Marker
87      * deltas are placed into one of the three given vectors depending on
88      * the type of delta (add, remove, or change).
89      */

90     void getMarkerDeltas(IResourceDelta delta, List JavaDoc additions, List JavaDoc removals,
91             List JavaDoc 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         //recurse on child deltas
116
IResourceDelta[] children = delta.getAffectedChildren();
117         for (int i = 0; i < children.length; i++) {
118             getMarkerDeltas(children[i], additions, removals, changes);
119         }
120     }
121
122     /* (non-Javadoc)
123      * Method declared on ITreeContentProvider,
124      */

125     public Object JavaDoc getParent(Object JavaDoc element) {
126         return input;
127     }
128
129     /**
130      * hasChildren method comment.
131      */

132     public boolean hasChildren(Object JavaDoc element) {
133         if (element instanceof IWorkspace) {
134             return true;
135         } else {
136             return false;
137         }
138     }
139
140     public void inputChanged(Viewer newViewer, Object JavaDoc oldInput, Object JavaDoc 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     /**
150      * The workbench has changed. Process the delta and provide updates to the viewer,
151      * inside the UI thread.
152      *
153      * @see IResourceChangeListener#resourceChanged
154      */

155     public void resourceChanged(final IResourceChangeEvent event) {
156
157         // gather all marker changes from the delta.
158
// be sure to do this in the calling thread,
159
// as the delta is destroyed when this method returns
160
final List JavaDoc additions = new ArrayList JavaDoc();
161         final List JavaDoc removals = new ArrayList JavaDoc();
162         final List JavaDoc changes = new ArrayList JavaDoc();
163
164         IResourceDelta delta = event.getDelta();
165         if (delta == null) {
166             return;
167         }
168         getMarkerDeltas(delta, additions, removals, changes);
169
170         // update the viewer based on the marker changes, in the UI thread
171
if (additions.size() + removals.size() + changes.size() > 0) {
172             viewer.getControl().getDisplay().asyncExec(new Runnable JavaDoc() {
173                 public void run() {
174                     // This method runs inside an asyncExec. The widget may have been destroyed
175
// by the time this is run. Check for this and do nothing if so.
176
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