KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > fieldassist > FieldAssistColors


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  *******************************************************************************/

11 package org.eclipse.jface.fieldassist;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
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 /**
27  * FieldAssistColors defines protocol for retrieving colors that can be used to
28  * provide visual cues with fields. For consistency with JFace dialogs and
29  * wizards, it is recommended that FieldAssistColors is used when colors are
30  * used to annotate fields.
31  * <p>
32  * Color resources that are returned using methods in this class are maintained
33  * in the JFace color registries, or by SWT. Users of any color resources
34  * provided by this class are not responsible for the lifecycle of the color.
35  * Colors provided by this class should never be disposed by clients. In some
36  * cases, clients are provided information, such as RGB values, in order to
37  * create their own color resources. In these cases, the client should manage
38  * the lifecycle of any created resource.
39  *
40  * @since 3.2
41  * @deprecated As of 3.3, this class is no longer necessary.
42  */

43 public class FieldAssistColors {
44
45     private static boolean DEBUG = false;
46
47     /*
48      * Keys are background colors, values are the color with the alpha value
49      * applied
50      */

51     private static Map JavaDoc requiredFieldColorMap = new HashMap JavaDoc();
52
53     /*
54      * Keys are colors we have created, values are the displays on which they
55      * were created.
56      */

57     private static Map JavaDoc displays = new HashMap JavaDoc();
58
59     /**
60      * Compute the RGB of the color that should be used for the background of a
61      * control to indicate that the control has an error. Because the color
62      * suitable for indicating an error depends on the colors set into the
63      * control, this color is always computed dynamically and provided as an RGB
64      * value. Clients who use this RGB to create a Color resource are
65      * responsible for managing the life cycle of the color.
66      * <p>
67      * This color is computed dynamically each time that it is queried. Clients
68      * should typically call this method once, create a color from the RGB
69      * provided, and dispose of the color when finished using it.
70      *
71      * @param control
72      * the control for which the background color should be computed.
73      * @return the RGB value indicating a background color appropriate for
74      * indicating an error in the control.
75      */

76     public static RGB computeErrorFieldBackgroundRGB(Control control) {
77         /*
78          * Use a 10% alpha of the error color applied on top of the widget
79          * background color.
80          */

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         // 10% alpha
88
int alpha = (int) (0xFF * 0.10f);
89         // Alpha blending math
90
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     /**
98      * Return the color that should be used for the background of a control to
99      * indicate that the control is a required field and does not have content.
100      * <p>
101      * This color is managed by FieldAssistResources and should never be
102      * disposed by clients.
103      *
104      * @param control
105      * the control on which the background color will be used.
106      * @return the color used to indicate that a field is required.
107      */

108     public static Color getRequiredFieldBackgroundColor(Control control) {
109         final Display display = control.getDisplay();
110
111         // If we are in high contrast mode, then don't apply an alpha
112
if (display.getHighContrast()) {
113             return control.getBackground();
114         }
115
116         // See if a color has already been computed
117
Object JavaDoc storedColor = requiredFieldColorMap.get(control.getBackground());
118         if (storedColor != null) {
119             return (Color) storedColor;
120         }
121
122         // There is no color already created, so we must create one.
123
// Use a 15% alpha of yellow on top of the widget background.
124
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         // 15% alpha
131
int alpha = (int) (0xFF * 0.15f);
132         // Alpha blending math
133
destRed += (src.getRed() - destRed) * alpha / 0xFF;
134         destGreen += (src.getGreen() - destGreen) * alpha / 0xFF;
135         destBlue += (src.getBlue() - destBlue) * alpha / 0xFF;
136
137         // create the color
138
Color color = new Color(display, destRed, destGreen, destBlue);
139         // record the color in a map using the original color as the key
140
requiredFieldColorMap.put(dest, color);
141         // If we have never created a color on this display before, install
142
// a dispose exec on the display.
143
if (!displays.containsValue(display)) {
144             display.disposeExec(new Runnable JavaDoc() {
145                 public void run() {
146                     disposeColors(display);
147                 }
148             });
149         }
150         // Record the color and its display in a map for later disposal.
151
displays.put(color, display);
152         return color;
153     }
154
155     /*
156      * Dispose any colors that were allocated for the given display.
157      */

158     private static void disposeColors(Display display) {
159         List JavaDoc toBeRemoved = new ArrayList JavaDoc(1);
160
161         if (DEBUG) {
162             System.out.println("Display map is " + displays.toString()); //$NON-NLS-1$
163
System.out
164                     .println("Color map is " + requiredFieldColorMap.toString()); //$NON-NLS-1$
165
}
166
167         // Look for any stored colors that were created on this display
168
for (Iterator JavaDoc i = displays.keySet().iterator(); i.hasNext();) {
169             Color color = (Color) i.next();
170             if (((Display) displays.get(color)).equals(display)) {
171                 // The color is on this display. Mark it for removal.
172
toBeRemoved.add(color);
173
174                 // Now look for any references to it in the required field color
175
// map
176
List JavaDoc toBeRemovedFromRequiredMap = new ArrayList JavaDoc(1);
177                 for (Iterator JavaDoc iter = requiredFieldColorMap.keySet().iterator(); iter
178                         .hasNext();) {
179                     Color bgColor = (Color) iter.next();
180                     if (((Color) requiredFieldColorMap.get(bgColor))
181                             .equals(color)) {
182                         // mark it for removal from the required field color map
183
toBeRemovedFromRequiredMap.add(bgColor);
184                     }
185                 }
186                 // Remove references in the required field map now that
187
// we are done iterating.
188
for (int j = 0; j < toBeRemovedFromRequiredMap.size(); j++) {
189                     requiredFieldColorMap.remove(toBeRemovedFromRequiredMap
190                             .get(j));
191                 }
192             }
193         }
194         // Remove references in the display map now that we are
195
// done iterating
196
for (int i = 0; i < toBeRemoved.size(); i++) {
197             Color color = (Color) toBeRemoved.get(i);
198             // Removing from the display map must be done before disposing the
199
// color or else the comparison between this color and the one
200
// in the map will fail.
201
displays.remove(color);
202             // Dispose it
203
if (DEBUG) {
204                 System.out.println("Disposing color " + color.toString()); //$NON-NLS-1$
205
}
206             color.dispose();
207         }
208         if (DEBUG) {
209             System.out.println("Display map is " + displays.toString()); //$NON-NLS-1$
210
System.out
211                     .println("Color map is " + requiredFieldColorMap.toString()); //$NON-NLS-1$
212
}
213     }
214
215 }
216
Popular Tags