Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ba68b99b3318133d7e10e206d66c68f8cda30c80 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*******************************************************************************
 * Copyright (c) 2006, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.model.elements;

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.model.provisional.IChildrenCountUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementContentProvider;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IHasChildrenUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate;

/**
 * @since 3.3
 */
public abstract class ElementContentProvider implements IElementContentProvider {

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

	protected abstract class ElementContentProviderJob extends Job {

		public ElementContentProviderJob(String name) {
			super(name);
			setSystem(true);
			setUser(false);
		}

		@Override
		public boolean belongsTo(Object family) {
			return family == ElementContentProvider.class;
		}

	}

	@Override
	public void update(final IChildrenUpdate[] updates) {
		Job job = new ElementContentProviderJob("Debug children update") { //$NON-NLS-1$
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				for (int i = 0; i < updates.length; i++) {
					IChildrenUpdate update = updates[i];
					if (!update.isCanceled()) {
						retrieveChildren(update);
					}
					update.done();
				}
				return Status.OK_STATUS;
			}
		};
		job.setRule(getRule(updates));
		job.schedule();
	}

	@Override
	public void update(final IChildrenCountUpdate[] updates) {
		Job job = new ElementContentProviderJob("Debug child count update") { //$NON-NLS-1$
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				for (int i = 0; i < updates.length; i++) {
					IChildrenCountUpdate update = updates[i];
					if (!update.isCanceled()) {
						retrieveChildCount(update);
					}
					update.done();
				}
				return Status.OK_STATUS;
			}
		};
		job.setRule(getRule(updates));
		job.schedule();
	}

    /**
     * Computes the children for the given parent in the specified context.
     *
     * @param update update request
     */
    protected void retrieveChildren(IChildrenUpdate update) {
		if (!update.isCanceled()) {
			IStatus status = Status.OK_STATUS;
			try {
				IPresentationContext context = update.getPresentationContext();
				if (supportsContext(context)) {
					int offset = update.getOffset();
					Object[] children = getChildren(update.getElement(), offset, update.getLength(), context, update);
					if (!update.isCanceled() && children != null) {
						for (int i = 0; i < children.length; i++) {
							update.setChild(children[i], offset + i);
						}
					}
				}
			} catch (CoreException e) {
				status = e.getStatus();
			}
			update.setStatus(status);
		}
    }

    /**
     * Computes whether the given element is a container.
     *
     * @param parent potential parent
     * @param context presentation context
     * @param monitor result to report to
     */
    protected void retrieveChildCount(IChildrenCountUpdate update) {
		if (!update.isCanceled()) {
			IStatus status = Status.OK_STATUS;
			try {
				IPresentationContext context = update.getPresentationContext();
				if (supportsContext(context)) {
					int childCount = getChildCount( update.getElement(), context, update);
					if (!update.isCanceled()) {
						update.setChildCount(childCount);
					}
				} else {
					update.setChildCount(0);
				}
			} catch (CoreException e) {
				status = e.getStatus();
			}
			update.setStatus(status);
		}
    }

    /**
     * Returns the children for the given parent at the specified index in the specified context
     * or <code>null</code> if none.
     *
     * @param parent element to retrieve children for
     * @param index child index
     * @param length number of children to retrieve
     * @param context context children will be presented in
     * @return child or <code>null</code>
     * @throws CoreException if an exception occurs retrieving child
     */
    protected abstract Object[] getChildren(Object parent, int index, int length, IPresentationContext context, IViewerUpdate monitor) throws CoreException;

    /**
     * Returns the number of children for the given element.
     *
     * @param elementPath element that may have children
     * @param context context element will be presented in
     * @return number of children
     * @throws CoreException if an exception occurs determining child count
     */
    protected abstract int getChildCount(Object element, IPresentationContext context, IViewerUpdate monitor) throws CoreException;

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

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

    /**
     * Returns the range of elements from <code>index</code> to <code>index + length</code>
     * or <code>null</code> if the index and range is outside the bounds of the original element array.
     *
     * @param elements the original element array
     * @param index the initial index to start copying from
     * @param length the number of elements we want to copy into the returned array
     * @return element or <code>null</code>
     */
    protected Object[] getElements(Object[] elements, int index, int length) {
    	int max = elements.length;
    	if (index < max && ((index + length) > max)) {
    		length = max - index;
    	}
    	if ((index + length) <= elements.length) {
    		Object[] sub = new Object[length];
    		System.arraycopy(elements, index, sub, 0, length);
    		return sub;
    	}
    	return null;
    }

	@Override
	public void update(final IHasChildrenUpdate[] updates) {
		Job job = new ElementContentProviderJob("Debug has children update") { //$NON-NLS-1$
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				for (int i = 0; i < updates.length; i++) {
					IHasChildrenUpdate update = updates[i];
					if (!update.isCanceled()) {
						updateHasChildren(update);
					}
					update.done();
				}
				return Status.OK_STATUS;
			}
		};
		job.setRule(getRule(updates));
		job.schedule();
	}

	/**
	 * Updates whether the given elements have children.
	 *
	 * @param update specifies element and progress monitor
	 */
	protected void updateHasChildren(IHasChildrenUpdate update) {
		if (!update.isCanceled()) {
			IStatus status = Status.OK_STATUS;
			try {
				IPresentationContext context = update.getPresentationContext();
				if (supportsContext(context)) {
					boolean hasChildren = hasChildren(update.getElement(), context, update);
					if (!update.isCanceled()) {
						update.setHasChilren(hasChildren);
					}
				} else {
					update.setHasChilren(false);
				}
			} catch (CoreException e) {
				status = e.getStatus();
			}
			update.setStatus(status);
		}

	}

	/**
	 * Returns whether the given element has children in the specified context.
	 * Subclasses can override to be more efficient.
	 *
	 * @param element
	 * @param context
	 * @param monitor
	 * @return
	 */
	protected boolean hasChildren(Object element, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
		return getChildCount(element, context, monitor) > 0;
	}

	/**
	 * Returns a scheduling rule to use when performing the given updates or
	 * <code>null</code> if none.
	 *
	 * @param updates
	 * @return scheduling rule or <code>null</code> if none
	 */
	protected ISchedulingRule getRule(IChildrenCountUpdate[] updates) {
		return null;
	}

	/**
	 * Returns a scheduling rule to use when performing the given updates or
	 * <code>null</code> if none.
	 *
	 * @param updates
	 * @return scheduling rule or <code>null</code> if none
	 */
	protected ISchedulingRule getRule(IChildrenUpdate[] updates) {
		return null;
	}

	/**
	 * Returns a scheduling rule to use when performing the given updates or
	 * <code>null</code> if none.
	 *
	 * @param updates
	 * @return scheduling rule or <code>null</code> if none
	 */
	protected ISchedulingRule getRule(IHasChildrenUpdate[] updates) {
		return null;
	}

}

Back to the top