KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > model > elements > ElementContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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 package org.eclipse.debug.internal.ui.model.elements;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.jobs.ISchedulingRule;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenCountUpdate;
20 import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate;
21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementContentProvider;
22 import org.eclipse.debug.internal.ui.viewers.model.provisional.IHasChildrenUpdate;
23 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
24 import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate;
25
26 /**
27  * @since 3.3
28  */

29 public abstract class ElementContentProvider implements IElementContentProvider {
30     
31     protected static final Object JavaDoc[] EMPTY = new Object JavaDoc[0];
32
33     /* (non-Javadoc)
34      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IElementContentProvider#updateChildren(java.lang.Object, int, int, org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext, org.eclipse.debug.internal.ui.viewers.model.provisional.IElementRequestMonitor)
35      */

36     public void update(final IChildrenUpdate[] updates) {
37         Job job = new Job("children update") { //$NON-NLS-1$
38
protected IStatus run(IProgressMonitor monitor) {
39                 for (int i = 0; i < updates.length; i++) {
40                     IChildrenUpdate update = updates[i];
41                     if (!update.isCanceled()) {
42                         retrieveChildren(update);
43                     }
44                     update.done();
45                 }
46                 return Status.OK_STATUS;
47             }
48         };
49         job.setSystem(true);
50         job.setRule(getRule(updates));
51         job.schedule();
52     }
53
54     /* (non-Javadoc)
55      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IElementContentProvider#update(org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenCountUpdate)
56      */

57     public void update(final IChildrenCountUpdate[] updates) {
58         Job job = new Job("child count update") { //$NON-NLS-1$
59
protected IStatus run(IProgressMonitor monitor) {
60                 for (int i = 0; i < updates.length; i++) {
61                     IChildrenCountUpdate update = updates[i];
62                     if (!update.isCanceled()) {
63                         retrieveChildCount(update);
64                     }
65                     update.done();
66                 }
67                 return Status.OK_STATUS;
68             }
69         };
70         job.setSystem(true);
71         job.setRule(getRule(updates));
72         job.schedule();
73     }
74         
75     /**
76      * Computes the children for the given parent in the specified context.
77      *
78      * @param update update request
79      */

80     protected void retrieveChildren(IChildrenUpdate update) {
81         if (!update.isCanceled()) {
82             IStatus status = Status.OK_STATUS;
83             try {
84                 IPresentationContext context = update.getPresentationContext();
85                 if (supportsContext(context)) {
86                     int offset = update.getOffset();
87                     Object JavaDoc[] children = getChildren(update.getElement(), offset, update.getLength(), context, update);
88                     if (!update.isCanceled() && children != null) {
89                         for (int i = 0; i < children.length; i++) {
90                             update.setChild(children[i], offset + i);
91                         }
92                     }
93                 }
94             } catch (CoreException e) {
95                 status = e.getStatus();
96             }
97             update.setStatus(status);
98         }
99     }
100     
101     /**
102      * Computes whether the given element is a container.
103      *
104      * @param parent potential parent
105      * @param context presentation context
106      * @param monitor result to report to
107      */

108     protected void retrieveChildCount(IChildrenCountUpdate update) {
109         if (!update.isCanceled()) {
110             IStatus status = Status.OK_STATUS;
111             try {
112                 IPresentationContext context = update.getPresentationContext();
113                 if (supportsContext(context)) {
114                     int childCount = getChildCount( update.getElement(), context, update);
115                     if (!update.isCanceled()) {
116                         update.setChildCount(childCount);
117                     }
118                 } else {
119                     update.setChildCount(0);
120                 }
121             } catch (CoreException e) {
122                 status = e.getStatus();
123             }
124             update.setStatus(status);
125         }
126     }
127         
128     /**
129      * Returns the children for the given parent at the specified index in the specified context
130      * or <code>null</code> if none.
131      *
132      * @param parent element to retrieve children for
133      * @param index child index
134      * @param length number of children to retrieve
135      * @param context context children will be presented in
136      * @return child or <code>null</code>
137      * @throws CoreException if an exception occurs retrieving child
138      */

139     protected abstract Object JavaDoc[] getChildren(Object JavaDoc parent, int index, int length, IPresentationContext context, IViewerUpdate monitor) throws CoreException;
140     
141     /**
142      * Returns the number of children for the given element.
143      *
144      * @param elementPath element that may have children
145      * @param context context element will be presented in
146      * @return number of children
147      * @throws CoreException if an exception occurs determining child count
148      */

149     protected abstract int getChildCount(Object JavaDoc element, IPresentationContext context, IViewerUpdate monitor) throws CoreException;
150
151     /**
152      * Returns whether this adapter supports the given context.
153      *
154      * @param context
155      * @return whether this adapter supports the given context
156      */

157     protected boolean supportsContext(IPresentationContext context) {
158         return supportsContextId(context.getId());
159     }
160     
161     /**
162      * Returns whether this adapter provides content in the specified context id.
163      *
164      * @param id part id
165      * @return whether this adapter provides content in the specified context id
166      */

167     protected abstract boolean supportsContextId(String JavaDoc id);
168
169     /**
170      * Returns the range of elements from <code>index</code> to <code>index + length</code>
171      * or <code>null</code> if the index and range is outside the bounds of the original element array.
172      *
173      * @param elements the original element array
174      * @param index the initial index to start copying from
175      * @param length the number of elements we want to copy into the returned array
176      * @return element or <code>null</code>
177      */

178     protected Object JavaDoc[] getElements(Object JavaDoc[] elements, int index, int length) {
179         int max = elements.length;
180         if (index < max && ((index + length) > max)) {
181             length = max - index;
182         }
183         if ((index + length) <= elements.length) {
184             Object JavaDoc[] sub = new Object JavaDoc[length];
185             System.arraycopy(elements, index, sub, 0, length);
186             return sub;
187         }
188         return null;
189     }
190
191     /* (non-Javadoc)
192      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IElementContentProvider#update(org.eclipse.debug.internal.ui.viewers.model.provisional.IHasChildrenUpdate[])
193      */

194     public void update(final IHasChildrenUpdate[] updates) {
195         Job job = new Job("has children update") { //$NON-NLS-1$
196
protected IStatus run(IProgressMonitor monitor) {
197                 for (int i = 0; i < updates.length; i++) {
198                     IHasChildrenUpdate update = updates[i];
199                     if (!update.isCanceled()) {
200                         updateHasChildren(update);
201                     }
202                     update.done();
203                 }
204                 return Status.OK_STATUS;
205             }
206         };
207         job.setSystem(true);
208         job.setRule(getRule(updates));
209         job.schedule();
210     }
211
212     /**
213      * Updates whether the given elements have children.
214      *
215      * @param update specifies element and progress monitor
216      */

217     protected void updateHasChildren(IHasChildrenUpdate update) {
218         if (!update.isCanceled()) {
219             IStatus status = Status.OK_STATUS;
220             try {
221                 IPresentationContext context = update.getPresentationContext();
222                 if (supportsContext(context)) {
223                     boolean hasChildren = hasChildren(update.getElement(), context, update);
224                     if (!update.isCanceled()) {
225                         update.setHasChilren(hasChildren);
226                     }
227                 } else {
228                     update.setHasChilren(false);
229                 }
230             } catch (CoreException e) {
231                 status = e.getStatus();
232             }
233             update.setStatus(status);
234         }
235         
236     }
237
238     /**
239      * Returns whether the given element has children in the specified context.
240      * Subclasses can override to be more efficient.
241      *
242      * @param element
243      * @param context
244      * @param monitor
245      * @return
246      */

247     protected boolean hasChildren(Object JavaDoc element, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
248         return getChildCount(element, context, monitor) > 0;
249     }
250     
251     /**
252      * Returns a scheduling rule to use when performing the given updates or
253      * <code>null</code> if none.
254      *
255      * @param updates
256      * @return scheduling rule or <code>null</code> if none
257      */

258     protected ISchedulingRule getRule(IChildrenCountUpdate[] updates) {
259         return null;
260     }
261     
262     /**
263      * Returns a scheduling rule to use when performing the given updates or
264      * <code>null</code> if none.
265      *
266      * @param updates
267      * @return scheduling rule or <code>null</code> if none
268      */

269     protected ISchedulingRule getRule(IChildrenUpdate[] updates) {
270         return null;
271     }
272     
273     /**
274      * Returns a scheduling rule to use when performing the given updates or
275      * <code>null</code> if none.
276      *
277      * @param updates
278      * @return scheduling rule or <code>null</code> if none
279      */

280     protected ISchedulingRule getRule(IHasChildrenUpdate[] updates) {
281         return null;
282     }
283     
284 }
285
Popular Tags