1 11 package org.eclipse.debug.internal.ui.model.elements; 12 13 import java.util.LinkedList ; 14 import java.util.NoSuchElementException ; 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 36 public abstract class ElementLabelProvider implements IElementLabelProvider { 37 38 private Job fLabelJob = null; 39 40 43 interface ILabelJob { 44 50 public boolean queue(ILabelUpdate[] updates); 51 } 52 53 57 class LabelJob extends Job implements ILabelJob { 58 59 private LabelUpdater fUpdater = new LabelUpdater(); 60 61 public LabelJob() { 62 super("Label Job"); setSystem(true); 64 } 65 66 69 protected IStatus run(IProgressMonitor monitor) { 70 fUpdater.run(); 71 return Status.OK_STATUS; 72 } 73 74 77 public boolean queue(ILabelUpdate[] updates) { 78 return fUpdater.queue(updates); 79 } 80 81 84 public boolean shouldRun() { 85 return fUpdater.shouldRun(); 86 } 87 88 } 89 90 94 class UILabelJob extends UIJob implements ILabelJob { 95 96 private LabelUpdater fUpdater = new LabelUpdater(); 97 98 public UILabelJob() { 99 super("Label Job"); setSystem(true); 101 } 102 103 106 public IStatus runInUIThread(IProgressMonitor monitor) { 107 fUpdater.run(); 108 return Status.OK_STATUS; 109 } 110 111 114 public boolean queue(ILabelUpdate[] updates) { 115 return fUpdater.queue(updates); 116 } 117 118 121 public boolean shouldRun() { 122 return fUpdater.shouldRun(); 123 } 124 } 125 126 129 class LabelUpdater implements Runnable { 130 131 LinkedList fQueue = new LinkedList (); 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 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 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 e) { 173 fQueue = null; 174 } 175 return update; 176 } 177 178 public boolean shouldRun() { 179 return fQueue != null; 180 } 181 } 182 183 188 protected void retrieveLabel(ILabelUpdate update) throws CoreException { 189 String [] 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 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 216 protected FontData getFontData(TreePath elementPath, IPresentationContext presentationContext, String columnId) throws CoreException { 217 return null; 218 } 219 220 227 protected RGB getForeground(TreePath elementPath, IPresentationContext presentationContext, String columnId) throws CoreException { 228 return null; 229 } 230 231 238 protected RGB getBackground(TreePath elementPath, IPresentationContext presentationContext, String columnId) throws CoreException { 239 return null; 240 } 241 242 249 protected ImageDescriptor getImageDescriptor(TreePath elementPath, IPresentationContext presentationContext, String columnId) throws CoreException { 250 return null; 251 } 252 253 260 protected abstract String getLabel(TreePath elementPath, IPresentationContext presentationContext, String columnId) throws CoreException; 261 262 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 fLabelJob.schedule(); 275 } 276 277 284 private Job newLabelJob(ILabelUpdate[] updates) { 285 if (requiresUIJob(updates)) { 286 return new UILabelJob(); 287 } else { 288 return new LabelJob(); 289 } 290 } 291 292 297 protected boolean requiresUIJob(ILabelUpdate[] updates) { 298 return false; 299 } 300 301 } 302 | Popular Tags |