KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > provisional > AsynchronousContentAdapter


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.debug.internal.ui.viewers.provisional;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.jobs.ISchedulingRule;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.debug.internal.ui.viewers.AsynchronousSchedulingRuleFactory;
21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
22
23 /**
24  * Abstract implementation of an asynchronous content adapter.
25  * <p>
26  * Clients may subclass this class.
27  * </p>
28  * @since 3.2
29  */

30 public abstract class AsynchronousContentAdapter implements IAsynchronousContentAdapter {
31     
32     protected static final Object JavaDoc[] EMPTY = new Object JavaDoc[0];
33     
34     /*
35      * (non-Javadoc)
36      * @see org.eclipse.debug.internal.ui.viewers.provisional.IAsynchronousContentAdapter#retrieveChildren(java.lang.Object, org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext, org.eclipse.debug.internal.ui.viewers.provisional.IChildrenRequestMonitor)
37      */

38     public void retrieveChildren(final Object JavaDoc parent, final IPresentationContext context, final IChildrenRequestMonitor result) {
39         Job job = new Job("Retrieving Children") { //$NON-NLS-1$
40
protected IStatus run(IProgressMonitor monitor) {
41                 if (!monitor.isCanceled()) {
42                     computeChildren(parent, context, result);
43                 }
44                 return Status.OK_STATUS;
45             }
46         };
47         job.setSystem(true);
48         job.setRule(getRetrieveChildrenRule(parent, context));
49         job.schedule();
50     }
51     
52     /**
53      * Returns the scheduling rule for jobs retrieving children.
54      *
55      * @param parent
56      * @param context
57      * @return scheduling rule or <code>null</code>
58      */

59     protected ISchedulingRule getRetrieveChildrenRule(Object JavaDoc parent, IPresentationContext context) {
60         return AsynchronousSchedulingRuleFactory.getDefault().newSerialPerObjectRule(context);
61     }
62     
63
64     /*
65      * (non-Javadoc)
66      * @see org.eclipse.debug.internal.ui.viewers.provisional.IAsynchronousContentAdapter#isContainer(java.lang.Object, org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext, org.eclipse.debug.internal.ui.viewers.provisional.IContainerRequestMonitor)
67      */

68     public void isContainer(final Object JavaDoc element, final IPresentationContext context, final IContainerRequestMonitor result) {
69         Job job = new Job("Computing hasChildren") { //$NON-NLS-1$
70
protected IStatus run(IProgressMonitor monitor) {
71                 if (!monitor.isCanceled()) {
72                     computeIsContainer(element, context, result);
73                 }
74                 return Status.OK_STATUS;
75             }
76         };
77         job.setSystem(true);
78         job.setRule(getIsContainerRule(element, context));
79         job.schedule();
80     }
81     
82     /**
83      * Returns the scheduling rule for jobs determining if an element is a container.
84      *
85      * @param parent
86      * @param context
87      * @return scheduling rule or <code>null</code>
88      */

89     protected ISchedulingRule getIsContainerRule(Object JavaDoc parent, IPresentationContext context) {
90         return AsynchronousSchedulingRuleFactory.getDefault().newSerialPerObjectRule(context);
91     }
92     
93     /**
94      * Computes the children for the given parent in the specified context.
95      *
96      * @param parent parent to retrieve children for
97      * @param context presentation context
98      * @param monitor result to report to
99      */

100     protected void computeChildren(Object JavaDoc parent, IPresentationContext context, IChildrenRequestMonitor monitor) {
101         if (!monitor.isCanceled()) {
102             IStatus status = Status.OK_STATUS;
103             try {
104                 if (supportsContext(context)) {
105                     monitor.addChildren(getChildren(parent, context));
106                 }
107             } catch (CoreException e) {
108                 status = e.getStatus();
109             }
110             monitor.setStatus(status);
111             monitor.done();
112         }
113     }
114     
115     /**
116      * Computes whether the given element is a container.
117      *
118      * @param parent potential parent
119      * @param context presentation context
120      * @param monitor result to report to
121      */

122     protected void computeIsContainer(Object JavaDoc parent, IPresentationContext context, IContainerRequestMonitor monitor) {
123         if (!monitor.isCanceled()) {
124             IStatus status = Status.OK_STATUS;
125             try {
126                 monitor.setIsContainer(hasChildren(parent, context));
127             } catch (CoreException e) {
128                 status = e.getStatus();
129             }
130             monitor.setStatus(status);
131             monitor.done();
132         }
133     }
134         
135     /**
136      * Returns the children for the given parent in the specified context.
137      *
138      * @param parent element to retrieve children for
139      * @param context context children will be presented in
140      * @return children
141      * @throws CoreException if an exception occurs retrieving children
142      */

143     protected abstract Object JavaDoc[] getChildren(Object JavaDoc parent, IPresentationContext context) throws CoreException;
144     
145     /**
146      * Returns whether the given element has children in the specified context.
147      *
148      * @param element element that may have children
149      * @param context context element will be presented in
150      * @return whether the given element has children in the specified context
151      * @throws CoreException if an exception occurs determining whether the
152      * element has children
153      */

154     protected abstract boolean hasChildren(Object JavaDoc element, IPresentationContext context) throws CoreException;
155
156     /**
157      * Returns whether this adapter supports the given context.
158      *
159      * @param context
160      * @return whether this adapter supports the given context
161      */

162     protected boolean supportsContext(IPresentationContext context) {
163         return supportsPartId(context.getId());
164     }
165     
166     /**
167      * Returns whether this adapter provides content in the specified part.
168      *
169      * @param id part id
170      * @return whether this adapter provides content in the specified part
171      */

172     protected abstract boolean supportsPartId(String JavaDoc id);
173 }
174
Popular Tags