KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > model > elements > ElementLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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 package org.eclipse.debug.internal.ui.model.elements;
12
13 import java.util.LinkedList JavaDoc;
14 import java.util.NoSuchElementException JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementLabelProvider;
22 import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate;
23 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.viewers.TreePath;
26 import org.eclipse.swt.graphics.FontData;
27 import org.eclipse.swt.graphics.RGB;
28 import org.eclipse.ui.progress.UIJob;
29
30 /**
31  * Implementation of a context sensitive label provider, which provides
32  * base functionality for subclasses such as label jobs and a basic label updater.
33  *
34  * @since 3.3.0.qualifier
35  */

36 public abstract class ElementLabelProvider implements IElementLabelProvider {
37
38     private Job fLabelJob = null;
39     
40     /**
41      * Describes a label job
42      */

43     interface ILabelJob {
44         /**
45          * Returns whether the updates were queued.
46          *
47          * @param updates updates
48          * @return whether the updates were queued
49          */

50         public boolean queue(ILabelUpdate[] updates);
51     }
52     
53     /**
54      * A <code>Job</code> to update labels. This <code>Job</code> can run
55      * in a non-UI thread.
56      */

57     class LabelJob extends Job implements ILabelJob {
58         
59         private LabelUpdater fUpdater = new LabelUpdater();
60
61         public LabelJob() {
62             super("Label Job"); //$NON-NLS-1$
63
setSystem(true);
64         }
65         
66         /* (non-Javadoc)
67          * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
68          */

69         protected IStatus run(IProgressMonitor monitor) {
70             fUpdater.run();
71             return Status.OK_STATUS;
72         }
73
74         /* (non-Javadoc)
75          * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider.ILabelJob#queue(org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate)
76          */

77         public boolean queue(ILabelUpdate[] updates) {
78             return fUpdater.queue(updates);
79         }
80
81         /* (non-Javadoc)
82          * @see org.eclipse.core.runtime.jobs.Job#shouldRun()
83          */

84         public boolean shouldRun() {
85             return fUpdater.shouldRun();
86         }
87         
88     }
89     
90     /**
91      * A <code>Job</code> to update labels. This <code>Job</code> runs
92      * only in the UI thread.
93      */

94     class UILabelJob extends UIJob implements ILabelJob {
95         
96         private LabelUpdater fUpdater = new LabelUpdater();
97
98         public UILabelJob() {
99             super("Label Job"); //$NON-NLS-1$
100
setSystem(true);
101         }
102         
103         /* (non-Javadoc)
104          * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
105          */

106         public IStatus runInUIThread(IProgressMonitor monitor) {
107             fUpdater.run();
108             return Status.OK_STATUS;
109         }
110         
111         /* (non-Javadoc)
112          * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider.ILabelJob#queue(org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate)
113          */

114         public boolean queue(ILabelUpdate[] updates) {
115             return fUpdater.queue(updates);
116         }
117         
118         /* (non-Javadoc)
119          * @see org.eclipse.core.runtime.jobs.Job#shouldRun()
120          */

121         public boolean shouldRun() {
122             return fUpdater.shouldRun();
123         }
124     }
125     
126     /**
127      * Queue of label updates
128      */

129     class LabelUpdater implements Runnable JavaDoc {
130         
131         LinkedList JavaDoc fQueue = new LinkedList JavaDoc();
132         
133         public synchronized boolean queue(ILabelUpdate[] updates) {
134             if (fQueue == null) {
135                 return false;
136             } else {
137                 for (int i = 0; i < updates.length; i++) {
138                     fQueue.addLast(updates[i]);
139                 }
140                 return true;
141             }
142         }
143
144         /* (non-Javadoc)
145          * @see java.lang.Runnable#run()
146          */

147         public void run() {
148             ILabelUpdate update = getNextUpdate();
149             while (update != null) {
150                 try {
151                     retrieveLabel(update);
152                 } catch (CoreException e) {
153                     update.setStatus(e.getStatus());
154                 }
155                 update.done();
156                 update = getNextUpdate();
157             }
158         }
159         
160         /**
161          * Returns the next update to process, if there is one in the
162          * queue. If there are no queued items <code>null</code> is returned
163          * @return the next queued item or <code>null</code> if the queue is empty.
164          */

165         public synchronized ILabelUpdate getNextUpdate() {
166             if (fQueue == null) {
167                 return null;
168             }
169             ILabelUpdate update = null;
170             try {
171                 update = (ILabelUpdate) fQueue.removeFirst();
172             } catch (NoSuchElementException JavaDoc e) {
173                 fQueue = null;
174             }
175             return update;
176         }
177         
178         public boolean shouldRun() {
179             return fQueue != null;
180         }
181     }
182     
183     /**
184      * Retrieves label attributes for the specified update.
185      *
186      * @param update
187      */

188     protected void retrieveLabel(ILabelUpdate update) throws CoreException {
189         String JavaDoc[] columnIds = update.getColumnIds();
190         IPresentationContext presentationContext = update.getPresentationContext();
191         TreePath elementPath = update.getElementPath();
192         int numColumns = 1;
193         if (columnIds != null) {
194             numColumns = columnIds.length;
195         }
196         for (int i = 0; i < numColumns; i++) {
197             String JavaDoc columnId = null;
198             if (columnIds != null) {
199                 columnId = columnIds[i];
200             }
201             update.setLabel(getLabel(elementPath, presentationContext, columnId), i);
202             update.setImageDescriptor(getImageDescriptor(elementPath, presentationContext, columnId), i);
203             update.setBackground(getBackground(elementPath, presentationContext, columnId), i);
204             update.setForeground(getForeground(elementPath, presentationContext, columnId), i);
205             update.setFontData(getFontData(elementPath, presentationContext, columnId), i);
206         }
207     }
208
209     /**
210      * Returns the <code>FontData</code> for the path in the given column with the current presentation
211      * @param element
212      * @param presentationContext
213      * @param columnId
214      * @return font information or <code>null</code>
215      */

216     protected FontData getFontData(TreePath elementPath, IPresentationContext presentationContext, String JavaDoc columnId) throws CoreException {
217         return null;
218     }
219
220     /**
221      * Returns the <code>RGB</code> foreground colour for the path in the given column with the current presentation
222      * @param element
223      * @param presentationContext
224      * @param columnId
225      * @return color or <code>null</code>
226      */

227     protected RGB getForeground(TreePath elementPath, IPresentationContext presentationContext, String JavaDoc columnId) throws CoreException {
228         return null;
229     }
230
231     /**
232      * Returns the <code>RGB</code> background colour for the path in the given column with the current presentation
233      * @param element
234      * @param presentationContext
235      * @param columnId
236      * @return color or <code>null</code>
237      */

238     protected RGB getBackground(TreePath elementPath, IPresentationContext presentationContext, String JavaDoc columnId) throws CoreException {
239         return null;
240     }
241
242     /**
243      * Returns the <code>ImageDescriptor</code> for the path in the given column with the current presentation
244      * @param element
245      * @param presentationContext
246      * @param columnId
247      * @return image descriptor or <code>null</code>
248      */

249     protected ImageDescriptor getImageDescriptor(TreePath elementPath, IPresentationContext presentationContext, String JavaDoc columnId) throws CoreException {
250         return null;
251     }
252
253     /**
254      * Returns the label for the path in the given column with the current presentation
255      * @param element
256      * @param presentationContext
257      * @param columnId
258      * @return label
259      */

260     protected abstract String JavaDoc getLabel(TreePath elementPath, IPresentationContext presentationContext, String JavaDoc columnId) throws CoreException;
261
262     /* (non-Javadoc)
263      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IElementLabelProvider#update(org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate[])
264      */

265     public synchronized void update(ILabelUpdate[] updates) {
266         if (fLabelJob == null) {
267             fLabelJob = newLabelJob(updates);
268         }
269         if (!((ILabelJob)fLabelJob).queue(updates)) {
270             fLabelJob = newLabelJob(updates);
271             ((ILabelJob)fLabelJob).queue(updates);
272         }
273         // TODO: rule
274
fLabelJob.schedule();
275     }
276     
277     /**
278      * Returns a new <code>Job</code> to update the specified labels. This method
279      * is used to determine if a UI job is needed or not, in the event the request for an update
280      * job has come from a non-UI thread.
281      * @param updates an array of pending label updates
282      * @return a new <code>Job</code> to update labels with.
283      */

284     private Job newLabelJob(ILabelUpdate[] updates) {
285         if (requiresUIJob(updates)) {
286             return new UILabelJob();
287         } else {
288             return new LabelJob();
289         }
290     }
291     
292     /**
293      * Returns whether a UI job should be used for updates versus a non-UI job.
294      * @param updates
295      * @return true if the array of updates requires a UI job to update the labels, false otherwise
296      */

297     protected boolean requiresUIJob(ILabelUpdate[] updates) {
298         return false;
299     }
300     
301 }
302
Popular Tags