Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/fieldassist/FieldDecorationRegistry.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) 2006, 2007 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.jface.fieldassist.FieldDecorationRegistry; | |
14 | |
15 import org.eclipse.jface.fieldassist.FieldDecoration; | |
16 | |
17 | |
18 import org.eclipse.swt.graphics.Image; | |
19 import org.eclipse.jface.resource.ImageDescriptor; | |
20 import org.eclipse.jface.resource.ImageRegistry; | |
21 import org.eclipse.jface.resource.JFaceResources; | |
22 | |
23 import java.lang.all; | |
24 import java.util.Iterator; | |
25 import java.util.HashMap; | |
26 import java.util.Set; | |
27 | |
28 /** | |
29 * FieldDecorationRegistry is a common registry used to define shared field | |
30 * decorations within an application. Unlike resource registries, the | |
31 * FieldDecorationRegistry does not perform any lifecycle management of the | |
32 * decorations. | |
33 * </p> | |
34 * <p> | |
35 * Clients may specify images for the decorations in several different ways. | |
36 * Images may be described by their image id in a specified | |
37 * {@link ImageRegistry}. In this case, the life cycle of the image is managed | |
38 * by the image registry, and the decoration registry will not attempt to obtain | |
39 * an image from the image registry until the decoration is actually requested. | |
40 * In cases where the client has access to an already-created image, the image | |
41 * itself can be specified when registering the decoration. In this case, the | |
42 * life cycle should be managed by the specifying client. | |
43 * </p> | |
44 * | |
45 * @see FieldDecoration | |
46 * @see ImageRegistry | |
47 * | |
48 * @since 3.2 | |
49 */ | |
50 public class FieldDecorationRegistry { | |
51 | |
52 /** | |
53 * Decoration id for the decoration that should be used to cue the user that | |
54 * content proposals are available. | |
55 */ | |
56 public static const String DEC_CONTENT_PROPOSAL = "DEC_CONTENT_PROPOSAL"; //$NON-NLS-1$ | |
57 | |
58 /** | |
59 * Decoration id for the decoration that should be used to cue the user that | |
60 * a field is required. | |
61 */ | |
62 public static const String DEC_REQUIRED = "DEC_REQUIRED"; //$NON-NLS-1$ | |
63 | |
64 /** | |
65 * Decoration id for the decoration that should be used to cue the user that | |
66 * a field has an error. | |
67 */ | |
68 public static const String DEC_ERROR = "DEC_ERROR"; //$NON-NLS-1$ | |
69 | |
70 /** | |
71 * Decoration id for the decoration that should be used to cue the user that | |
72 * a field has a warning. | |
73 */ | |
74 public static const String DEC_WARNING = "DEC_WARNING"; //$NON-NLS-1$ | |
75 | |
76 /** | |
77 * Decoration id for the decoration that should be used to cue the user that | |
78 * a field has additional information. | |
79 * | |
80 * @since 3.3 | |
81 */ | |
82 public static const String DEC_INFORMATION = "DEC_INFORMATION"; //$NON-NLS-1$ | |
83 | |
84 /** | |
85 * Decoration id for the decoration that should be used to cue the user that | |
86 * a field has an error with quick fix available. | |
87 * | |
88 * @since 3.3 | |
89 */ | |
90 public static const String DEC_ERROR_QUICKFIX = "DEC_ERRORQUICKFIX"; //$NON-NLS-1$ | |
91 | |
92 /* | |
93 * Image id's | |
94 */ | |
95 private static const String IMG_DEC_FIELD_CONTENT_PROPOSAL = "org.eclipse.jface.fieldassist.IMG_DEC_FIELD_CONTENT_PROPOSAL"; //$NON-NLS-1$ | |
96 | |
97 private static const String IMG_DEC_FIELD_REQUIRED = "org.eclipse.jface.fieldassist.IMG_DEC_FIELD_REQUIRED"; //$NON-NLS-1$ | |
98 | |
99 private static const String IMG_DEC_FIELD_ERROR = "org.eclipse.jface.fieldassist.IMG_DEC_FIELD_ERROR"; //$NON-NLS-1$ | |
100 | |
101 private static const String IMG_DEC_FIELD_ERROR_QUICKFIX = "org.eclipse.jface.fieldassist.IMG_DEC_FIELD_ERROR_QUICKFIX"; //$NON-NLS-1$ | |
102 | |
103 private static const String IMG_DEC_FIELD_WARNING = "org.eclipse.jface.fieldassist.IMG_DEC_FIELD_WARNING"; //$NON-NLS-1$ | |
104 | |
105 private static const String IMG_DEC_FIELD_INFO = "org.eclipse.jface.fieldassist.IMG_DEC_FIELD_INFO"; //$NON-NLS-1$ | |
106 | |
107 /* | |
108 * Declare images and decorations immediately. | |
109 */ | |
110 private static void static_this(FieldDecorationRegistry reg) { | |
111 ImageRegistry imageRegistry = JFaceResources.getImageRegistry(); | |
112 | |
113 // Define the images used in the standard decorations. | |
114 imageRegistry.put(IMG_DEC_FIELD_CONTENT_PROPOSAL, ImageDescriptor.createFromFile( | |
115 getImportData!("org.eclipse.jface.fieldassist.images.contassist_ovr.gif")));//$NON-NLS-1$ | |
116 imageRegistry.put(IMG_DEC_FIELD_ERROR, ImageDescriptor.createFromFile( | |
117 getImportData!("org.eclipse.jface.fieldassist.images.error_ovr.gif")));//$NON-NLS-1$ | |
118 | |
119 imageRegistry.put(IMG_DEC_FIELD_WARNING, ImageDescriptor.createFromFile( | |
120 getImportData!("org.eclipse.jface.fieldassist.images.warn_ovr.gif")));//$NON-NLS-1$ | |
121 | |
122 imageRegistry.put(IMG_DEC_FIELD_REQUIRED, ImageDescriptor.createFromFile( | |
123 getImportData!("org.eclipse.jface.fieldassist.images.required_field_cue.gif")));//$NON-NLS-1$ | |
124 | |
125 imageRegistry.put(IMG_DEC_FIELD_ERROR_QUICKFIX, ImageDescriptor.createFromFile( | |
126 getImportData!("org.eclipse.jface.fieldassist.images.errorqf_ovr.gif")));//$NON-NLS-1$ | |
127 | |
128 imageRegistry.put(IMG_DEC_FIELD_INFO, ImageDescriptor.createFromFile( | |
129 getImportData!("org.eclipse.jface.fieldassist.images.info_ovr.gif")));//$NON-NLS-1$ | |
130 | |
131 // Define the standard decorations. Some do not have standard | |
132 // descriptions. Use null in these cases. | |
133 reg | |
134 .registerFieldDecoration( | |
135 DEC_CONTENT_PROPOSAL, | |
136 JFaceResources | |
137 .getString("FieldDecorationRegistry.contentAssistMessage"), //$NON-NLS-1$ | |
138 IMG_DEC_FIELD_CONTENT_PROPOSAL, imageRegistry); | |
139 | |
140 reg.registerFieldDecoration( | |
141 DEC_ERROR, | |
142 JFaceResources | |
143 .getString("FieldDecorationRegistry.errorMessage"), //$NON-NLS-1$ | |
144 IMG_DEC_FIELD_ERROR, imageRegistry); | |
145 | |
146 reg.registerFieldDecoration( | |
147 DEC_ERROR_QUICKFIX, | |
148 JFaceResources | |
149 .getString("FieldDecorationRegistry.errorQuickFixMessage"), //$NON-NLS-1$ | |
150 IMG_DEC_FIELD_ERROR_QUICKFIX, imageRegistry); | |
151 | |
152 reg.registerFieldDecoration(DEC_WARNING, null, | |
153 IMG_DEC_FIELD_WARNING, imageRegistry); | |
154 | |
155 reg.registerFieldDecoration(DEC_INFORMATION, null, | |
156 IMG_DEC_FIELD_INFO, imageRegistry); | |
157 | |
158 reg | |
159 .registerFieldDecoration( | |
160 DEC_REQUIRED, | |
161 JFaceResources | |
162 .getString("FieldDecorationRegistry.requiredFieldMessage"), //$NON-NLS-1$ | |
163 IMG_DEC_FIELD_REQUIRED, imageRegistry); | |
164 | |
165 } | |
166 | |
167 /* | |
168 * Data structure that holds onto the decoration image info and description, | |
169 * and can produce a decorator on request. | |
170 */ | |
171 class Entry { | |
172 private String description; | |
173 | |
174 private String imageId; | |
175 | |
176 private ImageRegistry imageRegistry; | |
177 | |
178 private Image image; | |
179 | |
180 private FieldDecoration decoration; | |
181 | |
182 this(String description, String imageId, ImageRegistry registry) { | |
183 this.description = description; | |
184 this.imageId = imageId; | |
185 this.imageRegistry = registry; | |
186 } | |
187 | |
188 this(String description, Image image) { | |
189 this.description = description; | |
190 this.image = image; | |
191 } | |
192 | |
193 FieldDecoration getDecoration() { | |
194 if (decoration is null) { | |
195 if (image is null) { | |
196 if (imageRegistry is null) { | |
197 imageRegistry = JFaceResources.getImageRegistry(); | |
198 } | |
199 image = imageRegistry.get(imageId); | |
200 } | |
201 decoration = new FieldDecoration(image, description); | |
202 } | |
203 // Null out all other fields now that the decoration has an image | |
204 description = null; | |
205 imageId = null; | |
206 imageRegistry = null; | |
207 image = null; | |
208 | |
209 return decoration; | |
210 } | |
211 } | |
212 | |
213 /** | |
214 * Default instance of the registry. Applications may install their own | |
215 * registry. | |
216 */ | |
217 private static FieldDecorationRegistry defaultInstance; | |
218 | |
219 /** | |
220 * Maximum width and height used by decorations in this registry. Clients | |
221 * may use these values to reserve space in dialogs for decorations or to | |
222 * adjust layouts so that decorated and non-decorated fields line up. | |
223 */ | |
224 private int maxDecorationWidth = 0; | |
225 private int maxDecorationHeight = 0; | |
226 | |
227 private HashMap /* <String id, FieldDecoration> */decorations; | |
228 | |
229 /** | |
230 * Get the default FieldDecorationRegistry. | |
231 * | |
232 * @return the singleton FieldDecorationRegistry that is used to manage | |
233 * shared field decorations. | |
234 */ | |
235 public static FieldDecorationRegistry getDefault() { | |
236 if (defaultInstance is null) { | |
237 synchronized(FieldDecorationRegistry.classinfo){ | |
238 if (defaultInstance is null) { | |
239 defaultInstance = new FieldDecorationRegistry(); | |
240 static_this(defaultInstance); | |
241 } | |
242 } | |
243 } | |
244 return defaultInstance; | |
245 } | |
246 | |
247 /** | |
248 * Set the default FieldDecorationRegistry. | |
249 * | |
250 * @param defaultRegistry | |
251 * the singleton FieldDecorationRegistry that is used to manage | |
252 * shared field decorations. | |
253 */ | |
254 public static void setDefault(FieldDecorationRegistry defaultRegistry) { | |
255 defaultInstance = defaultRegistry; | |
256 } | |
257 | |
258 /** | |
259 * Construct a FieldDecorationRegistry. | |
260 */ | |
261 public this() { | |
262 decorations = new HashMap(); | |
263 maxDecorationWidth = 0; | |
264 maxDecorationHeight = 0; | |
265 } | |
266 | |
267 /** | |
268 * Get the maximum width (in pixels) of any decoration retrieved so far in | |
269 * the registry. This value changes as decorations are added and retrieved. | |
270 * This value can be used by clients to reserve space or otherwise compute | |
271 * margins when aligning non-decorated fields with decorated fields. | |
272 * | |
273 * @return the maximum width in pixels of any accessed decoration | |
274 */ | |
275 public int getMaximumDecorationWidth() { | |
276 return maxDecorationWidth; | |
277 } | |
278 | |
279 /** | |
280 * Get the maximum height (in pixels) of any decoration retrieved so far in | |
281 * the registry. This value changes as decorations are added and retrieved. | |
282 * This value can be used by clients to reserve space or otherwise compute | |
283 * margins when aligning non-decorated fields with decorated fields. | |
284 * | |
285 * | |
286 * @return the maximum height in pixels of any accessed decoration | |
287 */ | |
288 public int getMaximumDecorationHeight() { | |
289 return maxDecorationHeight; | |
290 } | |
291 | |
292 /** | |
293 * Registers a field decoration using the specified id. The lifecyle of the | |
294 * supplied image should be managed by the client. That is, it will never be | |
295 * disposed by this registry and the decoration should be removed from the | |
296 * registry if the image is ever disposed elsewhere. | |
297 * | |
298 * @param id | |
299 * the String id used to identify and access the decoration. | |
300 * @param description | |
301 * the String description to be used in the decoration, or | |
302 * <code>null</code> if the decoration has no description. | |
303 * @param image | |
304 * the image to be used in the decoration | |
305 */ | |
306 public void registerFieldDecoration(String id, String description, | |
307 Image image) { | |
308 decorations.put(stringcast(id), new Entry(description, image)); | |
309 // Recompute the maximums since this might be a replacement | |
310 recomputeMaximums(); | |
311 } | |
312 | |
313 /** | |
314 * Registers a field decoration using the specified id. An image id of an | |
315 * image located in the default JFaceResources image registry is supplied. | |
316 * The image will not be created until the decoration is requested. | |
317 * | |
318 * @param id | |
319 * the String id used to identify and access the decoration. | |
320 * @param description | |
321 * the String description to be used in the decoration, or | |
322 * <code>null</code> if the decoration has no description. * | |
323 * @param imageId | |
324 * the id of the image in the JFaceResources image registry that | |
325 * is used for this decorator | |
326 */ | |
327 public void registerFieldDecoration(String id, String description, | |
328 String imageId) { | |
329 decorations.put(stringcast(id), new Entry(description, imageId, JFaceResources | |
330 .getImageRegistry())); | |
331 // Recompute the maximums as this could be a replacement of a previous | |
332 // image. | |
333 recomputeMaximums(); | |
334 } | |
335 | |
336 /** | |
337 * Registers a field decoration using the specified id. An image id and an | |
338 * image registry are supplied. The image will not be created until the | |
339 * decoration is requested. | |
340 * | |
341 * @param id | |
342 * the String id used to identify and access the decoration. | |
343 * @param description | |
344 * the String description to be used in the decoration, or | |
345 * <code>null</code> if the decoration has no description. * | |
346 * @param imageId | |
347 * the id of the image in the supplied image registry that is | |
348 * used for this decorator | |
349 * @param imageRegistry | |
350 * the registry used to obtain the image | |
351 */ | |
352 public void registerFieldDecoration(String id, String description, | |
353 String imageId, ImageRegistry imageRegistry) { | |
354 decorations.put(stringcast(id), new Entry(description, imageId, imageRegistry)); | |
355 // Recompute the maximums since this could be a replacement | |
356 recomputeMaximums(); | |
357 } | |
358 | |
359 /** | |
360 * Unregisters the field decoration with the specified id. No lifecycle | |
361 * management is performed on the decoration's image. This message has no | |
362 * effect if no field decoration with the specified id was previously | |
363 * registered. | |
364 * </p> | |
365 * <p> | |
366 * This method need not be called if the registered decoration's image is | |
367 * managed in an image registry. In that case, leaving the decoration in the | |
368 * registry will do no harm since the image will remain valid and will be | |
369 * properly disposed when the application is shut down. This method should | |
370 * be used in cases where the caller intends to dispose of the image | |
371 * referred to by the decoration, or otherwise determines that the | |
372 * decoration should no longer be used. | |
373 * | |
374 * @param id | |
375 * the String id of the decoration to be unregistered. | |
376 */ | |
377 public void unregisterFieldDecoration(String id) { | |
378 decorations.remove(stringcast(id)); | |
379 recomputeMaximums(); | |
380 } | |
381 | |
382 /** | |
383 * Returns the field decoration registered by the specified id . | |
384 * | |
385 * @param id | |
386 * the String id used to access the decoration. | |
387 * @return the FieldDecoration with the specified id, or <code>null</code> | |
388 * if there is no decoration with the specified id. | |
389 */ | |
390 public FieldDecoration getFieldDecoration(String id) { | |
391 Object entry = decorations.get(stringcast(id)); | |
392 if (entry is null) { | |
393 return null; | |
394 } | |
395 return (cast(Entry) entry).getDecoration(); | |
396 | |
397 } | |
398 | |
399 /* | |
400 * The maximum decoration width and height must be recomputed. Typically | |
401 * called in response to adding, removing, or replacing a decoration. | |
402 */ | |
403 private void recomputeMaximums() { | |
404 Iterator entries = decorations.values().iterator(); | |
405 | |
406 maxDecorationHeight = 0; | |
407 maxDecorationWidth = 0; | |
408 while (entries.hasNext()) { | |
409 Image image = (cast(Entry)entries.next()).getDecoration().getImage(); | |
410 if (image !is null) { | |
411 maxDecorationHeight = Math.max(maxDecorationHeight, image.getBounds().height); | |
412 maxDecorationWidth = Math.max(maxDecorationWidth, image.getBounds().width); | |
413 } | |
414 } | |
415 | |
416 } | |
417 } |