Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8a5b036a65ff7c2c71ae73a187ba2a03a641359c (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
/*******************************************************************************
 * Copyright (c) 2005, 2011 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 org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelDelta;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ModelDelta;
import org.eclipse.debug.internal.ui.viewers.provisional.AbstractModelProxy;

/**
 * Handles debug events for an event update policy in a viewer.
 *
 * @since 3.2
 */
public abstract class DebugEventHandler {

	private AbstractModelProxy fModelProxy;

	/**
	 * Constructs an event handler for the given model proxy.
	 *
	 * @param proxy the backing proxy
	 */
	public DebugEventHandler(AbstractModelProxy proxy) {
		fModelProxy = proxy;
	}

	/**
	 * Disposes this event handler
	 */
	public synchronized void dispose() {
		fModelProxy = null;
	}

	/**
	 * Returns the model proxy this event handler working for,
	 * or <code>null</code> if disposed.
	 *
	 * @return the backing model proxy
	 */
	protected synchronized AbstractModelProxy getModelProxy() {
		return fModelProxy;
	}

	/**
	 * Returns whether this event handler handles the given event
	 *
	 * @param event event to handle
	 * @return whether this event handler handles the given event
	 */
	protected abstract boolean handlesEvent(DebugEvent event);

	/**
	 * Handles a create event.
	 *
	 * @param event the event to handle
	 */
	protected void handleCreate(DebugEvent event) {
		refreshRoot(event);
	}

	/**
	 * Handles a terminate event.
	 *
	 * @param event the event to handle
	 */
	protected void handleTerminate(DebugEvent event) {
		refreshRoot(event);
	}

	/**
	 * Handles a suspend event.
	 *
	 * @param event the event to handle
	 */
	protected void handleSuspend(DebugEvent event) {
		refreshRoot(event);
	}

	/**
	 * Handles a resume event for which a suspend is expected shortly (<500ms).
	 *
	 * @param event the event to handle
	 */
	protected void handleResumeExpectingSuspend(DebugEvent event) {
		// do nothing unless the suspend times out
	}

	/**
	 * Handles a resume event that is not expecting an immediate suspend event
	 *
	 * @param event the event to handle
	 */
	protected void handleResume(DebugEvent event) {
		refreshRoot(event);
	}

	/**
	 * Handles a change event.
	 *
	 * @param event the event to handle
	 */
	protected void handleChange(DebugEvent event) {
		refreshRoot(event);
	}

	/**
	 * Handles an unknown event.
	 *
	 * @param event the event to handle
	 */
	protected void handleOther(DebugEvent event) {
		refreshRoot(event);
	}

	/**
	 * Notification that a pending suspend event was not received for the given
	 * resume event within the timeout period.
	 *
	 * @param event the event to handle
	 */
	protected void handleSuspendTimeout(DebugEvent event) {
		refreshRoot(event);
	}

	/**
	 * Handles the given suspend event which caused a timeout. It is
	 * paired with its original resume event.
	 *
	 * @param suspend suspend event
	 * @param resume resume event
	 */
	protected void handleLateSuspend(DebugEvent suspend, DebugEvent resume) {
		refreshRoot(suspend);
	}

	/**
	 * Fires a model delta to indicate that the launch manager should be refreshed.
	 * Subclasses should override individual handle events to provide deltas that
	 * better reflect the actual change in the model.
	 *
	 * @param event the event that triggered the root refresh
	 */
	protected void refreshRoot(DebugEvent event) {
		ModelDelta delta = new ModelDelta(DebugPlugin.getDefault().getLaunchManager(), IModelDelta.CONTENT);
		fireDelta(delta);
	}

	/**
	 * Fires the given delta, unless this handler has been disposed.
	 *
	 * @param delta the delta to fire in the backing model proxy
	 */
	protected void fireDelta(IModelDelta delta) {
		AbstractModelProxy modelProxy = getModelProxy();
		if (modelProxy != null) {
			modelProxy.fireModelChanged(delta);
		}
	}

	/**
	 * Returns whether this handler has been disposed.
	 *
	 * @return whether this handler has been disposed
	 */
	protected synchronized boolean isDisposed() {
		return fModelProxy == null;
	}

	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