78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2005, 2007 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Brad Reynolds - bug 164653
|
|
11 *******************************************************************************/
|
|
12 module org.eclipse.core.databinding.observable.value.AbstractVetoableValue;
|
81
|
13 import org.eclipse.core.databinding.observable.value.IVetoableValue;
|
|
14 import org.eclipse.core.databinding.observable.value.ValueDiff;
|
|
15 import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
|
|
16 import org.eclipse.core.databinding.observable.value.IValueChangingListener;
|
|
17 import org.eclipse.core.databinding.observable.value.ChangeVetoException;
|
|
18 import org.eclipse.core.databinding.observable.value.ValueChangingEvent;
|
78
|
19
|
|
20 import java.lang.all;
|
|
21
|
|
22 import org.eclipse.core.databinding.observable.Diffs;
|
|
23 import org.eclipse.core.databinding.observable.Realm;
|
|
24 import org.eclipse.core.internal.databinding.Util;
|
|
25
|
|
26 /**
|
|
27 *
|
|
28 * <p>
|
|
29 * This class is thread safe. All state accessing methods must be invoked from
|
|
30 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
|
|
31 * listeners may be invoked from any thread.
|
|
32 * </p>
|
|
33 * @since 1.0
|
|
34 *
|
|
35 */
|
|
36 public abstract class AbstractVetoableValue : AbstractObservableValue
|
|
37 , IVetoableValue {
|
|
38
|
|
39 /**
|
|
40 * Creates a new vetoable value.
|
|
41 */
|
|
42 public this() {
|
|
43 this(Realm.getDefault());
|
|
44 }
|
|
45
|
|
46 /**
|
|
47 * @param realm
|
|
48 */
|
|
49 public this(Realm realm) {
|
|
50 super(realm);
|
|
51 }
|
|
52
|
|
53 final protected void doSetValue(Object value) {
|
|
54 Object currentValue = doGetValue();
|
|
55 ValueDiff diff = Diffs.createValueDiff(currentValue, value);
|
|
56 bool okToProceed = fireValueChanging(diff);
|
|
57 if (!okToProceed) {
|
|
58 throw new ChangeVetoException("Change not permitted"); //$NON-NLS-1$
|
|
59 }
|
|
60 doSetApprovedValue(value);
|
|
61
|
|
62 if (!Util.equals(diff.getOldValue(), diff.getNewValue())) {
|
|
63 fireValueChange(diff);
|
|
64 }
|
|
65 }
|
|
66
|
|
67 /**
|
|
68 * Sets the value. Invoked after performing veto checks. Should not fire change events.
|
|
69 *
|
|
70 * @param value
|
|
71 */
|
|
72 protected abstract void doSetApprovedValue(Object value);
|
|
73
|
|
74 public synchronized void addValueChangingListener(
|
|
75 IValueChangingListener listener) {
|
|
76 addListener(ValueChangingEvent.TYPE, listener);
|
|
77 }
|
|
78
|
|
79 public synchronized void removeValueChangingListener(
|
|
80 IValueChangingListener listener) {
|
|
81 removeListener(ValueChangingEvent.TYPE, listener);
|
|
82 }
|
|
83
|
|
84 /**
|
|
85 * Notifies listeners about a pending change, and returns true if no
|
|
86 * listener vetoed the change.
|
|
87 *
|
|
88 * @param diff
|
|
89 * @return false if the change was vetoed, true otherwise
|
|
90 */
|
|
91 protected bool fireValueChanging(ValueDiff diff) {
|
|
92 checkRealm();
|
|
93
|
|
94 ValueChangingEvent event = new ValueChangingEvent(this, diff);
|
|
95 fireEvent(event);
|
|
96 return !event.veto;
|
|
97 }
|
|
98
|
|
99 public synchronized void dispose() {
|
|
100 super.dispose();
|
|
101 }
|
|
102
|
|
103 }
|