Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fd4ca97a220273b6c6fd09ed01c963007cf6cff8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*******************************************************************************
 * Copyright (c) 2005, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.debug.internal.ui.viewers.provisional;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.internal.ui.viewers.AsynchronousSchedulingRuleFactory;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;

/**
 * Abstract implementation of an asynchronous content adapter.
 * <p>
 * Clients may subclass this class.
 * </p>
 * @since 3.2
 */
public abstract class AsynchronousContentAdapter implements IAsynchronousContentAdapter {

	protected static final Object[] EMPTY = new Object[0];

    @Override
	public void retrieveChildren(final Object parent, final IPresentationContext context, final IChildrenRequestMonitor result) {
		Job job = new Job("Retrieving Children") { //$NON-NLS-1$
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				if (!monitor.isCanceled()) {
					computeChildren(parent, context, result);
				}
				return Status.OK_STATUS;
			}
		};
		job.setSystem(true);
		job.setRule(getRetrieveChildrenRule(parent, context));
		job.schedule();
	}

    /**
     * Returns the scheduling rule for jobs retrieving children.
     *
     * @param parent the parent
     * @param context the presentation context
     * @return scheduling rule or <code>null</code>
     */
    protected ISchedulingRule getRetrieveChildrenRule(Object parent, IPresentationContext context) {
    	return AsynchronousSchedulingRuleFactory.getDefault().newSerialPerObjectRule(context);
    }


    @Override
	public void isContainer(final Object element, final IPresentationContext context, final IContainerRequestMonitor result) {
		Job job = new Job("Computing hasChildren") { //$NON-NLS-1$
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				if (!monitor.isCanceled()) {
					computeIsContainer(element, context, result);
				}
				return Status.OK_STATUS;
			}
		};
		job.setSystem(true);
		job.setRule(getIsContainerRule(element, context));
		job.schedule();
	}

    /**
     * Returns the scheduling rule for jobs determining if an element is a container.
     *
     * @param parent the parent
     * @param context the presentation context
     * @return scheduling rule or <code>null</code>
     */
    protected ISchedulingRule getIsContainerRule(Object parent, IPresentationContext context) {
    	return AsynchronousSchedulingRuleFactory.getDefault().newSerialPerObjectRule(context);
    }

    /**
     * Computes the children for the given parent in the specified context.
     *
     * @param parent parent to retrieve children for
     * @param context presentation context
     * @param monitor result to report to
     */
    protected void computeChildren(Object parent, IPresentationContext context, IChildrenRequestMonitor monitor) {
		if (!monitor.isCanceled()) {
			IStatus status = Status.OK_STATUS;
			try {
				if (supportsContext(context)) {
					monitor.addChildren(getChildren(parent, context));
				}
			} catch (CoreException e) {
				status = e.getStatus();
			}
			monitor.setStatus(status);
			monitor.done();
		}
    }

    /**
     * Computes whether the given element is a container.
     *
     * @param parent potential parent
     * @param context presentation context
     * @param monitor result to report to
     */
    protected void computeIsContainer(Object parent, IPresentationContext context, IContainerRequestMonitor monitor) {
		if (!monitor.isCanceled()) {
			IStatus status = Status.OK_STATUS;
			try {
				monitor.setIsContainer(hasChildren(parent, context));
			} catch (CoreException e) {
				status = e.getStatus();
			}
			monitor.setStatus(status);
			monitor.done();
		}
    }

    /**
     * Returns the children for the given parent in the specified context.
     *
     * @param parent element to retrieve children for
     * @param context context children will be presented in
     * @return children
     * @throws CoreException if an exception occurs retrieving children
     */
    protected abstract Object[] getChildren(Object parent, IPresentationContext context) throws CoreException;

    /**
     * Returns whether the given element has children in the specified context.
     *
     * @param element element that may have children
     * @param context context element will be presented in
     * @return whether the given element has children in the specified context
     * @throws CoreException if an exception occurs determining whether the
     *  element has children
     */
    protected abstract boolean hasChildren(Object element, IPresentationContext context) throws CoreException;

    /**
     * Returns whether this adapter supports the given context.
     *
     * @param context the presentation context
     * @return whether this adapter supports the given context
     */
    protected boolean supportsContext(IPresentationContext context) {
		return supportsPartId(context.getId());
    }

    /**
     * Returns whether this adapter provides content in the specified part.
     *
     * @param id part id
     * @return whether this adapter provides content in the specified part
     */
    protected abstract boolean supportsPartId(String id);
}

Back to the top