KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > provisional > AsynchronousLabelAdapter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.debug.internal.ui.viewers.provisional;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.jobs.ISchedulingRule;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.debug.internal.ui.viewers.AsynchronousSchedulingRuleFactory;
21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
22 import org.eclipse.debug.internal.ui.views.launch.DebugElementHelper;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.swt.graphics.FontData;
25 import org.eclipse.swt.graphics.RGB;
26 import org.eclipse.ui.progress.UIJob;
27
28 /**
29  * Abstract implementation of an asynchronous label adapter
30  * <p>
31  * Clients may subclass this class.
32  * </p>
33  * @since 3.2
34  */

35 public abstract class AsynchronousLabelAdapter implements IAsynchronousLabelAdapter {
36     
37     /* (non-Javadoc)
38      * @see org.eclipse.debug.ui.viewers.IAsynchronousLabelAdapter#retrieveLabel(java.lang.Object, org.eclipse.debug.ui.viewers.IPresentationContext, org.eclipse.debug.ui.viewers.ILabelRequestMonitor)
39      */

40     public void retrieveLabel(final Object JavaDoc element, final IPresentationContext context, final ILabelRequestMonitor result) {
41         Job job = null;
42         if (requiresUIJob(element)) {
43             job = new UIJob("Retrieving labels") { //$NON-NLS-1$
44
public IStatus runInUIThread(IProgressMonitor monitor) {
45                     computeLabels(element, context, result);
46                     return Status.OK_STATUS;
47                 }
48             };
49         } else {
50             job = new Job("Retrieving labels") { //$NON-NLS-1$
51
protected IStatus run(IProgressMonitor monitor) {
52                     computeLabels(element, context, result);
53                     return Status.OK_STATUS;
54                 }
55             };
56         }
57         job.setSystem(true);
58         job.setRule(getLabelRule(element, context));
59         job.schedule();
60     }
61     
62     /**
63      * Returns the scheduling rule for label jobs.
64      *
65      * @param element
66      * @param context
67      * @return scheduling rule or <code>null</code>
68      */

69     protected ISchedulingRule getLabelRule(Object JavaDoc element, IPresentationContext context) {
70         return AsynchronousSchedulingRuleFactory.getDefault().newSerialPerObjectRule(context);
71     }
72     
73     /**
74      * Returns whether this label adapter requires to be run in the UI thread.
75      * By default, label jobs are not run in the UI thread. Subclasses should
76      * override if required.
77      *
78      * @return whether this label adapter requires to be run in the UI thread.
79      */

80     protected boolean requiresUIJob(Object JavaDoc object) {
81         return !DebugElementHelper.isInitialized(object);
82     }
83     
84     /**
85      * Computes label attributes for the given element in the specified context.
86      *
87      * @param element element to compute label for
88      * @param context presentation context
89      * @param result monitor to report results to
90      */

91     protected void computeLabels(Object JavaDoc element, IPresentationContext context, ILabelRequestMonitor monitor) {
92         if (!monitor.isCanceled()) {
93             IStatus status = Status.OK_STATUS;
94             try {
95                 monitor.setLabels(getLabels(element, context));
96                 if (!monitor.isCanceled()) {
97                     monitor.setImageDescriptors(getImageDescriptors(element, context));
98                 }
99                 if (!monitor.isCanceled()) {
100                     monitor.setFontDatas(getFontDatas(element, context));
101                 }
102                 if (!monitor.isCanceled()) {
103                     monitor.setBackgrounds(getBackgrounds(element, context));
104                 }
105                 if (!monitor.isCanceled()) {
106                     monitor.setForegrounds(getForegrounds(element, context));
107                 }
108             } catch (CoreException e) {
109                 status = e.getStatus();
110             }
111             if (!monitor.isCanceled()) {
112                 monitor.setStatus(status);
113                 monitor.done();
114             }
115         }
116     }
117     
118     /**
119      * Returns a label for the give element in the specified context.
120      *
121      * @param element element to compute label for
122      * @param context presentation context
123      * @return label
124      * @exception CoreException if an exception occurs computing label
125      */

126     protected abstract String JavaDoc[] getLabels(Object JavaDoc element, IPresentationContext context) throws CoreException;
127     
128     /**
129      * Returns an image descriptor for the given element in the specified context
130      * or <code>null</code>.
131      *
132      * @param element element to compute image descriptor for
133      * @param context presentation context
134      * @return image descriptor or <code>null</code>
135      * @throws CoreException if an exception occurs computing image descriptor
136      */

137     protected abstract ImageDescriptor[] getImageDescriptors(Object JavaDoc element, IPresentationContext context) throws CoreException;
138     
139     /**
140      * Returns font data for the given element in the specified context or <code>null</code>
141      * to use the default font.
142      *
143      * @param element element to compute font data for
144      * @param context presentation context
145      * @return font data or <code>null</code>
146      * @throws CoreException if an exception occurs computing font data
147      */

148     protected abstract FontData[] getFontDatas(Object JavaDoc element, IPresentationContext context) throws CoreException;
149     
150     /**
151      * Returns a foreground color for the given element in the specified context or <code>null</code>
152      * to use the default color.
153      *
154      * @param element element to compute color for
155      * @param context presentation context
156      * @return color or <code>null</code>
157      * @throws CoreException if an exception occurs computing color
158      */

159     protected abstract RGB[] getForegrounds(Object JavaDoc element, IPresentationContext context) throws CoreException;
160     
161     /**
162      * Returns a background color for the given element in the specified context or <code>null</code>
163      * to use the default color.
164      *
165      * @param element element to compute color for
166      * @param context presentation context
167      * @return color or <code>null</code>
168      * @throws CoreException if an exception occurs computing color
169      */

170     protected abstract RGB[] getBackgrounds(Object JavaDoc element, IPresentationContext context) throws CoreException;
171 }
172
Popular Tags