1 11 package org.eclipse.jface.fieldassist; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.jface.resource.JFaceColors; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.graphics.Color; 22 import org.eclipse.swt.graphics.RGB; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.swt.widgets.Display; 25 26 43 public class FieldAssistColors { 44 45 private static boolean DEBUG = false; 46 47 51 private static Map requiredFieldColorMap = new HashMap (); 52 53 57 private static Map displays = new HashMap (); 58 59 76 public static RGB computeErrorFieldBackgroundRGB(Control control) { 77 81 Color dest = control.getBackground(); 82 Color src = JFaceColors.getErrorText(control.getDisplay()); 83 int destRed = dest.getRed(); 84 int destGreen = dest.getGreen(); 85 int destBlue = dest.getBlue(); 86 87 int alpha = (int) (0xFF * 0.10f); 89 destRed += (src.getRed() - destRed) * alpha / 0xFF; 91 destGreen += (src.getGreen() - destGreen) * alpha / 0xFF; 92 destBlue += (src.getBlue() - destBlue) * alpha / 0xFF; 93 94 return new RGB(destRed, destGreen, destBlue); 95 } 96 97 108 public static Color getRequiredFieldBackgroundColor(Control control) { 109 final Display display = control.getDisplay(); 110 111 if (display.getHighContrast()) { 113 return control.getBackground(); 114 } 115 116 Object storedColor = requiredFieldColorMap.get(control.getBackground()); 118 if (storedColor != null) { 119 return (Color) storedColor; 120 } 121 122 Color dest = control.getBackground(); 125 Color src = display.getSystemColor(SWT.COLOR_YELLOW); 126 int destRed = dest.getRed(); 127 int destGreen = dest.getGreen(); 128 int destBlue = dest.getBlue(); 129 130 int alpha = (int) (0xFF * 0.15f); 132 destRed += (src.getRed() - destRed) * alpha / 0xFF; 134 destGreen += (src.getGreen() - destGreen) * alpha / 0xFF; 135 destBlue += (src.getBlue() - destBlue) * alpha / 0xFF; 136 137 Color color = new Color(display, destRed, destGreen, destBlue); 139 requiredFieldColorMap.put(dest, color); 141 if (!displays.containsValue(display)) { 144 display.disposeExec(new Runnable () { 145 public void run() { 146 disposeColors(display); 147 } 148 }); 149 } 150 displays.put(color, display); 152 return color; 153 } 154 155 158 private static void disposeColors(Display display) { 159 List toBeRemoved = new ArrayList (1); 160 161 if (DEBUG) { 162 System.out.println("Display map is " + displays.toString()); System.out 164 .println("Color map is " + requiredFieldColorMap.toString()); } 166 167 for (Iterator i = displays.keySet().iterator(); i.hasNext();) { 169 Color color = (Color) i.next(); 170 if (((Display) displays.get(color)).equals(display)) { 171 toBeRemoved.add(color); 173 174 List toBeRemovedFromRequiredMap = new ArrayList (1); 177 for (Iterator iter = requiredFieldColorMap.keySet().iterator(); iter 178 .hasNext();) { 179 Color bgColor = (Color) iter.next(); 180 if (((Color) requiredFieldColorMap.get(bgColor)) 181 .equals(color)) { 182 toBeRemovedFromRequiredMap.add(bgColor); 184 } 185 } 186 for (int j = 0; j < toBeRemovedFromRequiredMap.size(); j++) { 189 requiredFieldColorMap.remove(toBeRemovedFromRequiredMap 190 .get(j)); 191 } 192 } 193 } 194 for (int i = 0; i < toBeRemoved.size(); i++) { 197 Color color = (Color) toBeRemoved.get(i); 198 displays.remove(color); 202 if (DEBUG) { 204 System.out.println("Disposing color " + color.toString()); } 206 color.dispose(); 207 } 208 if (DEBUG) { 209 System.out.println("Display map is " + displays.toString()); System.out 211 .println("Color map is " + requiredFieldColorMap.toString()); } 213 } 214 215 } 216 | Popular Tags |