1 11 package org.eclipse.jface.resource; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.eclipse.core.runtime.Assert; 22 import org.eclipse.swt.graphics.Color; 23 import org.eclipse.swt.graphics.RGB; 24 import org.eclipse.swt.widgets.Display; 25 26 46 public class ColorRegistry extends ResourceRegistry { 47 48 52 protected Display display; 53 54 58 private List staleColors = new ArrayList (); 59 60 64 private Map stringToColor = new HashMap (7); 65 66 70 private Map stringToRGB = new HashMap (7); 71 72 75 protected Runnable displayRunnable = new Runnable () { 76 public void run() { 77 clearCaches(); 78 } 79 }; 80 81 87 public ColorRegistry() { 88 this(Display.getCurrent(), true); 89 } 90 91 96 public ColorRegistry(Display display) { 97 this (display, true); 98 } 99 100 109 public ColorRegistry(Display display, boolean cleanOnDisplayDisposal) { 110 Assert.isNotNull(display); 111 this.display = display; 112 if (cleanOnDisplayDisposal) { 113 hookDisplayDispose(); 114 } 115 } 116 117 125 private Color createColor(RGB rgb) { 126 return new Color(display, rgb); 127 } 128 129 134 private void disposeColors(Iterator iterator) { 135 while (iterator.hasNext()) { 136 Object next = iterator.next(); 137 ((Color) next).dispose(); 138 } 139 } 140 141 148 public Color get(String symbolicName) { 149 150 Assert.isNotNull(symbolicName); 151 Object result = stringToColor.get(symbolicName); 152 if (result != null) { 153 return (Color) result; 154 } 155 156 Color color = null; 157 158 result = stringToRGB.get(symbolicName); 159 if (result == null) { 160 return null; 161 } 162 163 color = createColor((RGB) result); 164 165 stringToColor.put(symbolicName, color); 166 167 return color; 168 } 169 170 173 public Set getKeySet() { 174 return Collections.unmodifiableSet(stringToRGB.keySet()); 175 } 176 177 183 public RGB getRGB(String symbolicName) { 184 Assert.isNotNull(symbolicName); 185 return (RGB) stringToRGB.get(symbolicName); 186 } 187 188 195 public ColorDescriptor getColorDescriptor(String symbolicName) { 196 return ColorDescriptor.createFrom(getRGB(symbolicName)); 197 } 198 199 202 protected void clearCaches() { 203 disposeColors(stringToColor.values().iterator()); 204 disposeColors(staleColors.iterator()); 205 stringToColor.clear(); 206 staleColors.clear(); 207 } 208 209 212 public boolean hasValueFor(String colorKey) { 213 return stringToRGB.containsKey(colorKey); 214 } 215 216 219 private void hookDisplayDispose() { 220 display.disposeExec(displayRunnable); 221 } 222 223 235 public void put(String symbolicName, RGB colorData) { 236 put(symbolicName, colorData, true); 237 } 238 239 254 private void put(String symbolicName, RGB colorData, boolean update) { 255 256 Assert.isNotNull(symbolicName); 257 Assert.isNotNull(colorData); 258 259 RGB existing = (RGB) stringToRGB.get(symbolicName); 260 if (colorData.equals(existing)) { 261 return; 262 } 263 264 Color oldColor = (Color) stringToColor.remove(symbolicName); 265 stringToRGB.put(symbolicName, colorData); 266 if (update) { 267 fireMappingChanged(symbolicName, existing, colorData); 268 } 269 270 if (oldColor != null) { 271 staleColors.add(oldColor); 272 } 273 } 274 } 275 | Popular Tags |