Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 642dfe6db427b586a6c6e7281373ccd3bcb7ec9f (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
/*******************************************************************************
 * Copyright (c) 2008 Matthew Hall 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:
 *     Matthew Hall - initial API and implementation (bug 237718)
 *     Matthew Hall - but 246626
 ******************************************************************************/

package org.eclipse.core.databinding.observable;

import org.eclipse.core.internal.databinding.Util;

/**
 * An observable which decorates another observable
 * 
 * @since 1.2
 * 
 */
public class DecoratingObservable extends AbstractObservable implements
		IDecoratingObservable {

	private IObservable decorated;

	private IStaleListener staleListener;

	private boolean disposedDecoratedOnDispose;

	/**
	 * Constructs a DecoratingObservable which decorates the given observable.
	 * 
	 * @param decorated
	 *            the observable being decorated.
	 * @param disposeDecoratedOnDispose
	 *            whether the decorated observable should be disposed when the
	 *            decorator is disposed
	 */
	public DecoratingObservable(IObservable decorated,
			boolean disposeDecoratedOnDispose) {
		super(decorated.getRealm());
		this.decorated = decorated;
		this.disposedDecoratedOnDispose = disposeDecoratedOnDispose;
	}

	public IObservable getDecorated() {
		return decorated;
	}

	public boolean isStale() {
		getterCalled();
		return decorated.isStale();
	}

	protected void getterCalled() {
		ObservableTracker.getterCalled(this);
	}

	protected void firstListenerAdded() {
		if (staleListener == null) {
			staleListener = new IStaleListener() {
				public void handleStale(StaleEvent staleEvent) {
					DecoratingObservable.this.handleStaleEvent(staleEvent);
				}
			};
		}
		decorated.addStaleListener(staleListener);
	}

	protected void lastListenerRemoved() {
		if (staleListener != null) {
			decorated.removeStaleListener(staleListener);
			staleListener = null;
		}
	}

	/**
	 * Called whenever a StaleEvent is received from the decorated observable.
	 * By default, this method fires the stale event again, with the decorating
	 * observable as the event source. Subclasses may override to provide
	 * different behavior.
	 * 
	 * @param event
	 *            the stale event received from the decorated observable
	 */
	protected void handleStaleEvent(StaleEvent event) {
		fireStale();
	}

	public boolean equals(Object obj) {
		getterCalled();
		if (obj == this)
			return true;
		if (obj == null)
			return false;
		if (getClass() == obj.getClass()) {
			DecoratingObservable other = (DecoratingObservable) obj;
			return Util.equals(this.decorated, other.decorated);
		}
		return Util.equals(decorated, obj);
	}

	public int hashCode() {
		getterCalled();
		return decorated.hashCode();
	}

	public synchronized void dispose() {
		if (decorated != null && staleListener != null) {
			decorated.removeStaleListener(staleListener);
		}
		if (decorated != null) {
			if (disposedDecoratedOnDispose)
				decorated.dispose();
			decorated = null;
		}
		staleListener = null;
		super.dispose();
	}
}

Back to the top