Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fefac87711638e06ea5b33aecbbf24267cfe58df (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*******************************************************************************
 * Copyright (c) 2006, 2014 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
 *     Patrick Chuong (Texas Instruments) - added support for checkbox (Bug 286310)
 *     Patrick Chuong (Texas Instruments) - bug fix 306768
 *******************************************************************************/
package org.eclipse.debug.internal.ui.model.elements;

import java.util.LinkedList;

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.ICheckUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementLabelProvider;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.progress.UIJob;

/**
 * Implementation of a context sensitive label provider, which provides
 * base functionality for subclasses such as label jobs and a basic label updater.
 *
 * @since 3.3.0.qualifier
 */
public abstract class ElementLabelProvider implements IElementLabelProvider {

	private Job fLabelJob = null;

	/**
	 * Describes a label job
	 */
	interface ILabelJob {
		/**
		 * Returns whether the updates were queued.
		 *
		 * @param updates updates
		 * @return whether the updates were queued
		 */
		boolean queue(ILabelUpdate[] updates);
	}

	/**
	 * A <code>Job</code> to update labels. This <code>Job</code> can run
	 * in a non-UI thread.
	 */
	class LabelJob extends Job implements ILabelJob {

		private LabelUpdater fUpdater = new LabelUpdater();

		public LabelJob() {
			super("Label Job"); //$NON-NLS-1$
			setSystem(true);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
		 */
		@Override
		protected IStatus run(IProgressMonitor monitor) {
			fUpdater.run();
			return Status.OK_STATUS;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider.ILabelJob#queue(org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate)
		 */
		@Override
		public boolean queue(ILabelUpdate[] updates) {
			return fUpdater.queue(updates);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.core.runtime.jobs.Job#shouldRun()
		 */
		@Override
		public boolean shouldRun() {
			return fUpdater.shouldRun();
		}

	}

	/**
	 * A <code>Job</code> to update labels. This <code>Job</code> runs
	 * only in the UI thread.
	 */
	class UILabelJob extends UIJob implements ILabelJob {

		private LabelUpdater fUpdater = new LabelUpdater();

		public UILabelJob() {
			super("Label Job"); //$NON-NLS-1$
			setSystem(true);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
		 */
		@Override
		public IStatus runInUIThread(IProgressMonitor monitor) {
			fUpdater.run();
			return Status.OK_STATUS;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider.ILabelJob#queue(org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate)
		 */
		@Override
		public boolean queue(ILabelUpdate[] updates) {
			return fUpdater.queue(updates);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.core.runtime.jobs.Job#shouldRun()
		 */
		@Override
		public boolean shouldRun() {
			return fUpdater.shouldRun();
		}
	}

	/**
	 * Queue of label updates
	 */
	class LabelUpdater implements Runnable {
		LinkedList<ILabelUpdate> fQueue = new LinkedList<>();

		public synchronized boolean queue(ILabelUpdate[] updates) {
			if (fQueue == null) {
				return false;
			} else {
				for (int i = 0; i < updates.length; i++) {
					fQueue.addLast(updates[i]);
				}
				return true;
			}
		}

		/* (non-Javadoc)
		 * @see java.lang.Runnable#run()
		 */
		@Override
		public void run() {
			ILabelUpdate update = getNextUpdate();
			while (update != null) {
				ISchedulingRule rule = getRule(update);
				if (!update.isCanceled()) {
					try {
						if (rule != null) {
							Job.getJobManager().beginRule(rule, null);
						}
						retrieveLabel(update);
					} catch (CoreException e) {
						update.setStatus(e.getStatus());
					} finally {
						if (rule != null) {
							Job.getJobManager().endRule(rule);
						}
					}
				}
				update.done();
				update = getNextUpdate();
			}
		}

		/**
		 * Returns the next update to process, if there is one in the
		 * queue. If there are no queued items <code>null</code> is returned
		 * @return the next queued item or <code>null</code> if the queue is empty.
		 */
		public synchronized ILabelUpdate getNextUpdate() {
			if (fQueue == null) {
				return null;
			}
			if (fQueue.isEmpty()) {
				fQueue = null;
				return null;
			}
			return fQueue.removeFirst();
		}

		public boolean shouldRun() {
			return fQueue != null;
		}
	}

	/**
	 * Retrieves label attributes for the specified update.
	 *
	 * @param update
	 */
	protected void retrieveLabel(ILabelUpdate update) throws CoreException {
		String[] columnIds = update.getColumnIds();
		IPresentationContext presentationContext = update.getPresentationContext();
		TreePath elementPath = update.getElementPath();
		int numColumns = 1;
		if (columnIds != null) {
			numColumns = columnIds.length;
		}
		for (int i = 0; i < numColumns; i++) {
			String columnId = null;
			if (columnIds != null) {
				columnId = columnIds[i];
			}
			update.setLabel(getLabel(elementPath, presentationContext, columnId, i), i);
			update.setImageDescriptor(getImageDescriptor(elementPath, presentationContext, columnId, i), i);
			update.setBackground(getBackground(elementPath, presentationContext, columnId), i);
			update.setForeground(getForeground(elementPath, presentationContext, columnId), i);
			update.setFontData(getFontData(elementPath, presentationContext, columnId), i);
			if (update instanceof ICheckUpdate &&
			    Boolean.TRUE.equals(presentationContext.getProperty(ICheckUpdate.PROP_CHECK)))
			{
				((ICheckUpdate) update).setChecked(
				    getChecked(elementPath, presentationContext), getGrayed(elementPath, presentationContext));
			}
		}
	}

	/**
	 * Returns the <code>FontData</code> for the path in the given column with the current presentation
	 * @param elementPath
	 * @param presentationContext
	 * @param columnId
	 * @return font information or <code>null</code>
	 * @throws CoreException
	 */
	protected FontData getFontData(TreePath elementPath, IPresentationContext presentationContext, String columnId) throws CoreException {
		return null;
	}

	/**
	 * Returns the <code>RGB</code> foreground colour for the path in the given column with the current presentation
	 * @param elementPath
	 * @param presentationContext
	 * @param columnId
	 * @return color or <code>null</code>
	 * @throws CoreException
	 */
	protected RGB getForeground(TreePath elementPath, IPresentationContext presentationContext, String columnId) throws CoreException {
		return null;
	}

	/**
	 * Returns the <code>RGB</code> background colour for the path in the given column with the current presentation
	 * @param elementPath
	 * @param presentationContext
	 * @param columnId
	 * @return color or <code>null</code>
	 * @throws CoreException
	 */
	protected RGB getBackground(TreePath elementPath, IPresentationContext presentationContext, String columnId) throws CoreException {
		return null;
	}

	/**
	 * Returns the <code>ImageDescriptor</code> for the path in the given column with the current presentation
	 * @param elementPath
	 * @param presentationContext
	 * @param columnId
	 * @return image descriptor or <code>null</code>
	 * @throws CoreException
	 */
	protected ImageDescriptor getImageDescriptor(TreePath elementPath, IPresentationContext presentationContext, String columnId) throws CoreException {
		return null;
	}

	/**
	 * Returns the <code>ImageDescriptor</code> for the path in the given column with the current presentation
	 * @param elementPath
	 * @param presentationContext
	 * @param columnId
	 * @param columnIndex
	 * @return image descriptor or <code>null</code>
	 * @throws CoreException
	 *
	 * @since 3.6
	 */
	protected ImageDescriptor getImageDescriptor(TreePath elementPath, IPresentationContext presentationContext, String columnId, int columnIndex) throws CoreException {
		return getImageDescriptor(elementPath, presentationContext, columnId);
	}

	/**
	 * Returns the label for the path in the given column with the current presentation
	 * @param element
	 * @param presentationContext
	 * @param columnId
	 * @return label
	 */
	protected abstract String getLabel(TreePath elementPath, IPresentationContext presentationContext, String columnId) throws CoreException;

	/**
	 * Returns the label for the path in the given column with the current presentation.
	 * @param elementPath
	 * @param presentationContext
	 * @param columnId
	 * @param columnIndex
	 * @return label
	 *
	 * @since 3.6
	 */
	protected String getLabel(TreePath elementPath, IPresentationContext presentationContext, String columnId, int columnIndex) throws CoreException {
		return getLabel(elementPath, presentationContext, columnId);
	}

	/**
	 * Returns the checked state for the given path.
	 *
     * @param path Path of the element to retrieve the grayed state for.
     * @param presentationContext Presentation context where the element is
     * displayed.
     * @return <code>true<code> if the element check box should be checked
     * @throws CoreException
	 *
	 * @since 3.6
	 */
	protected boolean getChecked(TreePath path, IPresentationContext presentationContext) throws CoreException {
		return false;
	}

	/**
	 * Returns the grayed state for the given path.
	 *
	 * @param path Path of the element to retrieve the grayed state for.
     * @param presentationContext Presentation context where the element is
     * displayed.
	 * @return <code>true<code> if the element check box should be grayed
	 * @throws CoreException
     *
     * @since 3.6
	 */
	protected boolean getGrayed(TreePath path, IPresentationContext presentationContext) throws CoreException {
		return false;
	}

    /* (non-Javadoc)
     * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IElementLabelProvider#update(org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate[])
     */
    @Override
	public synchronized void update(ILabelUpdate[] updates) {
		if (fLabelJob == null) {
			fLabelJob = newLabelJob(updates);
		}
		if (!((ILabelJob)fLabelJob).queue(updates)) {
			fLabelJob = newLabelJob(updates);
			((ILabelJob)fLabelJob).queue(updates);
		}
		// TODO: rule
		fLabelJob.schedule();
	}

    /**
     * Returns a new <code>Job</code> to update the specified labels. This method
     * is used to determine if a UI job is needed or not, in the event the request for an update
     * job has come from a non-UI thread.
     * @param updates an array of pending label updates
     * @return a new <code>Job</code> to update labels with.
     */
    private Job newLabelJob(ILabelUpdate[] updates) {
    	if (requiresUIJob(updates)) {
			return new UILabelJob();
		} else {
			return new LabelJob();
		}
    }

    /**
     * Returns whether a UI job should be used for updates versus a non-UI job.
     * @param updates
     * @return true if the array of updates requires a UI job to update the labels, false otherwise
     */
    protected boolean requiresUIJob(ILabelUpdate[] updates) {
    	return false;
    }

    /**
     * Returns the scheduling rule for the given update or <code>null</code>
     * it none.
     *
     * @param update label update
     * @return associated scheduling rule, or <code>null</code>
     */
    protected ISchedulingRule getRule(ILabelUpdate update) {
    	return null;
    }

}

Back to the top