Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/fieldassist/FieldAssistColors.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 | 735224fcc45f |
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.FieldAssistColors; | |
14 | |
15 | |
16 import org.eclipse.swt.SWT; | |
17 import org.eclipse.swt.graphics.Color; | |
18 import org.eclipse.swt.graphics.RGB; | |
19 import org.eclipse.swt.widgets.Control; | |
20 import org.eclipse.swt.widgets.Display; | |
21 import org.eclipse.jface.resource.JFaceColors; | |
22 | |
23 import java.lang.all; | |
24 import java.util.List; | |
25 import java.util.ArrayList; | |
26 import java.util.Iterator; | |
27 import java.util.Map; | |
28 import java.util.HashMap; | |
29 import java.util.Set; | |
30 import tango.io.Stdout; | |
31 | |
32 /** | |
33 * FieldAssistColors defines protocol for retrieving colors that can be used to | |
34 * provide visual cues with fields. For consistency with JFace dialogs and | |
35 * wizards, it is recommended that FieldAssistColors is used when colors are | |
36 * used to annotate fields. | |
37 * <p> | |
38 * Color resources that are returned using methods in this class are maintained | |
39 * in the JFace color registries, or by SWT. Users of any color resources | |
40 * provided by this class are not responsible for the lifecycle of the color. | |
41 * Colors provided by this class should never be disposed by clients. In some | |
42 * cases, clients are provided information, such as RGB values, in order to | |
43 * create their own color resources. In these cases, the client should manage | |
44 * the lifecycle of any created resource. | |
45 * | |
46 * @since 3.2 | |
47 * @deprecated As of 3.3, this class is no longer necessary. | |
48 */ | |
49 public class FieldAssistColors { | |
50 | |
51 private static bool DEBUG = false; | |
52 | |
53 /* | |
54 * Keys are background colors, values are the color with the alpha value | |
55 * applied | |
56 */ | |
57 private static Map requiredFieldColorMap; | |
58 | |
59 /* | |
60 * Keys are colors we have created, values are the displays on which they | |
61 * were created. | |
62 */ | |
63 private static Map displays; | |
64 | |
65 static this(){ | |
66 requiredFieldColorMap = new HashMap(); | |
67 displays = new HashMap(); | |
68 } | |
69 | |
70 /** | |
71 * Compute the RGB of the color that should be used for the background of a | |
72 * control to indicate that the control has an error. Because the color | |
73 * suitable for indicating an error depends on the colors set into the | |
74 * control, this color is always computed dynamically and provided as an RGB | |
75 * value. Clients who use this RGB to create a Color resource are | |
76 * responsible for managing the life cycle of the color. | |
77 * <p> | |
78 * This color is computed dynamically each time that it is queried. Clients | |
79 * should typically call this method once, create a color from the RGB | |
80 * provided, and dispose of the color when finished using it. | |
81 * | |
82 * @param control | |
83 * the control for which the background color should be computed. | |
84 * @return the RGB value indicating a background color appropriate for | |
85 * indicating an error in the control. | |
86 */ | |
87 public static RGB computeErrorFieldBackgroundRGB(Control control) { | |
88 /* | |
89 * Use a 10% alpha of the error color applied on top of the widget | |
90 * background color. | |
91 */ | |
92 Color dest = control.getBackground(); | |
93 Color src = JFaceColors.getErrorText(control.getDisplay()); | |
94 int destRed = dest.getRed(); | |
95 int destGreen = dest.getGreen(); | |
96 int destBlue = dest.getBlue(); | |
97 | |
98 // 10% alpha | |
99 int alpha = cast(int) (0xFF * 0.10f); | |
100 // Alpha blending math | |
101 destRed += (src.getRed() - destRed) * alpha / 0xFF; | |
102 destGreen += (src.getGreen() - destGreen) * alpha / 0xFF; | |
103 destBlue += (src.getBlue() - destBlue) * alpha / 0xFF; | |
104 | |
105 return new RGB(destRed, destGreen, destBlue); | |
106 } | |
107 | |
108 /** | |
109 * Return the color that should be used for the background of a control to | |
110 * indicate that the control is a required field and does not have content. | |
111 * <p> | |
112 * This color is managed by FieldAssistResources and should never be | |
113 * disposed by clients. | |
114 * | |
115 * @param control | |
116 * the control on which the background color will be used. | |
117 * @return the color used to indicate that a field is required. | |
118 */ | |
119 public static Color getRequiredFieldBackgroundColor(Control control) { | |
120 final Display display = control.getDisplay(); | |
121 | |
122 // If we are in high contrast mode, then don't apply an alpha | |
123 if (display.getHighContrast()) { | |
124 return control.getBackground(); | |
125 } | |
126 | |
127 // See if a color has already been computed | |
128 Object storedColor = requiredFieldColorMap.get(control.getBackground()); | |
129 if (storedColor !is null) { | |
130 return cast(Color) storedColor; | |
131 } | |
132 | |
133 // There is no color already created, so we must create one. | |
134 // Use a 15% alpha of yellow on top of the widget background. | |
135 Color dest = control.getBackground(); | |
136 Color src = display.getSystemColor(SWT.COLOR_YELLOW); | |
137 int destRed = dest.getRed(); | |
138 int destGreen = dest.getGreen(); | |
139 int destBlue = dest.getBlue(); | |
140 | |
141 // 15% alpha | |
142 int alpha = cast(int) (0xFF * 0.15f); | |
143 // Alpha blending math | |
144 destRed += (src.getRed() - destRed) * alpha / 0xFF; | |
145 destGreen += (src.getGreen() - destGreen) * alpha / 0xFF; | |
146 destBlue += (src.getBlue() - destBlue) * alpha / 0xFF; | |
147 | |
148 // create the color | |
149 Color color = new Color(display, destRed, destGreen, destBlue); | |
150 // record the color in a map using the original color as the key | |
151 requiredFieldColorMap.put(dest, color); | |
152 // If we have never created a color on this display before, install | |
153 // a dispose exec on the display. | |
154 if (!displays.containsKey(display)) { | |
155 display.disposeExec(new class Runnable { | |
156 public void run() { | |
157 disposeColors(display); | |
158 } | |
159 }); | |
160 } | |
161 // Record the color and its display in a map for later disposal. | |
162 displays.put(color, display); | |
163 return color; | |
164 } | |
165 | |
166 /* | |
167 * Dispose any colors that were allocated for the given display. | |
168 */ | |
169 private static void disposeColors(Display display) { | |
170 List toBeRemoved = new ArrayList(1); | |
171 | |
172 if (DEBUG) { | |
173 Stdout.formatln("Display map is {}", (cast(Object)displays).toString()); //$NON-NLS-1$ | |
174 Stdout.formatln("Color map is {}", (cast(Object)requiredFieldColorMap).toString()); //$NON-NLS-1$ | |
175 } | |
176 | |
177 // Look for any stored colors that were created on this display | |
178 for (Iterator i = displays.keySet().iterator(); i.hasNext();) { | |
179 Color color = cast(Color) i.next(); | |
180 if ((cast(Display) displays.get(color)).opEquals(display)) { | |
181 // The color is on this display. Mark it for removal. | |
182 toBeRemoved.add(color); | |
183 | |
184 // Now look for any references to it in the required field color | |
185 // map | |
186 List toBeRemovedFromRequiredMap = new ArrayList(1); | |
187 for (Iterator iter = requiredFieldColorMap.keySet().iterator(); iter | |
188 .hasNext();) { | |
189 Color bgColor = cast(Color) iter.next(); | |
190 if ((cast(Color) requiredFieldColorMap.get(bgColor)) | |
191 .opEquals(color)) { | |
192 // mark it for removal from the required field color map | |
193 toBeRemovedFromRequiredMap.add(bgColor); | |
194 } | |
195 } | |
196 // Remove references in the required field map now that | |
197 // we are done iterating. | |
198 for (int j = 0; j < toBeRemovedFromRequiredMap.size(); j++) { | |
199 requiredFieldColorMap.remove(toBeRemovedFromRequiredMap | |
200 .get(j)); | |
201 } | |
202 } | |
203 } | |
204 // Remove references in the display map now that we are | |
205 // done iterating | |
206 for (int i = 0; i < toBeRemoved.size(); i++) { | |
207 Color color = cast(Color) toBeRemoved.get(i); | |
208 // Removing from the display map must be done before disposing the | |
209 // color or else the comparison between this color and the one | |
210 // in the map will fail. | |
211 displays.remove(color); | |
212 // Dispose it | |
213 if (DEBUG) { | |
214 Stdout.formatln("Disposing color {}", color.toString()); //$NON-NLS-1$ | |
215 } | |
216 color.dispose(); | |
217 } | |
218 if (DEBUG) { | |
219 Stdout.formatln("Display map is {}", (cast(Object)displays).toString()); //$NON-NLS-1$ | |
220 Stdout.formatln("Color map is {}", (cast(Object)requiredFieldColorMap).toString()); //$NON-NLS-1$ | |
221 } | |
222 } | |
223 | |
224 } |