KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18 /**
19  * <p>
20  * An abstract implementation of {@link IObjectWithState}. This provides basic
21  * handling for adding and remove state. When state is added, the handler
22  * attaches itself as a listener and fire a handleStateChange event to notify
23  * this handler. When state is removed, the handler removes itself as a
24  * listener.
25  * </p>
26  * <p>
27  * Clients may extend this class.
28  * </p>
29  *
30  * @since 3.2
31  */

32 public abstract class AbstractHandlerWithState extends AbstractHandler
33         implements IObjectWithState, IStateListener {
34
35     /**
36      * The map of states currently held by this handler. If this handler has no
37      * state (generally, when inactive), then this will be <code>null</code>.
38      */

39     private Map JavaDoc states = null;
40
41     /**
42      * <p>
43      * Adds a state to this handler. This will add this handler as a listener to
44      * the state, and then fire a handleStateChange so that the handler can
45      * respond to the incoming state.
46      * </p>
47      * <p>
48      * Clients may extend this method, but they should call this super method
49      * first before doing anything else.
50      * </p>
51      *
52      * @param stateId
53      * The identifier indicating the type of state being added; must
54      * not be <code>null</code>.
55      * @param state
56      * The state to add; must not be <code>null</code>.
57      */

58     public void addState(final String JavaDoc stateId, final State state) {
59         if (state == null) {
60             throw new NullPointerException JavaDoc("Cannot add a null state"); //$NON-NLS-1$
61
}
62
63         if (states == null) {
64             states = new HashMap JavaDoc(3);
65         }
66         states.put(stateId, state);
67         state.addListener(this);
68         handleStateChange(state, null);
69     }
70
71     public final State getState(final String JavaDoc stateId) {
72         if ((states == null) || (states.isEmpty())) {
73             return null;
74         }
75
76         return (State) states.get(stateId);
77     }
78
79     public final String JavaDoc[] getStateIds() {
80         if ((states == null) || (states.isEmpty())) {
81             return null;
82         }
83
84         final Set JavaDoc stateIds = states.keySet();
85         return (String JavaDoc[]) stateIds.toArray(new String JavaDoc[stateIds.size()]);
86     }
87
88     /**
89      * <p>
90      * Removes a state from this handler. This will remove this handler as a
91      * listener to the state. No event is fired to notify the handler of this
92      * change.
93      * </p>
94      * <p>
95      * Clients may extend this method, but they should call this super method
96      * first before doing anything else.
97      * </p>
98      *
99      * @param stateId
100      * The identifier of the state to remove; must not be
101      * <code>null</code>.
102      */

103     public void removeState(final String JavaDoc stateId) {
104         if (stateId == null) {
105             throw new NullPointerException JavaDoc("Cannot remove a null state"); //$NON-NLS-1$
106
}
107
108         final State state = (State) states.get(stateId);
109         if (state != null) {
110             state.removeListener(this);
111             if (states != null) {
112                 states.remove(state);
113                 if (states.isEmpty()) {
114                     states = null;
115                 }
116             }
117         }
118     }
119 }
120
Popular Tags