comparison org.eclipse.core.databinding.observable/src/org/eclipse/core/databinding/observable/value/AbstractVetoableValue.d @ 95:6208d4f6a277

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