Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cce0299d7eeae38a4a022921159c420a116958b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*******************************************************************************
 * Copyright (c) 2005, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.viewers.update;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IDebugEventSetListener;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.internal.ui.viewers.provisional.AbstractModelProxy;

/**
 * @since 3.2
 */
public abstract class EventHandlerModelProxy extends AbstractModelProxy implements IDebugEventSetListener {

    /**
     * Map of elements to timer tasks
     */
	private Map<Object, PendingSuspendTask> fTimerTasks = new HashMap<>();

    /**
     * Timer for timer tasks
     */
    private Timer fTimer = new Timer(true);

    /**
     * Map of event source to resume events with a pending suspend that timed
     * out.
     */
	private Map<Object, DebugEvent> fPendingSuspends = new HashMap<>();

    /**
     * Event handlers for specific elements
     */
    private DebugEventHandler[] fHandlers = new DebugEventHandler[0];

    /**
     * Task used to update an element that resumed for a step or evaluation that
     * took too long to suspend.
     */
    private class PendingSuspendTask extends TimerTask {

        private DebugEvent fEvent;

        private DebugEventHandler fHandler;

        /**
         * Resume event for which there is a pending suspend.
         *
         * @param resume
         *            event
         */
        public PendingSuspendTask(DebugEventHandler handler, DebugEvent resume) {
            fHandler = handler;
            fEvent = resume;
        }

        /*
         * (non-Javadoc)
         *
         * @see java.util.TimerTask#run()
         */
        @Override
		public void run() {
            synchronized (fPendingSuspends) {
                fPendingSuspends.put(fEvent.getSource(), fEvent);
            }
            dispatchSuspendTimeout(fHandler, fEvent);
        }

    }

    /**
     * Adds the given handler to this event update policy.
     *
     * @param handler
     */
    protected abstract DebugEventHandler[] createEventHandlers();

    @Override
	public synchronized void dispose() {
    	super.dispose();
        fTimer.cancel();
        fTimerTasks.clear();
        DebugPlugin.getDefault().removeDebugEventListener(this);
        for (int i = 0; i < fHandlers.length; i++) {
            DebugEventHandler handler = fHandlers[i];
            handler.dispose();
        }
    }

    @Override
	public void init(IPresentationContext context) {
    	super.init(context);
        DebugPlugin.getDefault().addDebugEventListener(this);
        fHandlers = createEventHandlers();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
     */
    @Override
	public final void handleDebugEvents(DebugEvent[] events) {
        if (isDisposed()) {
            return;
        }
        for (int i = 0; i < events.length; i++) {
            DebugEvent event = events[i];
            if (containsEvent(event)) {
                for (int j = 0; j < fHandlers.length; j++) {
                    DebugEventHandler handler = fHandlers[j];
                    if (isDisposed()) {
                    	return;
                    }
                    if (handler.handlesEvent(event)) {
                        switch (event.getKind()) {
	                        case DebugEvent.CREATE:
	                            dispatchCreate(handler, event);
	                            break;
	                        case DebugEvent.TERMINATE:
	                            dispatchTerminate(handler, event);
	                            break;
	                        case DebugEvent.SUSPEND:
	                            dispatchSuspend(handler, event);
	                            break;
	                        case DebugEvent.RESUME:
	                            dispatchResume(handler, event);
	                            break;
	                        case DebugEvent.CHANGE:
	                            dispatchChange(handler, event);
	                            break;
	                        default:
	                            dispatchOther(handler, event);
	                            break;
                        }
                    }
                }
            }
        }
    }

    /**
     * Returns whether this event handler should process the event.
     *
     * @param event debug event
     * @return whether this event handler should process the event
     */
    protected boolean containsEvent(DebugEvent event) {
        return true;
    }

    /**
     * Dispatches a create event.
     *
     * @param event
     */
    protected void dispatchCreate(DebugEventHandler handler, DebugEvent event) {
        handler.handleCreate(event);
    }

    /**
     * Dispatches a terminate event.
     *
     * @param event
     */
    protected void dispatchTerminate(DebugEventHandler handler, DebugEvent event) {
        handler.handleTerminate(event);
    }

    /**
     * Dispatches a suspend event. Subclasses may override.
     *
     * @param event
     */
    protected void dispatchSuspend(DebugEventHandler handler, DebugEvent event) {
        // stop timer, if any
        synchronized (this) {
            TimerTask task = fTimerTasks.remove(event.getSource());
            if (task != null) {
                task.cancel();
            }
        }
        DebugEvent resume = null;
        synchronized (this) {
            resume = fPendingSuspends.remove(event.getSource());
        }
        if (resume == null) {
            handler.handleSuspend(event);
        } else {
            handler.handleLateSuspend(event, resume);
        }
    }

    /**
     * Dispatches a resume event. By default, if the resume is for an evaluation
     * or a step, a timer is started to update the event source if the step or
     * evaluation takes more than 500ms. Otherwise the source is refreshed.
     * Subclasses may override.
     *
     * @param event
     */
    protected void dispatchResume(DebugEventHandler handler, DebugEvent event) {
        if (event.isEvaluation() || event.isStepStart()) {
            // start a timer to update if the corresponding suspend does not
            // come quickly
        	synchronized (this) {
        		if (!isDisposed()) {
                    PendingSuspendTask task = new PendingSuspendTask(handler, event);
                    fTimerTasks.put(event.getSource(), task);
                    fTimer.schedule(task, 500);
        		}
			}
        	if (!isDisposed()) {
        		handler.handleResumeExpectingSuspend(event);
        	}
        } else {
            handler.handleResume(event);
        }
    }

    /**
     * Dispatches a change event.
     *
     * @param event
     */
    protected void dispatchChange(DebugEventHandler handler, DebugEvent event) {
        handler.handleChange(event);
    }

    /**
     * Dispatches an unknown event.
     *
     * @param event
     */
    protected void dispatchOther(DebugEventHandler handler, DebugEvent event) {
        handler.handleOther(event);
    }

    /**
     * Notification that a pending suspend event was not received for the given
     * resume event and handler within the timeout period.
     *
     * @param resume
     *            resume event with missing suspend event
     */
    protected void dispatchSuspendTimeout(DebugEventHandler handler, DebugEvent resume) {
        handler.handleSuspendTimeout(resume);
    }

    /**
     * Returns the index of the given element in the list or -1 if
     * not present.
     *
     * @param list
     * @param element
     * @return index or -1 if not present
     */
	protected int indexOf(Object[] list, Object element) {
		for (int i = 0; i < list.length; i++) {
			if (element.equals(list[i])) {
				return i;
			}
		}
		return -1;
	}

}

Back to the top