comparison dwtx/jface/fieldassist/FieldAssistColors.d @ 29:f12d40e7da8f

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