1 11 package org.eclipse.debug.internal.ui.viewers.update; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 import java.util.Timer ; 16 import java.util.TimerTask ; 17 18 import org.eclipse.debug.core.DebugEvent; 19 import org.eclipse.debug.core.DebugPlugin; 20 import org.eclipse.debug.core.IDebugEventSetListener; 21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext; 22 import org.eclipse.debug.internal.ui.viewers.provisional.AbstractModelProxy; 23 24 27 public abstract class EventHandlerModelProxy extends AbstractModelProxy implements IDebugEventSetListener { 28 29 32 private Map fTimerTasks = new HashMap (); 33 34 37 private Timer fTimer = new Timer (true); 38 39 43 private Map fPendingSuspends = new HashMap (); 44 45 48 private DebugEventHandler[] fHandlers = new DebugEventHandler[0]; 49 50 54 private class PendingSuspendTask extends TimerTask { 55 56 private DebugEvent fEvent; 57 58 private DebugEventHandler fHandler; 59 60 66 public PendingSuspendTask(DebugEventHandler handler, DebugEvent resume) { 67 fHandler = handler; 68 fEvent = resume; 69 } 70 71 76 public void run() { 77 synchronized (fPendingSuspends) { 78 fPendingSuspends.put(fEvent.getSource(), fEvent); 79 } 80 dispatchSuspendTimeout(fHandler, fEvent); 81 } 82 83 } 84 85 90 protected abstract DebugEventHandler[] createEventHandlers(); 91 92 public synchronized void dispose() { 93 super.dispose(); 94 fTimer.cancel(); 95 fTimerTasks.clear(); 96 DebugPlugin.getDefault().removeDebugEventListener(this); 97 for (int i = 0; i < fHandlers.length; i++) { 98 DebugEventHandler handler = fHandlers[i]; 99 handler.dispose(); 100 } 101 } 102 103 public void init(IPresentationContext context) { 104 super.init(context); 105 DebugPlugin.getDefault().addDebugEventListener(this); 106 fHandlers = createEventHandlers(); 107 } 108 109 114 public final void handleDebugEvents(DebugEvent[] events) { 115 if (isDisposed()) { 116 return; 117 } 118 for (int i = 0; i < events.length; i++) { 119 DebugEvent event = events[i]; 120 if (containsEvent(event)) { 121 for (int j = 0; j < fHandlers.length; j++) { 122 DebugEventHandler handler = fHandlers[j]; 123 if (isDisposed()) { 124 return; 125 } 126 if (handler.handlesEvent(event)) { 127 switch (event.getKind()) { 128 case DebugEvent.CREATE: 129 dispatchCreate(handler, event); 130 break; 131 case DebugEvent.TERMINATE: 132 dispatchTerminate(handler, event); 133 break; 134 case DebugEvent.SUSPEND: 135 dispatchSuspend(handler, event); 136 break; 137 case DebugEvent.RESUME: 138 dispatchResume(handler, event); 139 break; 140 case DebugEvent.CHANGE: 141 dispatchChange(handler, event); 142 break; 143 default: 144 dispatchOther(handler, event); 145 break; 146 } 147 } 148 } 149 } 150 } 151 } 152 153 protected boolean containsEvent(DebugEvent event) { 154 return true; 155 } 156 157 162 protected void dispatchCreate(DebugEventHandler handler, DebugEvent event) { 163 handler.handleCreate(event); 164 } 165 166 171 protected void dispatchTerminate(DebugEventHandler handler, DebugEvent event) { 172 handler.handleTerminate(event); 173 } 174 175 180 protected void dispatchSuspend(DebugEventHandler handler, DebugEvent event) { 181 synchronized (this) { 183 TimerTask task = (TimerTask ) fTimerTasks.remove(event.getSource()); 184 if (task != null) { 185 task.cancel(); 186 } 187 } 188 DebugEvent resume = null; 189 synchronized (this) { 190 resume = (DebugEvent) fPendingSuspends.remove(event.getSource()); 191 } 192 if (resume == null) { 193 handler.handleSuspend(event); 194 } else { 195 handler.handleLateSuspend(event, resume); 196 } 197 } 198 199 207 protected void dispatchResume(DebugEventHandler handler, DebugEvent event) { 208 if (event.isEvaluation() || event.isStepStart()) { 209 synchronized (this) { 212 if (!isDisposed()) { 213 PendingSuspendTask task = new PendingSuspendTask(handler, event); 214 fTimerTasks.put(event.getSource(), task); 215 fTimer.schedule(task, 500); 216 } 217 } 218 if (!isDisposed()) { 219 handler.handleResumeExpectingSuspend(event); 220 } 221 } else { 222 handler.handleResume(event); 223 } 224 } 225 226 231 protected void dispatchChange(DebugEventHandler handler, DebugEvent event) { 232 handler.handleChange(event); 233 } 234 235 240 protected void dispatchOther(DebugEventHandler handler, DebugEvent event) { 241 handler.handleOther(event); 242 } 243 244 251 protected void dispatchSuspendTimeout(DebugEventHandler handler, DebugEvent resume) { 252 handler.handleSuspendTimeout(resume); 253 } 254 255 263 protected int indexOf(Object [] list, Object element) { 264 for (int i = 0; i < list.length; i++) { 265 if (element.equals(list[i])) { 266 return i; 267 } 268 } 269 return -1; 270 } 271 272 } 273 | Popular Tags |