blob: 07c3427dfed2e3f941a556a9df12f87832a23e14 (
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
|
/*******************************************************************************
* Copyright (c) 2009, 2015 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 263691)
* Stefan Xenos <sxenos@gmail.com> - Bug 335792
******************************************************************************/
package org.eclipse.core.databinding.observable.value;
import org.eclipse.core.databinding.observable.Diffs;
/**
* An {@link IVetoableValue} decorator for an observable value.
*
* @param <T>
* the type of value being observed
* @since 1.2
*/
public class DecoratingVetoableValue<T> extends DecoratingObservableValue<T>
implements IVetoableValue<T> {
/**
* @param decorated
* @param disposeDecoratedOnDispose
*/
public DecoratingVetoableValue(IObservableValue<T> decorated,
boolean disposeDecoratedOnDispose) {
super(decorated, disposeDecoratedOnDispose);
}
@Override
public void setValue(T value) {
checkRealm();
T currentValue = getValue();
ValueDiff<T> diff = Diffs.createValueDiff(currentValue, value);
boolean okToProceed = fireValueChanging(diff);
if (!okToProceed) {
throw new ChangeVetoException("Change not permitted"); //$NON-NLS-1$
}
super.setValue(value);
}
@Override
public synchronized void addValueChangingListener(
IValueChangingListener<T> listener) {
addListener(ValueChangingEvent.TYPE, listener);
}
@Override
public synchronized void removeValueChangingListener(
IValueChangingListener<T> listener) {
removeListener(ValueChangingEvent.TYPE, listener);
}
/**
* Notifies listeners about a pending change, and returns true if no
* listener vetoed the change.
*
* @param diff
* @return false if the change was vetoed, true otherwise
*/
protected boolean fireValueChanging(ValueDiff<T> diff) {
checkRealm();
ValueChangingEvent<T> event = new ValueChangingEvent<>(this, diff);
fireEvent(event);
return !event.veto;
}
}
|