diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/fieldassist/FieldAssistColors.d	Thu Apr 03 18:56:20 2008 +0200
@@ -0,0 +1,222 @@
+/*******************************************************************************
+ * Copyright (c) 2006, 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.jface.fieldassist.FieldAssistColors;
+
+import tango.util.collection.ArraySeq;
+import tango.util.collection.HashMap;
+import tango.util.collection.model.Map;
+import tango.util.collection.model.Seq;
+
+import dwt.DWT;
+import dwt.graphics.Color;
+import dwt.graphics.RGB;
+import dwt.widgets.Control;
+import dwt.widgets.Display;
+import dwtx.jface.resource.JFaceColors;
+
+import dwt.dwthelper.utils;
+import dwt.dwthelper.Runnable;
+import tango.io.Stdout;
+
+/**
+ * FieldAssistColors defines protocol for retrieving colors that can be used to
+ * provide visual cues with fields. For consistency with JFace dialogs and
+ * wizards, it is recommended that FieldAssistColors is used when colors are
+ * used to annotate fields.
+ * <p>
+ * Color resources that are returned using methods in this class are maintained
+ * in the JFace color registries, or by DWT. Users of any color resources
+ * provided by this class are not responsible for the lifecycle of the color.
+ * Colors provided by this class should never be disposed by clients. In some
+ * cases, clients are provided information, such as RGB values, in order to
+ * create their own color resources. In these cases, the client should manage
+ * the lifecycle of any created resource.
+ *
+ * @since 3.2
+ * @deprecated As of 3.3, this class is no longer necessary.
+ */
+public class FieldAssistColors {
+
+    private static bool DEBUG = false;
+
+    /*
+     * Keys are background colors, values are the color with the alpha value
+     * applied
+     */
+    private static Map!(Object,Object) requiredFieldColorMap;
+
+    /*
+     * Keys are colors we have created, values are the displays on which they
+     * were created.
+     */
+    private static Map!(Object,Object) displays;
+
+    static this(){
+        requiredFieldColorMap = new HashMap!(Object,Object);
+        displays = new HashMap!(Object,Object);
+    }
+
+    /**
+     * Compute the RGB of the color that should be used for the background of a
+     * control to indicate that the control has an error. Because the color
+     * suitable for indicating an error depends on the colors set into the
+     * control, this color is always computed dynamically and provided as an RGB
+     * value. Clients who use this RGB to create a Color resource are
+     * responsible for managing the life cycle of the color.
+     * <p>
+     * This color is computed dynamically each time that it is queried. Clients
+     * should typically call this method once, create a color from the RGB
+     * provided, and dispose of the color when finished using it.
+     *
+     * @param control
+     *            the control for which the background color should be computed.
+     * @return the RGB value indicating a background color appropriate for
+     *         indicating an error in the control.
+     */
+    public static RGB computeErrorFieldBackgroundRGB(Control control) {
+        /*
+         * Use a 10% alpha of the error color applied on top of the widget
+         * background color.
+         */
+        Color dest = control.getBackground();
+        Color src = JFaceColors.getErrorText(control.getDisplay());
+        int destRed = dest.getRed();
+        int destGreen = dest.getGreen();
+        int destBlue = dest.getBlue();
+
+        // 10% alpha
+        int alpha = cast(int) (0xFF * 0.10f);
+        // Alpha blending math
+        destRed += (src.getRed() - destRed) * alpha / 0xFF;
+        destGreen += (src.getGreen() - destGreen) * alpha / 0xFF;
+        destBlue += (src.getBlue() - destBlue) * alpha / 0xFF;
+
+        return new RGB(destRed, destGreen, destBlue);
+    }
+
+    /**
+     * Return the color that should be used for the background of a control to
+     * indicate that the control is a required field and does not have content.
+     * <p>
+     * This color is managed by FieldAssistResources and should never be
+     * disposed by clients.
+     *
+     * @param control
+     *            the control on which the background color will be used.
+     * @return the color used to indicate that a field is required.
+     */
+    public static Color getRequiredFieldBackgroundColor(Control control) {
+        final Display display = control.getDisplay();
+
+        // If we are in high contrast mode, then don't apply an alpha
+        if (display.getHighContrast()) {
+            return control.getBackground();
+        }
+
+        // See if a color has already been computed
+        Object storedColor = requiredFieldColorMap.get(control.getBackground());
+        if (storedColor !is null) {
+            return cast(Color) storedColor;
+        }
+
+        // There is no color already created, so we must create one.
+        // Use a 15% alpha of yellow on top of the widget background.
+        Color dest = control.getBackground();
+        Color src = display.getSystemColor(DWT.COLOR_YELLOW);
+        int destRed = dest.getRed();
+        int destGreen = dest.getGreen();
+        int destBlue = dest.getBlue();
+
+        // 15% alpha
+        int alpha = cast(int) (0xFF * 0.15f);
+        // Alpha blending math
+        destRed += (src.getRed() - destRed) * alpha / 0xFF;
+        destGreen += (src.getGreen() - destGreen) * alpha / 0xFF;
+        destBlue += (src.getBlue() - destBlue) * alpha / 0xFF;
+
+        // create the color
+        Color color = new Color(display, destRed, destGreen, destBlue);
+        // record the color in a map using the original color as the key
+        requiredFieldColorMap.add(dest, color);
+        // If we have never created a color on this display before, install
+        // a dispose exec on the display.
+        if (!displays.contains(display)) {
+            display.disposeExec(new class Runnable {
+                public void run() {
+                    disposeColors(display);
+                }
+            });
+        }
+        // Record the color and its display in a map for later disposal.
+        displays.add(color, display);
+        return color;
+    }
+
+    /*
+     * Dispose any colors that were allocated for the given display.
+     */
+    private static void disposeColors(Display display) {
+        auto toBeRemoved = new ArraySeq!(Object);
+
+        if (DEBUG) {
+            Stdout.formatln("Display map is {}", (cast(Object)displays).toString()); //$NON-NLS-1$
+            Stdout.formatln("Color map is {}", (cast(Object)requiredFieldColorMap).toString()); //$NON-NLS-1$
+        }
+
+        // Look for any stored colors that were created on this display
+        foreach( k, v; displays ){
+            Color color = cast(Color) k;
+            if ((cast(Display) displays.get(color)).opEquals(display)) {
+                // The color is on this display. Mark it for removal.
+                toBeRemoved.append(color);
+
+                // Now look for any references to it in the required field color
+                // map
+                auto toBeRemovedFromRequiredMap = new ArraySeq!(Object);
+                foreach( k, v; requiredFieldColorMap ){
+                    Color bgColor = cast(Color) k;
+                    if ((cast(Color) requiredFieldColorMap.get(bgColor))
+                            .opEquals(color)) {
+                        // mark it for removal from the required field color map
+                        toBeRemovedFromRequiredMap.append(bgColor);
+                    }
+                }
+                // Remove references in the required field map now that
+                // we are done iterating.
+                for (int j = 0; j < toBeRemovedFromRequiredMap.size(); j++) {
+                    requiredFieldColorMap.remove(toBeRemovedFromRequiredMap
+                            .get(j));
+                }
+            }
+        }
+        // Remove references in the display map now that we are
+        // done iterating
+        for (int i = 0; i < toBeRemoved.size(); i++) {
+            Color color = cast(Color) toBeRemoved.get(i);
+            // Removing from the display map must be done before disposing the
+            // color or else the comparison between this color and the one
+            // in the map will fail.
+            displays.remove(color);
+            // Dispose it
+            if (DEBUG) {
+                Stdout.formatln("Disposing color {}", color.toString()); //$NON-NLS-1$
+            }
+            color.dispose();
+        }
+        if (DEBUG) {
+            Stdout.formatln("Display map is {}", (cast(Object)displays).toString()); //$NON-NLS-1$
+            Stdout.formatln("Color map is {}", (cast(Object)requiredFieldColorMap).toString()); //$NON-NLS-1$
+        }
+    }
+
+}