Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 69ebfae28b586abc10b79aca31f0dc307f51e8cf (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
/*******************************************************************************
 * Copyright (c) 2009 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.e4.core.internal.contexts;

import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.contexts.RunAndTrack;

/**
 * An event describing a change to an {@link IEclipseContext}. The following types of events are
 * currently defined:
 * <ul>
 * <li>An event indicating that the listener has just been registered with a context.</li>
 * <li>An after-the-fact report that a context value has been changed (either a new value or a
 * change to an existing value.</li>
 * <li>An after-the-fact report that a context value has been removed.</li>
 * <li>A report that a context is about to be disposed.</li>
 * </ul>
 * <p>
 * In order to handle additional event types that may be introduced in future releases, clients
 * should not write code that presumes the set of event types is closed.
 * </p>
 * 
 * @see RunAndTrack
 */
public final class ContextChangeEvent {
	/**
	 * A change event type (value "0"), indicating that the listener receiving this event has just
	 * been registered with a context.
	 * 
	 * @see IEclipseContext#runAndTrack(RunAndTrack)
	 */
	public static final int INITIAL = 0;

	/**
	 * A change event type (value "1"), indicating that a context value has been added.
	 */
	public static final int ADDED = 1;

	/**
	 * A change event type (value "2"), indicating that a context value has been removed.
	 */
	public static final int REMOVED = 2;

	/**
	 * A change event type (value "3") indicating that the context is being disposed. The context is
	 * still valid at the time of this event.
	 */
	public static final int DISPOSE = 3;

	/**
	 * A change event type (value "4"), indicating that a context has been un-injected from the
	 * object.
	 */
	public static final int UNINJECTED = 4;

	/**
	 * A change event type (value "5"), indicating that a context needs to process scheduled
	 * updates.
	 */
	public static final int UPDATE = 5;

	/**
	 * A change event type (value "6"), indicating that stored computations depending
	 * on a given name might need to be updated.
	 */
	public static final int RECALC = 6;

	private Object[] args;
	private IEclipseContext context;
	private int eventType;
	private String key;

	private Object oldValue;

	/**
	 * Creates a new context event.
	 * 
	 * @param context
	 * @param eventType
	 * @param args
	 * @param name
	 */
	ContextChangeEvent(IEclipseContext context, int eventType, Object[] args, String name, Object oldValue) {
		this.context = context;
		this.key = name;
		this.eventType = eventType;
		this.args = args;
		this.oldValue = oldValue;
	}

	/**
	 * Returns the arguments that were supplied when the listener was registered.
	 * 
	 * @see IEclipseContext#runAndTrack(RunAndTrack)
	 * @return the arguments that were supplied when the listener was registered.
	 */
	public Object[] getArguments() {
		return args;
	}

	/**
	 * Returns the context where the change occurred.
	 * 
	 * @return the context where the change occurred
	 */
	public IEclipseContext getContext() {
		return context;
	}

	/**
	 * Returns the type of content change that occurred.
	 * 
	 * @return the type of content change that occurred.
	 */
	public int getEventType() {
		return eventType;
	}

	/**
	 * Returns the name of the context value that changed, or <code>null</code> if not applicable if
	 * this event type.
	 * 
	 * @return The name of the changed context value, or <code>null</code>
	 */
	public String getName() {
		return key;
	}

	public Object getOldValue() {
		return oldValue;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		// if ((eventType == DISPOSE) || (eventType == UNINJECTED))
		// result = prime * result + ((context == null) ? 0 : context.hashCode());
		result = prime * result + eventType;
		result = prime * result + ((key == null) ? 0 : key.hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ContextChangeEvent other = (ContextChangeEvent) obj;

		// if ((eventType == DISPOSE) || (eventType == UNINJECTED)) {
		// if (context == null) {
		// if (other.context != null)
		// return false;
		// } else if (!context.equals(other.context))
		// return false;
		// }

		if (eventType != other.eventType)
			return false;
		if (key == null) {
			if (other.key != null)
				return false;
		} else if (!key.equals(other.key))
			return false;
		return true;
	}

}

Back to the top