Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 38278d77e23a5577960fb0a4eb95711022576d64 (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
/*******************************************************************************
 * Copyright (c) 2009, 2012 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 java.lang.ref.Reference;
import java.util.Set;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.contexts.RunAndTrack;
import org.eclipse.e4.core.internal.contexts.EclipseContext.Scheduled;

public class TrackableComputationExt extends Computation {

	final private IEclipseContext originatingContext;
	final private RunAndTrack runnable;

	private ContextChangeEvent cachedEvent;

	public TrackableComputationExt(RunAndTrack runnable, IEclipseContext originatingContext) {
		this.runnable = runnable;
		this.originatingContext = originatingContext;
		init();
	}

	public int hashCode() {
		return hashCode;
	}

	protected int calcHashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((originatingContext == null) ? 0 : originatingContext.hashCode());
		result = prime * result + ((runnable == null) ? 0 : runnable.hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TrackableComputationExt other = (TrackableComputationExt) obj;
		if (originatingContext == null) {
			if (other.originatingContext != null)
				return false;
		} else if (!originatingContext.equals(other.originatingContext))
			return false;
		if (runnable == null) {
			if (other.runnable != null)
				return false;
		} else if (!runnable.equals(other.runnable))
			return false;
		return true;
	}

	public void handleInvalid(ContextChangeEvent event, Set<Scheduled> scheduledList) {
		//	don't call super - we keep the link unless uninjected / disposed
		int eventType = event.getEventType();
		if (eventType == ContextChangeEvent.INITIAL || eventType == ContextChangeEvent.DISPOSE) {
			// process right away
			update(event);
		} else {
			// schedule processing
			scheduledList.add(new Scheduled(this, event));
		}
	}

	public boolean update(ContextChangeEvent event) {
		// is this a structural event?
		// structural changes: INITIAL, DISPOSE, UNINJECTED are always processed right away
		int eventType = event.getEventType();
		if ((runnable instanceof RunAndTrackExt) && ((RunAndTrackExt) runnable).batchProcess()) {
			if ((eventType == ContextChangeEvent.ADDED) || (eventType == ContextChangeEvent.REMOVED)) {
				cachedEvent = event;
				EclipseContext eventsContext = (EclipseContext) event.getContext();
				eventsContext.addWaiting(this);
				return true;
			}
		}

		((EclipseContext) originatingContext).pushComputation(this);
		boolean result = true;
		try {
			if (cachedEvent != null) {
				if (runnable instanceof RunAndTrackExt) {
					result = ((RunAndTrackExt) runnable).update(event.getContext(), event.getEventType(), event.getArguments());
					if (eventType != ContextChangeEvent.DISPOSE && eventType != ContextChangeEvent.UNINJECTED)
						cachedEvent = null;
				} else {
					if (eventType != ContextChangeEvent.DISPOSE && eventType != ContextChangeEvent.UNINJECTED) {
						result = runnable.changed(originatingContext);
						cachedEvent = null;
					}
				}
			}
			if (eventType != ContextChangeEvent.UPDATE) {
				if (runnable instanceof RunAndTrackExt)
					result = ((RunAndTrackExt) runnable).update(event.getContext(), event.getEventType(), event.getArguments());
				else {
					if (eventType != ContextChangeEvent.DISPOSE && eventType != ContextChangeEvent.UNINJECTED)
						result = runnable.changed(originatingContext);
				}
			}
		} finally {
			((EclipseContext) originatingContext).popComputation(this);
		}
		EclipseContext eventsContext = (EclipseContext) event.getContext();

		if (eventType == ContextChangeEvent.DISPOSE) {
			if (originatingContext.equals(eventsContext)) {
				((EclipseContext) originatingContext).removeRAT(this);
				return false;
			}
		}
		if (!result) {
			((EclipseContext) originatingContext).removeRAT(this);
		}
		return result;
	}

	public String toString() {
		return runnable.toString();
	}

	public Reference<Object> getReference() {
		if (runnable instanceof RunAndTrackExt)
			return ((RunAndTrackExt) runnable).getReference();
		return null;
	}

}

Back to the top