1 11 package org.eclipse.debug.internal.ui.views; 12 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 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.core.DebugEvent; 22 import org.eclipse.debug.core.DebugPlugin; 23 import org.eclipse.debug.core.IDebugEventSetListener; 24 import org.eclipse.debug.ui.AbstractDebugView; 25 import org.eclipse.jface.viewers.IBasicPropertyConstants; 26 import org.eclipse.jface.viewers.ITreeContentProvider; 27 import org.eclipse.jface.viewers.StructuredSelection; 28 import org.eclipse.jface.viewers.TreeViewer; 29 import org.eclipse.jface.viewers.Viewer; 30 import org.eclipse.ui.progress.UIJob; 31 32 35 public abstract class AbstractDebugEventHandler implements IDebugEventSetListener { 36 37 40 private AbstractDebugView fView; 41 42 45 private List fEventSetQueue = new ArrayList (); 46 47 50 private List fDataQueue = new ArrayList (); 51 52 55 private Object LOCK = new Object (); 56 57 60 private EventProcessingJob fUpdateJob = new EventProcessingJob(); 61 62 65 protected static final DebugEvent[] EMPTY_EVENT_SET = new DebugEvent[0]; 66 67 private Object NULL = new Object (); 68 69 72 private class EventProcessingJob extends UIJob { 73 74 private static final int TIMEOUT = 200; 75 76 public EventProcessingJob() { 77 super(DebugUIViewsMessages.AbstractDebugEventHandler_0); setSystem(true); 79 setPriority(Job.INTERACTIVE); 80 } 81 82 85 public IStatus runInUIThread(IProgressMonitor monitor) { 86 boolean more = true; 87 long start = System.currentTimeMillis(); 88 while (more) { 90 DebugEvent[] eventSet = null; 91 Object data = null; 92 synchronized (LOCK) { 93 if (fEventSetQueue.isEmpty()) { 94 return Status.OK_STATUS; 95 } 96 eventSet = (DebugEvent[]) fEventSetQueue.remove(0); 97 more = !fEventSetQueue.isEmpty(); 98 data = fDataQueue.remove(0); 99 if (data == NULL) { 100 data = null; 101 } 102 } 103 if (isAvailable()) { 104 if (isViewVisible()) { 105 doHandleDebugEvents(eventSet, data); 106 } 107 updateForDebugEvents(eventSet, data); 108 } 109 110 if (more) { 111 long current = System.currentTimeMillis(); 112 if (current - start > TIMEOUT) { 113 break; 114 } 115 } 116 } 117 if (more) { 118 schedule(50); 120 } 121 return Status.OK_STATUS; 122 } 123 124 } 125 130 public AbstractDebugEventHandler(AbstractDebugView view) { 131 setView(view); 132 DebugPlugin plugin= DebugPlugin.getDefault(); 133 plugin.addDebugEventListener(this); 134 } 135 136 139 public void handleDebugEvents(DebugEvent[] events) { 140 if (!isAvailable()) { 141 return; 142 } 143 events = filterEvents(events); 145 if (events.length == 0) { 146 return; 147 } 148 synchronized (LOCK) { 149 events = doPreprocessEvents(events); 150 if (events.length == 0) { 151 return; 152 } 153 fEventSetQueue.add(events); 155 if (fDataQueue.size() < fEventSetQueue.size()) { 156 fDataQueue.add(NULL); 157 } 158 } 159 fUpdateJob.schedule(); 160 } 161 162 protected void queueData(Object data) { 163 synchronized (LOCK) { 164 fDataQueue.add(data); 165 } 166 } 167 168 protected DebugEvent[] doPreprocessEvents(DebugEvent[] events) { 169 return events; 170 } 171 172 178 protected DebugEvent[] filterEvents(DebugEvent[] events) { 179 return events; 180 } 181 182 189 protected void updateForDebugEvents(DebugEvent[] events, Object data) { 190 } 191 192 196 protected abstract void doHandleDebugEvents(DebugEvent[] events, Object data); 197 198 201 protected void insert(Object element) { 202 if (isAvailable()) { 203 Object parent= ((ITreeContentProvider)getTreeViewer().getContentProvider()).getParent(element); 204 if (parent != null) { 207 getView().showViewer(); 208 getTreeViewer().add(parent, element); 209 } 210 } 211 } 212 213 216 protected void remove(Object element) { 217 if (isAvailable()) { 218 getView().showViewer(); 219 getTreeViewer().remove(element); 220 } 221 } 222 223 226 protected void labelChanged(Object element) { 227 if (isAvailable()) { 228 getView().showViewer(); 229 getTreeViewer().update(element, new String [] {IBasicPropertyConstants.P_TEXT}); 230 } 231 } 232 233 236 protected void refresh(Object element) { 237 if (isAvailable()) { 238 getView().showViewer(); 239 getTreeViewer().refresh(element); 240 } 241 } 242 243 246 public void refresh() { 247 if (isAvailable()) { 248 getView().showViewer(); 249 getTreeViewer().refresh(); 250 } 251 } 252 253 256 protected void selectAndReveal(Object element) { 257 if (isAvailable()) { 258 getViewer().setSelection(new StructuredSelection(element), true); 259 } 260 } 261 262 265 public void dispose() { 266 DebugPlugin plugin= DebugPlugin.getDefault(); 267 plugin.removeDebugEventListener(this); 268 synchronized (LOCK) { 269 fEventSetQueue.clear(); 270 fDataQueue.clear(); 271 } 272 } 273 274 280 protected AbstractDebugView getView() { 281 return fView; 282 } 283 284 289 private void setView(AbstractDebugView view) { 290 fView = view; 291 } 292 293 299 protected Viewer getViewer() { 300 return getView().getViewer(); 301 } 302 303 310 protected TreeViewer getTreeViewer() { 311 if (getViewer() instanceof TreeViewer) { 312 return (TreeViewer)getViewer(); 313 } 314 return null; 315 } 316 317 324 protected boolean isAvailable() { 325 return getView().isAvailable(); 326 } 327 328 333 protected boolean isViewVisible() { 334 return getView().isVisible(); 335 } 336 337 341 protected void viewBecomesVisible() { 342 refresh(); 343 } 344 345 349 protected void viewBecomesHidden() { 350 } 351 } 352 353 | Popular Tags |