Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 648f555ac0f496a54399f172ca8a3b174b4557c0 (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
/*******************************************************************************
 * 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;
import org.eclipse.debug.internal.ui.views.launch.DebugElementHelper;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.progress.UIJob;

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

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.viewers.IAsynchronousLabelAdapter#retrieveLabel(java.lang.Object, org.eclipse.debug.ui.viewers.IPresentationContext, org.eclipse.debug.ui.viewers.ILabelRequestMonitor)
	 */
	@Override
	public void retrieveLabel(final Object element, final IPresentationContext context, final ILabelRequestMonitor result) {
		Job job = null;
		if (requiresUIJob(element)) {
			job = new UIJob("Retrieving labels") { //$NON-NLS-1$
				@Override
				public IStatus runInUIThread(IProgressMonitor monitor) {
					computeLabels(element, context, result);
					return Status.OK_STATUS;
				}
			};
		} else {
			job = new Job("Retrieving labels") { //$NON-NLS-1$
				@Override
				protected IStatus run(IProgressMonitor monitor) {
					computeLabels(element, context, result);
					return Status.OK_STATUS;
				}
			};
		}
		job.setSystem(true);
		job.setRule(getLabelRule(element, context));
		job.schedule();
	}

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

	/**
	 * Returns whether this label adapter requires to be run in the UI thread.
	 * By default, label jobs are not run in the UI thread. Subclasses should
	 * override if required.
	 * @param object the object context
	 *
	 * @return whether this label adapter requires to be run in the UI thread.
	 */
	protected boolean requiresUIJob(Object object) {
		return !DebugElementHelper.requiresUIThread(object);
	}

	/**
	 * Computes label attributes for the given element in the specified context.
	 *
	 * @param element element to compute label for
	 * @param context presentation context
	 * @param monitor monitor to report results to
	 */
	protected void computeLabels(Object element, IPresentationContext context, ILabelRequestMonitor monitor) {
		if (!monitor.isCanceled()) {
			IStatus status = Status.OK_STATUS;
			try {
				monitor.setLabels(getLabels(element, context));
				if (!monitor.isCanceled()) {
					monitor.setImageDescriptors(getImageDescriptors(element, context));
				}
				if (!monitor.isCanceled()) {
					monitor.setFontDatas(getFontDatas(element, context));
				}
				if (!monitor.isCanceled()) {
					monitor.setBackgrounds(getBackgrounds(element, context));
				}
				if (!monitor.isCanceled()) {
					monitor.setForegrounds(getForegrounds(element, context));
				}
			} catch (CoreException e) {
				status = e.getStatus();
			}
			if (!monitor.isCanceled()) {
				monitor.setStatus(status);
				monitor.done();
			}
		}
	}

	/**
	 * Returns a label for the give element in the specified context.
	 *
	 * @param element element to compute label for
	 * @param context presentation context
	 * @return label
	 * @exception CoreException if an exception occurs computing label
	 */
    protected abstract String[] getLabels(Object element, IPresentationContext context) throws CoreException;

    /**
     * Returns an image descriptor for the given element in the specified context
     * or <code>null</code>.
     *
     * @param element element to compute image descriptor for
     * @param context presentation context
     * @return image descriptor or <code>null</code>
     * @throws CoreException if an exception occurs computing image descriptor
     */
    protected abstract ImageDescriptor[] getImageDescriptors(Object element, IPresentationContext context) throws CoreException;

    /**
     * Returns font data for the given element in the specified context or <code>null</code>
     * to use the default font.
     *
     * @param element element to compute font data for
     * @param context presentation context
     * @return font data or <code>null</code>
     * @throws CoreException if an exception occurs computing font data
     */
    protected abstract FontData[] getFontDatas(Object element, IPresentationContext context) throws CoreException;

    /**
     * Returns a foreground color for the given element in the specified context or <code>null</code>
     * to use the default color.
     *
     * @param element element to compute color for
     * @param context presentation context
     * @return color or <code>null</code>
     * @throws CoreException if an exception occurs computing color
     */
    protected abstract RGB[] getForegrounds(Object element, IPresentationContext context) throws CoreException;

    /**
     * Returns a background color for the given element in the specified context or <code>null</code>
     * to use the default color.
     *
     * @param element element to compute color for
     * @param context presentation context
     * @return color or <code>null</code>
     * @throws CoreException if an exception occurs computing color
     */
    protected abstract RGB[] getBackgrounds(Object element, IPresentationContext context) throws CoreException;
}

Back to the top