KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > observable > value > AbstractVetoableValue


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
17 /**
18  *
19  * <p>
20  * This class is thread safe. All state accessing methods must be invoked from
21  * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
22  * listeners may be invoked from any thread.
23  * </p>
24  * @since 1.0
25  *
26  */

27 public abstract class AbstractVetoableValue extends AbstractObservableValue
28         implements IVetoableValue {
29
30     /**
31      * Creates a new vetoable value.
32      */

33     public AbstractVetoableValue() {
34         this(Realm.getDefault());
35     }
36
37     /**
38      * @param realm
39      */

40     public AbstractVetoableValue(Realm realm) {
41         super(realm);
42     }
43
44     final protected void doSetValue(Object JavaDoc value) {
45         Object JavaDoc currentValue = doGetValue();
46         ValueDiff diff = Diffs.createValueDiff(currentValue, value);
47         boolean okToProceed = fireValueChanging(diff);
48         if (!okToProceed) {
49             throw new ChangeVetoException("Change not permitted"); //$NON-NLS-1$
50
}
51         doSetApprovedValue(value);
52         fireValueChange(diff);
53     }
54
55     /**
56      * Sets the value. Invoked after performing veto checks.
57      *
58      * @param value
59      */

60     protected abstract void doSetApprovedValue(Object JavaDoc value);
61
62     public synchronized void addValueChangingListener(
63             IValueChangingListener listener) {
64         addListener(ValueChangingEvent.TYPE, listener);
65     }
66
67     public synchronized void removeValueChangingListener(
68             IValueChangingListener listener) {
69         removeListener(ValueChangingEvent.TYPE, listener);
70     }
71
72     /**
73      * Notifies listeners about a pending change, and returns true if no
74      * listener vetoed the change.
75      *
76      * @param diff
77      * @return false if the change was vetoed, true otherwise
78      */

79     protected boolean fireValueChanging(ValueDiff diff) {
80         checkRealm();
81
82         ValueChangingEvent event = new ValueChangingEvent(this, diff);
83         fireEvent(event);
84         return !event.veto;
85     }
86
87     public synchronized void dispose() {
88         super.dispose();
89     }
90
91 }
92
Popular Tags