1 11 package org.eclipse.jface.fieldassist; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 16 import org.eclipse.jface.resource.ImageDescriptor; 17 import org.eclipse.jface.resource.ImageRegistry; 18 import org.eclipse.jface.resource.JFaceResources; 19 import org.eclipse.swt.graphics.Image; 20 21 43 public class FieldDecorationRegistry { 44 45 49 public static final String DEC_CONTENT_PROPOSAL = "DEC_CONTENT_PROPOSAL"; 51 55 public static final String DEC_REQUIRED = "DEC_REQUIRED"; 57 61 public static final String DEC_ERROR = "DEC_ERROR"; 63 67 public static final String DEC_WARNING = "DEC_WARNING"; 69 75 public static final String DEC_INFORMATION = "DEC_INFORMATION"; 77 83 public static final String DEC_ERROR_QUICKFIX = "DEC_ERRORQUICKFIX"; 85 88 private static final String IMG_DEC_FIELD_CONTENT_PROPOSAL = "org.eclipse.jface.fieldassist.IMG_DEC_FIELD_CONTENT_PROPOSAL"; 90 private static final String IMG_DEC_FIELD_REQUIRED = "org.eclipse.jface.fieldassist.IMG_DEC_FIELD_REQUIRED"; 92 private static final String IMG_DEC_FIELD_ERROR = "org.eclipse.jface.fieldassist.IMG_DEC_FIELD_ERROR"; 94 private static final String IMG_DEC_FIELD_ERROR_QUICKFIX = "org.eclipse.jface.fieldassist.IMG_DEC_FIELD_ERROR_QUICKFIX"; 96 private static final String IMG_DEC_FIELD_WARNING = "org.eclipse.jface.fieldassist.IMG_DEC_FIELD_WARNING"; 98 private static final String IMG_DEC_FIELD_INFO = "org.eclipse.jface.fieldassist.IMG_DEC_FIELD_INFO"; 100 103 static { 104 ImageRegistry imageRegistry = JFaceResources.getImageRegistry(); 105 106 imageRegistry.put(IMG_DEC_FIELD_CONTENT_PROPOSAL, ImageDescriptor 108 .createFromFile(FieldDecorationRegistry.class, 109 "images/contassist_ovr.gif")); imageRegistry.put(IMG_DEC_FIELD_ERROR, ImageDescriptor.createFromFile( 111 FieldDecorationRegistry.class, "images/error_ovr.gif")); 113 imageRegistry.put(IMG_DEC_FIELD_WARNING, ImageDescriptor 114 .createFromFile(FieldDecorationRegistry.class, 115 "images/warn_ovr.gif")); 117 imageRegistry.put(IMG_DEC_FIELD_REQUIRED, ImageDescriptor 118 .createFromFile(FieldDecorationRegistry.class, 119 "images/required_field_cue.gif")); 121 imageRegistry.put(IMG_DEC_FIELD_ERROR_QUICKFIX, ImageDescriptor 122 .createFromFile(FieldDecorationRegistry.class, 123 "images/errorqf_ovr.gif")); 125 imageRegistry.put(IMG_DEC_FIELD_INFO, ImageDescriptor 126 .createFromFile(FieldDecorationRegistry.class, 127 "images/info_ovr.gif")); 129 getDefault() 132 .registerFieldDecoration( 133 DEC_CONTENT_PROPOSAL, 134 JFaceResources 135 .getString("FieldDecorationRegistry.contentAssistMessage"), IMG_DEC_FIELD_CONTENT_PROPOSAL, imageRegistry); 137 138 getDefault().registerFieldDecoration( 139 DEC_ERROR, 140 JFaceResources 141 .getString("FieldDecorationRegistry.errorMessage"), IMG_DEC_FIELD_ERROR, imageRegistry); 143 144 getDefault().registerFieldDecoration( 145 DEC_ERROR_QUICKFIX, 146 JFaceResources 147 .getString("FieldDecorationRegistry.errorQuickFixMessage"), IMG_DEC_FIELD_ERROR_QUICKFIX, imageRegistry); 149 150 getDefault().registerFieldDecoration(DEC_WARNING, null, 151 IMG_DEC_FIELD_WARNING, imageRegistry); 152 153 getDefault().registerFieldDecoration(DEC_INFORMATION, null, 154 IMG_DEC_FIELD_INFO, imageRegistry); 155 156 getDefault() 157 .registerFieldDecoration( 158 DEC_REQUIRED, 159 JFaceResources 160 .getString("FieldDecorationRegistry.requiredFieldMessage"), IMG_DEC_FIELD_REQUIRED, imageRegistry); 162 163 } 164 165 169 class Entry { 170 private String description; 171 172 private String imageId; 173 174 private ImageRegistry imageRegistry; 175 176 private Image image; 177 178 private FieldDecoration decoration; 179 180 Entry(String description, String imageId, ImageRegistry registry) { 181 this.description = description; 182 this.imageId = imageId; 183 this.imageRegistry = registry; 184 } 185 186 Entry(String description, Image image) { 187 this.description = description; 188 this.image = image; 189 } 190 191 FieldDecoration getDecoration() { 192 if (decoration == null) { 193 if (image == null) { 194 if (imageRegistry == null) { 195 imageRegistry = JFaceResources.getImageRegistry(); 196 } 197 image = imageRegistry.get(imageId); 198 } 199 decoration = new FieldDecoration(image, description); 200 } 201 description = null; 203 imageId = null; 204 imageRegistry = null; 205 image = null; 206 207 return decoration; 208 } 209 } 210 211 215 private static FieldDecorationRegistry defaultInstance; 216 217 222 private int maxDecorationWidth = 0; 223 private int maxDecorationHeight = 0; 224 225 private HashMap decorations = new HashMap (); 226 227 233 public static FieldDecorationRegistry getDefault() { 234 if (defaultInstance == null) { 235 defaultInstance = new FieldDecorationRegistry(); 236 } 237 return defaultInstance; 238 } 239 240 247 public static void setDefault(FieldDecorationRegistry defaultRegistry) { 248 defaultInstance = defaultRegistry; 249 } 250 251 254 public FieldDecorationRegistry() { 255 maxDecorationWidth = 0; 256 maxDecorationHeight = 0; 257 } 258 259 267 public int getMaximumDecorationWidth() { 268 return maxDecorationWidth; 269 } 270 271 280 public int getMaximumDecorationHeight() { 281 return maxDecorationHeight; 282 } 283 284 298 public void registerFieldDecoration(String id, String description, 299 Image image) { 300 decorations.put(id, new Entry(description, image)); 301 recomputeMaximums(); 303 } 304 305 319 public void registerFieldDecoration(String id, String description, 320 String imageId) { 321 decorations.put(id, new Entry(description, imageId, JFaceResources 322 .getImageRegistry())); 323 recomputeMaximums(); 326 } 327 328 344 public void registerFieldDecoration(String id, String description, 345 String imageId, ImageRegistry imageRegistry) { 346 decorations.put(id, new Entry(description, imageId, imageRegistry)); 347 recomputeMaximums(); 349 } 350 351 369 public void unregisterFieldDecoration(String id) { 370 decorations.remove(id); 371 recomputeMaximums(); 372 } 373 374 382 public FieldDecoration getFieldDecoration(String id) { 383 Object entry = decorations.get(id); 384 if (entry == null) { 385 return null; 386 } 387 return ((Entry) entry).getDecoration(); 388 389 } 390 391 395 private void recomputeMaximums() { 396 Iterator entries = decorations.values().iterator(); 397 398 maxDecorationHeight = 0; 399 maxDecorationWidth = 0; 400 while (entries.hasNext()) { 401 Image image = ((Entry)entries.next()).getDecoration().getImage(); 402 if (image != null) { 403 maxDecorationHeight = Math.max(maxDecorationHeight, image.getBounds().height); 404 maxDecorationWidth = Math.max(maxDecorationWidth, image.getBounds().width); 405 } 406 } 407 408 } 409 } 410 | Popular Tags |