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