1 11 package org.eclipse.jdt.internal.ui.text; 12 13 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.swt.graphics.Color; 19 import org.eclipse.swt.graphics.RGB; 20 import org.eclipse.swt.widgets.Display; 21 22 import org.eclipse.jdt.ui.text.IColorManager; 23 import org.eclipse.jdt.ui.text.IColorManagerExtension; 24 25 28 public class JavaColorManager implements IColorManager, IColorManagerExtension { 29 30 protected Map fKeyTable= new HashMap (10); 31 protected Map fDisplayTable= new HashMap (2); 32 33 37 private boolean fAutoDisposeOnDisplayDispose; 38 39 40 45 public JavaColorManager() { 46 this(true); 47 } 48 49 58 public JavaColorManager(boolean autoDisposeOnDisplayDispose) { 59 fAutoDisposeOnDisplayDispose= autoDisposeOnDisplayDispose; 60 } 61 62 public void dispose(Display display) { 63 Map colorTable= (Map ) fDisplayTable.get(display); 64 if (colorTable != null) { 65 Iterator e= colorTable.values().iterator(); 66 while (e.hasNext()) { 67 Color color= (Color)e.next(); 68 if (color != null && !color.isDisposed()) 69 color.dispose(); 70 } 71 } 72 } 73 74 77 public Color getColor(RGB rgb) { 78 79 if (rgb == null) 80 return null; 81 82 final Display display= Display.getCurrent(); 83 Map colorTable= (Map ) fDisplayTable.get(display); 84 if (colorTable == null) { 85 colorTable= new HashMap (10); 86 fDisplayTable.put(display, colorTable); 87 if (fAutoDisposeOnDisplayDispose) { 88 display.disposeExec(new Runnable () { 89 public void run() { 90 dispose(display); 91 } 92 }); 93 } 94 } 95 96 Color color= (Color) colorTable.get(rgb); 97 if (color == null) { 98 color= new Color(Display.getCurrent(), rgb); 99 colorTable.put(rgb, color); 100 } 101 102 return color; 103 } 104 105 108 public void dispose() { 109 if (!fAutoDisposeOnDisplayDispose) 110 dispose(Display.getCurrent()); 111 } 112 113 116 public Color getColor(String key) { 117 118 if (key == null) 119 return null; 120 121 RGB rgb= (RGB) fKeyTable.get(key); 122 return getColor(rgb); 123 } 124 125 128 public void bindColor(String key, RGB rgb) { 129 Object value= fKeyTable.get(key); 130 if (value != null) 131 throw new UnsupportedOperationException (); 132 133 fKeyTable.put(key, rgb); 134 } 135 136 139 public void unbindColor(String key) { 140 fKeyTable.remove(key); 141 } 142 } 143 | Popular Tags |