Mercurial > projects > dwt2
comparison org.eclipse.core.commands/src/org/eclipse/core/commands/ParameterType.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 | 6f068362a363 |
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 module org.eclipse.core.commands.ParameterType; | |
14 | |
15 import org.eclipse.core.commands.common.HandleObject; | |
16 import org.eclipse.core.commands.common.NotDefinedException; | |
17 import org.eclipse.core.internal.commands.util.Util; | |
18 import org.eclipse.core.commands.AbstractParameterValueConverter; | |
19 import org.eclipse.core.commands.IParameterTypeListener; | |
20 import org.eclipse.core.commands.ParameterTypeEvent; | |
21 | |
22 import java.lang.all; | |
23 import tango.text.convert.Format; | |
24 | |
25 /** | |
26 * <p> | |
27 * Provides information about the type of a command parameter. Clients can use a | |
28 * parameter type to check if an object matches the type of the parameter with | |
29 * {@link #isCompatible(Object)} and can get an | |
30 * {@link AbstractParameterValueConverter} to convert between objects matching | |
31 * the parameter type and strings that encode the object's identity. | |
32 * </p> | |
33 * <p> | |
34 * A command parameter is not required to declare a type. To determine if a | |
35 * given parameter has a type, check if an {@link IParameter} implements | |
36 * {@link ITypedParameter} and if so, use | |
37 * {@link ITypedParameter#getParameterType()} like this: | |
38 * </p> | |
39 * | |
40 * <pre> | |
41 * IParameter parameter = // ... get IParameter from Command | |
42 * if (parameter instanceof ITypedParameter) { | |
43 * ParameterType type = ((ITypedParameter)parameter).getParameterType(); | |
44 * if (type !is null) { | |
45 * // this parameter has a ParameterType | |
46 * } | |
47 * } | |
48 * </pre> | |
49 * | |
50 * @see IParameter | |
51 * @see ITypedParameter#getParameterType() | |
52 * @since 3.2 | |
53 */ | |
54 public final class ParameterType : HandleObject, Comparable { | |
55 | |
56 /** | |
57 * TODO: this was copied from | |
58 * org.eclipse.core.internal.expressions.Expressions is there a better place | |
59 * to reference this? | |
60 * | |
61 * @param element | |
62 * The element to test; may be <code>null</code>. | |
63 * @param type | |
64 * The type against which we are testing;may be <code>null</code>. | |
65 * @return <code>true</code> if the <code>element</code> is an instance | |
66 * of <code>type</code>; <code>false</code> otherwise. | |
67 */ | |
68 private static final bool isInstanceOf(Object element, | |
69 String type) { | |
70 // null isn't an instanceof of anything. | |
71 if (element is null) { | |
72 return false; | |
73 } | |
74 return isSubtype(element.classinfo, type); | |
75 } | |
76 | |
77 /** | |
78 * TODO: this was copied from | |
79 * org.eclipse.core.internal.expressions.Expressions is there a better place | |
80 * to reference this? | |
81 * | |
82 * @param clazz | |
83 * The class to match; may be <code>null</code>. | |
84 * @param type | |
85 * The type against which we are testing;may be <code>null</code>. | |
86 * @return <code>true</code> if the <code>element</code> is an instance | |
87 * of <code>type</code>; <code>false</code> otherwise. | |
88 */ | |
89 private static final bool isSubtype(ClassInfo clazz, String type) { | |
90 if (clazz.name.equals(type)) { | |
91 return true; | |
92 } | |
93 ClassInfo superClass = clazz.base; | |
94 if (superClass !is null && isSubtype(superClass, type)) { | |
95 return true; | |
96 } | |
97 Interface[] interfaces = clazz.interfaces; | |
98 for (int i = 0; i < interfaces.length; i++) { | |
99 if (isSubtype(interfaces[i].classinfo, type)) { | |
100 return true; | |
101 } | |
102 } | |
103 return false; | |
104 } | |
105 | |
106 /** | |
107 * An {@link AbstractParameterValueConverter} for converting parameter | |
108 * values between objects and strings. This may be <code>null</code>. | |
109 */ | |
110 private /+transient+/ AbstractParameterValueConverter parameterTypeConverter; | |
111 | |
112 /** | |
113 * A string specifying the object type of this parameter type. This will be | |
114 * <code>null</code> when the parameter type is undefined but never null | |
115 * when it is defined. | |
116 */ | |
117 private /+transient+/ String type = null; | |
118 | |
119 /** | |
120 * Constructs a new instance based on the given identifier. When a parameter | |
121 * type is first constructed, it is undefined. Parameter types should only | |
122 * be constructed by the {@link CommandManager} to ensure that the | |
123 * identifier remains unique. | |
124 * | |
125 * @param id | |
126 * The identifier for this type. This value must not be | |
127 * <code>null</code>, and must be unique amongst all parameter | |
128 * types. | |
129 */ | |
130 this(String id) { | |
131 super(id); | |
132 } | |
133 | |
134 /** | |
135 * Adds a listener to this parameter type that will be notified when its | |
136 * state changes. | |
137 * | |
138 * @param listener | |
139 * The listener to be added; must not be <code>null</code>. | |
140 */ | |
141 public final void addListener(IParameterTypeListener listener) { | |
142 addListenerObject(cast(Object)listener); | |
143 } | |
144 | |
145 /** | |
146 * Compares this parameter type with another object by comparing each of the | |
147 * non-transient attributes. | |
148 * | |
149 * @param object | |
150 * The object with which to compare; must be an instance of | |
151 * {@link ParameterType}. | |
152 * @return A negative integer, zero or a positive integer, if the object is | |
153 * greater than, equal to or less than this parameter type. | |
154 */ | |
155 public final int compareTo(Object object) { | |
156 ParameterType castedObject = cast(ParameterType) object; | |
157 int compareTo = Util.compare(defined, castedObject.defined); | |
158 if (compareTo is 0) { | |
159 compareTo = Util.compare(id, castedObject.id); | |
160 } | |
161 return compareTo; | |
162 } | |
163 | |
164 /** | |
165 * <p> | |
166 * Defines this parameter type, setting the defined property to | |
167 * <code>true</code>. | |
168 * </p> | |
169 * <p> | |
170 * Notification is sent to all listeners that something has changed. | |
171 * </p> | |
172 * | |
173 * @param type | |
174 * a string identifying the Java object type for this parameter | |
175 * type; <code>null</code> is interpreted as | |
176 * <code>"java.lang.Object"</code> | |
177 * @param parameterTypeConverter | |
178 * an {@link AbstractParameterValueConverter} to perform | |
179 * string/object conversions for parameter values; may be | |
180 * <code>null</code> | |
181 */ | |
182 public final void define(String type, | |
183 AbstractParameterValueConverter parameterTypeConverter) { | |
184 | |
185 bool definedChanged = !this.defined; | |
186 this.defined = true; | |
187 | |
188 this.type = (type is null) ? Object.classinfo.name : type; | |
189 this.parameterTypeConverter = parameterTypeConverter; | |
190 | |
191 fireParameterTypeChanged(new ParameterTypeEvent(this, definedChanged)); | |
192 } | |
193 | |
194 /** | |
195 * Notifies all listeners that this parameter type has changed. This sends | |
196 * the given event to all of the listeners, if any. | |
197 * | |
198 * @param event | |
199 * The event to send to the listeners; must not be | |
200 * <code>null</code>. | |
201 */ | |
202 private final void fireParameterTypeChanged(ParameterTypeEvent event) { | |
203 if (event is null) { | |
204 throw new NullPointerException( | |
205 "Cannot send a null event to listeners."); //$NON-NLS-1$ | |
206 } | |
207 | |
208 if (!isListenerAttached()) { | |
209 return; | |
210 } | |
211 | |
212 Object[] listeners = getListeners(); | |
213 for (int i = 0; i < listeners.length; i++) { | |
214 IParameterTypeListener listener = cast(IParameterTypeListener) listeners[i]; | |
215 listener.parameterTypeChanged(event); | |
216 } | |
217 } | |
218 | |
219 /** | |
220 * Returns the value converter associated with this parameter, if any. | |
221 * | |
222 * @return The parameter value converter, or <code>null</code> if there is | |
223 * no value converter for this parameter. | |
224 * @throws NotDefinedException | |
225 * if the parameter type is not currently defined | |
226 */ | |
227 public final AbstractParameterValueConverter getValueConverter() { | |
228 if (!isDefined()) { | |
229 throw new NotDefinedException( | |
230 "Cannot use getValueConverter() with an undefined ParameterType"); //$NON-NLS-1$ | |
231 } | |
232 | |
233 return parameterTypeConverter; | |
234 } | |
235 | |
236 /** | |
237 * Returns whether the provided value is compatible with this parameter | |
238 * type. An object is compatible with a parameter type if the object is an | |
239 * instance of the class defined as the parameter's type class. | |
240 * | |
241 * @param value | |
242 * an object to check for compatibility with this parameter type; | |
243 * may be <code>null</code>. | |
244 * @return <code>true</code> if the value is compatible with this type, | |
245 * <code>false</code> otherwise | |
246 * @throws NotDefinedException | |
247 * if the parameter type is not currently defined | |
248 */ | |
249 public bool isCompatible(Object value) { | |
250 if (!isDefined()) { | |
251 throw new NotDefinedException( | |
252 "Cannot use isCompatible() with an undefined ParameterType"); //$NON-NLS-1$ | |
253 } | |
254 return isInstanceOf(value, type); | |
255 } | |
256 | |
257 /** | |
258 * Unregisters listener for changes to properties of this parameter type. | |
259 * | |
260 * @param listener | |
261 * the instance to unregister. Must not be <code>null</code>. | |
262 * If an attempt is made to unregister an instance which is not | |
263 * already registered with this instance, no operation is | |
264 * performed. | |
265 */ | |
266 public final void removeListener(IParameterTypeListener listener) { | |
267 removeListenerObject(cast(Object)listener); | |
268 } | |
269 | |
270 /** | |
271 * The string representation of this parameter type. For debugging purposes | |
272 * only. This string should not be shown to an end user. | |
273 * | |
274 * @return The string representation; never <code>null</code>. | |
275 */ | |
276 public override final String toString() { | |
277 if (string is null) { | |
278 string = Format( "ParameterType({},{})", id, defined ); | |
279 } | |
280 return string; | |
281 } | |
282 | |
283 /** | |
284 * Makes this parameter type become undefined. Notification is sent to all | |
285 * listeners. | |
286 */ | |
287 public override final void undefine() { | |
288 string = null; | |
289 | |
290 final bool definedChanged = defined; | |
291 defined = false; | |
292 | |
293 type = null; | |
294 parameterTypeConverter = null; | |
295 | |
296 fireParameterTypeChanged(new ParameterTypeEvent(this, definedChanged)); | |
297 } | |
298 | |
299 } |