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