comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/observable/value/AbstractVetoableValue.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 383ce7bd736b
comparison
equal deleted inserted replaced
76:f05e6e8b2f2d 78:0a55d2d5a946
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;
13
14 import java.lang.all;
15
16 import org.eclipse.core.databinding.observable.Diffs;
17 import org.eclipse.core.databinding.observable.Realm;
18 import org.eclipse.core.internal.databinding.Util;
19
20 /**
21 *
22 * <p>
23 * This class is thread safe. All state accessing methods must be invoked from
24 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
25 * listeners may be invoked from any thread.
26 * </p>
27 * @since 1.0
28 *
29 */
30 public abstract class AbstractVetoableValue : AbstractObservableValue
31 , IVetoableValue {
32
33 /**
34 * Creates a new vetoable value.
35 */
36 public this() {
37 this(Realm.getDefault());
38 }
39
40 /**
41 * @param realm
42 */
43 public this(Realm realm) {
44 super(realm);
45 }
46
47 final protected void doSetValue(Object value) {
48 Object currentValue = doGetValue();
49 ValueDiff diff = Diffs.createValueDiff(currentValue, value);
50 bool okToProceed = fireValueChanging(diff);
51 if (!okToProceed) {
52 throw new ChangeVetoException("Change not permitted"); //$NON-NLS-1$
53 }
54 doSetApprovedValue(value);
55
56 if (!Util.equals(diff.getOldValue(), diff.getNewValue())) {
57 fireValueChange(diff);
58 }
59 }
60
61 /**
62 * Sets the value. Invoked after performing veto checks. Should not fire change events.
63 *
64 * @param value
65 */
66 protected abstract void doSetApprovedValue(Object value);
67
68 public synchronized void addValueChangingListener(
69 IValueChangingListener listener) {
70 addListener(ValueChangingEvent.TYPE, listener);
71 }
72
73 public synchronized void removeValueChangingListener(
74 IValueChangingListener listener) {
75 removeListener(ValueChangingEvent.TYPE, listener);
76 }
77
78 /**
79 * Notifies listeners about a pending change, and returns true if no
80 * listener vetoed the change.
81 *
82 * @param diff
83 * @return false if the change was vetoed, true otherwise
84 */
85 protected bool fireValueChanging(ValueDiff diff) {
86 checkRealm();
87
88 ValueChangingEvent event = new ValueChangingEvent(this, diff);
89 fireEvent(event);
90 return !event.veto;
91 }
92
93 public synchronized void dispose() {
94 super.dispose();
95 }
96
97 }