comparison org.eclipse.core.commands/src/org/eclipse/core/commands/NamedHandleObjectWithState.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13
14 module org.eclipse.core.commands.NamedHandleObjectWithState;
15
16 import org.eclipse.core.commands.common.NamedHandleObject;
17 import org.eclipse.core.commands.common.NotDefinedException;
18 import org.eclipse.core.commands.IObjectWithState;
19 import org.eclipse.core.commands.State;
20 import org.eclipse.core.commands.INamedHandleStateIds;
21
22 import java.lang.all;
23 import java.util.Map;
24 import java.util.HashMap;
25 import java.util.Set;
26
27 /**
28 * <p>
29 * A named handle object that can carry state with it. This state can be used to
30 * override the name or description.
31 * </p>
32 * <p>
33 * Clients may neither instantiate nor extend this class.
34 * </p>
35 *
36 * @since 3.2
37 */
38 abstract class NamedHandleObjectWithState : NamedHandleObject,
39 IObjectWithState {
40
41 /**
42 * An empty string array, which can be returned from {@link #getStateIds()}
43 * if there is no state.
44 */
45 private static const String[] NO_STATE = null;
46
47 /**
48 * The map of states currently held by this command. If this command has no
49 * state, then this will be <code>null</code>.
50 */
51 private Map states = null;
52
53 /**
54 * Constructs a new instance of <code>NamedHandleObject<WithState/code>.
55 *
56 * @param id
57 * The identifier for this handle; must not be <code>null</code>.
58 */
59 protected this(String id) {
60 super(id);
61 }
62
63 public void addState(String stateId, State state) {
64 if (state is null) {
65 throw new NullPointerException("Cannot add a null state"); //$NON-NLS-1$
66 }
67
68 if (states is null) {
69 states = new HashMap(3);
70 }
71 states.put(stateId, state);
72 }
73
74 public override final String getDescription() {
75 String description = super.getDescription(); // Trigger a NDE.
76
77 State descriptionState = getState(INamedHandleStateIds.DESCRIPTION);
78 if (descriptionState !is null) {
79 Object value = descriptionState.getValue();
80 if (value !is null) {
81 return value.toString();
82 }
83 }
84
85 return description;
86 }
87
88 public override final String getName() {
89 String name = super.getName(); // Trigger a NDE, if necessary.
90
91 State nameState = getState(INamedHandleStateIds.NAME);
92 if (nameState !is null) {
93 final Object value = nameState.getValue();
94 if (value !is null) {
95 return value.toString();
96 }
97 }
98
99 return name;
100 }
101
102 public final State getState(String stateId) {
103 if ((states is null) || (states.isEmpty())) {
104 return null;
105 }
106
107 return cast(State) states.get(stateId);
108 }
109
110 public final String[] getStateIds() {
111 if ((states is null) || (states.isEmpty())) {
112 return NO_STATE;
113 }
114
115 Set stateIds = states.keySet();
116 return stringcast( stateIds.toArray());
117 }
118
119 public void removeState(String id) {
120 if (id is null) {
121 throw new NullPointerException("Cannot remove a null id"); //$NON-NLS-1$
122 }
123
124 if (states !is null) {
125 states.remove(id);
126 if (states.isEmpty()) {
127 states = null;
128 }
129 }
130 }
131
132 }