KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.eclipse.core.commands.common.NamedHandleObject;
19 import org.eclipse.core.commands.common.NotDefinedException;
20
21 /**
22  * <p>
23  * A named handle object that can carry state with it. This state can be used to
24  * override the name or description.
25  * </p>
26  * <p>
27  * Clients may neither instantiate nor extend this class.
28  * </p>
29  *
30  * @since 3.2
31  */

32 abstract class NamedHandleObjectWithState extends NamedHandleObject implements
33         IObjectWithState {
34
35     /**
36      * An empty string array, which can be returned from {@link #getStateIds()}
37      * if there is no state.
38      */

39     private static final String JavaDoc[] NO_STATE = new String JavaDoc[0];
40
41     /**
42      * The map of states currently held by this command. If this command has no
43      * state, then this will be <code>null</code>.
44      */

45     private Map JavaDoc states = null;
46
47     /**
48      * Constructs a new instance of <code>NamedHandleObject<WithState/code>.
49      *
50      * @param id
51      * The identifier for this handle; must not be <code>null</code>.
52      */

53     protected NamedHandleObjectWithState(final String JavaDoc id) {
54         super(id);
55     }
56
57     public void addState(final String JavaDoc stateId, final State state) {
58         if (state == null) {
59             throw new NullPointerException JavaDoc("Cannot add a null state"); //$NON-NLS-1$
60
}
61
62         if (states == null) {
63             states = new HashMap JavaDoc(3);
64         }
65         states.put(stateId, state);
66     }
67
68     public final String JavaDoc getDescription() throws NotDefinedException {
69         final String JavaDoc description = super.getDescription(); // Trigger a NDE.
70

71         final State descriptionState = getState(INamedHandleStateIds.DESCRIPTION);
72         if (descriptionState != null) {
73             final Object JavaDoc value = descriptionState.getValue();
74             if (value != null) {
75                 return value.toString();
76             }
77         }
78
79         return description;
80     }
81
82     public final String JavaDoc getName() throws NotDefinedException {
83         final String JavaDoc name = super.getName(); // Trigger a NDE, if necessary.
84

85         final State nameState = getState(INamedHandleStateIds.NAME);
86         if (nameState != null) {
87             final Object JavaDoc value = nameState.getValue();
88             if (value != null) {
89                 return value.toString();
90             }
91         }
92
93         return name;
94     }
95
96     public final State getState(final String JavaDoc stateId) {
97         if ((states == null) || (states.isEmpty())) {
98             return null;
99         }
100
101         return (State) states.get(stateId);
102     }
103
104     public final String JavaDoc[] getStateIds() {
105         if ((states == null) || (states.isEmpty())) {
106             return NO_STATE;
107         }
108
109         final Set JavaDoc stateIds = states.keySet();
110         return (String JavaDoc[]) stateIds.toArray(new String JavaDoc[stateIds.size()]);
111     }
112
113     public void removeState(final String JavaDoc id) {
114         if (id == null) {
115             throw new NullPointerException JavaDoc("Cannot remove a null id"); //$NON-NLS-1$
116
}
117
118         if (states != null) {
119             states.remove(id);
120             if (states.isEmpty()) {
121                 states = null;
122             }
123         }
124     }
125
126 }
127
Popular Tags