Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0bf4635ebb7bfdd4556dbc3a26701902f4bc3d86 (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
/*******************************************************************************
 *  Copyright (c) 2008, 2010 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.equinox.internal.p2.ui.viewers;

import java.util.ArrayList;
import java.util.Arrays;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.equinox.internal.p2.ui.ProvUIMessages;
import org.eclipse.equinox.internal.p2.ui.model.ProvElement;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.progress.*;

/**
 * Content provider that retrieves children of a ProvElement.
 * 
 * @since 3.5
 * 
 */
public class ProvElementContentProvider implements ITreeContentProvider {

	private boolean fetchInBackground = false;
	Viewer viewer;
	private Job fetchJob;
	// family is used by test cases
	Object fetchFamily = new Object();

	/**
	 * 
	 */
	public ProvElementContentProvider() {
		// Default constructor
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
	 */
	public Object[] getElements(final Object input) {
		// Simple deferred fetch handling for table viewers
		if (fetchInBackground && input instanceof IDeferredWorkbenchAdapter && viewer instanceof AbstractTableViewer) {
			final Display display = viewer.getControl().getDisplay();
			final Object pending = new PendingUpdateAdapter();
			if (fetchJob != null)
				fetchJob.cancel();
			fetchJob = new Job(ProvUIMessages.ProvElementContentProvider_FetchJobTitle) {
				protected IStatus run(final IProgressMonitor monitor) {
					IDeferredWorkbenchAdapter parent = (IDeferredWorkbenchAdapter) input;
					final ArrayList<Object> children = new ArrayList<Object>();
					parent.fetchDeferredChildren(parent, new IElementCollector() {
						public void add(Object element, IProgressMonitor mon) {
							if (mon.isCanceled())
								return;
							children.add(element);
						}

						public void add(Object[] elements, IProgressMonitor mon) {
							if (mon.isCanceled())
								return;
							children.addAll(Arrays.asList(elements));
						}

						public void done() {
							// nothing special to do
						}

					}, monitor);
					if (!monitor.isCanceled()) {
						display.asyncExec(new Runnable() {
							public void run() {
								AbstractTableViewer tableViewer = (AbstractTableViewer) viewer;
								if (monitor.isCanceled() || tableViewer == null || tableViewer.getControl().isDisposed())
									return;
								tableViewer.getControl().setRedraw(false);
								tableViewer.remove(pending);
								tableViewer.add(children.toArray());
								finishedFetchingElements(input);
								tableViewer.getControl().setRedraw(true);
							}
						});
					}
					return Status.OK_STATUS;
				}

				public boolean belongsTo(Object family) {
					return family == fetchFamily;
				}

			};
			fetchJob.schedule();
			return new Object[] {pending};
		}
		Object[] elements = getChildren(input);
		finishedFetchingElements(input);
		return elements;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
	 */
	public Object getParent(Object child) {
		if (child instanceof ProvElement) {
			return ((ProvElement) child).getParent(child);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
	 */
	public boolean hasChildren(Object element) {
		if (element instanceof ProvElement)
			return ((ProvElement) element).hasChildren(element);
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
	 */
	public Object[] getChildren(final Object parent) {
		if (parent instanceof ProvElement) {
			return ((ProvElement) parent).getChildren(parent);
		}
		return new Object[0];
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
	 */
	public void dispose() {
		viewer = null;
		if (fetchJob != null) {
			fetchJob.cancel();
			fetchJob = null;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	public void inputChanged(Viewer aViewer, Object oldInput, Object newInput) {
		this.viewer = aViewer;
		if (fetchJob != null) {
			fetchJob.cancel();
			fetchJob = null;
		}
	}

	protected void finishedFetchingElements(Object parent) {
		// do nothing
	}

	public void setFetchInBackground(boolean fetchInBackground) {
		this.fetchInBackground = fetchInBackground;
	}
}

Back to the top