KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > commands > State


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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  *******************************************************************************/

11
12 package org.eclipse.core.commands;
13
14 import org.eclipse.core.commands.common.EventManager;
15 import org.eclipse.core.internal.commands.util.Util;
16
17 /**
18  * <p>
19  * A piece of state information that can be shared between objects, and might be
20  * persisted between sessions. This can be used for commands that toggle between
21  * two states and wish to pass this state information between different
22  * handlers.
23  * </p>
24  * <p>
25  * This state object can either be used as a single state object shared between
26  * several commands, or one state object per command -- depending on the needs
27  * of the application.
28  * </p>
29  * <p>
30  * Clients may instantiate or extend this class.
31  * </p>
32  *
33  * @since 3.2
34  */

35 public class State extends EventManager {
36
37     /**
38      * The identifier of the state; may be <code>null</code> if it has not
39      * been initialized.
40      */

41     private String JavaDoc id;
42
43     /**
44      * The value held by this state; may be anything at all.
45      */

46     private Object JavaDoc value;
47
48     /**
49      * Adds a listener to changes for this state.
50      *
51      * @param listener
52      * The listener to add; must not be <code>null</code>.
53      */

54     public void addListener(final IStateListener listener) {
55         addListenerObject(listener);
56     }
57
58     /**
59      * Disposes of this state. This allows the state to unregister itself with
60      * any managers or as a listener.
61      */

62     public void dispose() {
63         // The default implementation does nothing.
64
}
65
66     /**
67      * Notifies listeners to this state that it has changed in some way.
68      *
69      * @param oldValue
70      * The old value; may be anything.
71      */

72     protected final void fireStateChanged(final Object JavaDoc oldValue) {
73         final Object JavaDoc[] listeners = getListeners();
74         for (int i = 0; i < listeners.length; i++) {
75             final IStateListener listener = (IStateListener) listeners[i];
76             listener.handleStateChange(this, oldValue);
77         }
78     }
79
80     /**
81      * Returns the identifier for this state.
82      *
83      * @return The id; may be <code>null</code>.
84      */

85     public final String JavaDoc getId() {
86         return id;
87     }
88
89     /**
90      * The current value associated with this state. This can be any type of
91      * object, but implementations will usually restrict this value to a
92      * particular type.
93      *
94      * @return The current value; may be anything.
95      */

96
97     public Object JavaDoc getValue() {
98         return value;
99     }
100
101     /**
102      * Removes a listener to changes from this state.
103      *
104      * @param listener
105      * The listener to remove; must not be <code>null</code>.
106      */

107
108     public void removeListener(final IStateListener listener) {
109         removeListenerObject(listener);
110     }
111
112     /**
113      * Sets the identifier for this object. This method should only be called
114      * by the command framework. Clients should not call this method.
115      *
116      * @param id
117      * The id; must not be <code>null</code>.
118      */

119     public void setId(final String JavaDoc id) {
120         this.id = id;
121     }
122
123     /**
124      * Sets the value for this state object.
125      *
126      * @param value
127      * The value to set; may be anything.
128      */

129     public void setValue(final Object JavaDoc value) {
130         if (!Util.equals(this.value, value)) {
131             final Object JavaDoc oldValue = this.value;
132             this.value = value;
133             fireStateChanged(oldValue);
134         }
135     }
136 }
137
Popular Tags