diff dwtx/jface/window/DefaultToolTip.d @ 7:8a302fdb4140

Jface some window and resource classes
author Frank Benoit <benoit@tionex.de>
date Fri, 28 Mar 2008 23:32:40 +0100
parents
children ea8ff534f622
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/window/DefaultToolTip.d	Fri Mar 28 23:32:40 2008 +0100
@@ -0,0 +1,297 @@
+/*******************************************************************************
+ * 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.window.DefaultToolTip;
+
+import dwt.DWT;
+import dwt.custom.CLabel;
+import dwt.graphics.Color;
+import dwt.graphics.Font;
+import dwt.graphics.Image;
+import dwt.graphics.Point;
+import dwt.widgets.Composite;
+import dwt.widgets.Control;
+import dwt.widgets.Event;
+
+import dwtx.jface.window.ToolTip;
+
+import dwt.dwthelper.utils;
+
+/**
+ * Default implementation of ToolTip that provides an iconofied label with font
+ * and color controls by subclass.
+ *
+ * @since 3.3
+ */
+public class DefaultToolTip : ToolTip {
+    private String text;
+
+    private Color backgroundColor;
+
+    private Font font;
+
+    private Image backgroundImage;
+
+    private Color foregroundColor;
+
+    private Image image;
+
+    private int style = DWT.SHADOW_NONE;
+
+    /**
+     * Create new instance which add TooltipSupport to the widget
+     *
+     * @param control the control on whose action the tooltip is shown
+     */
+    public this(Control control) {
+        super(control);
+    }
+
+    /**
+     * Create new instance which add TooltipSupport to the widget
+     *
+     * @param control the control to which the tooltip is bound
+     * @param style style passed to control tooltip behaviour
+     * @param manualActivation <code>true</code> if the activation is done manually using
+     *            {@link #show(Point)}
+     * @see #RECREATE
+     * @see #NO_RECREATE
+     */
+    public this(Control control, int style, bool manualActivation) {
+        super(control, style, manualActivation);
+    }
+
+    /**
+     * Creates the content are of the the tooltip. By default this creates a
+     * CLabel to display text. To customize the text Subclasses may override the
+     * following methods
+     * <ul>
+     * <li>{@link #getStyle(Event)}</li>
+     * <li>{@link #getBackgroundColor(Event)}</li>
+     * <li>{@link #getForegroundColor(Event)}</li>
+     * <li>{@link #getFont(Event)}</li>
+     * <li>{@link #getImage(Event)}</li>
+     * <li>{@link #getText(Event)}</li>
+     * <li>{@link #getBackgroundImage(Event)}</li>
+     * </ul>
+     *
+     * @param event
+     *            the event that triggered the activation of the tooltip
+     * @param parent
+     *            the parent of the content area
+     * @return the content area created
+     */
+    protected Composite createToolTipContentArea(Event event, Composite parent) {
+        Image image = getImage(event);
+        Image bgImage = getBackgroundImage(event);
+        String text = getText(event);
+        Color fgColor = getForegroundColor(event);
+        Color bgColor = getBackgroundColor(event);
+        Font font = getFont(event);
+
+        CLabel label = new CLabel(parent, getStyle(event));
+        if (text !is null) {
+            label.setText(text);
+        }
+
+        if (image !is null) {
+            label.setImage(image);
+        }
+
+        if (fgColor !is null) {
+            label.setForeground(fgColor);
+        }
+
+        if (bgColor !is null) {
+            label.setBackground(bgColor);
+        }
+
+        if (bgImage !is null) {
+            label.setBackgroundImage(image);
+        }
+
+        if (font !is null) {
+            label.setFont(font);
+        }
+
+        return label;
+    }
+
+    /**
+     * The style used to create the {@link CLabel} in the default implementation
+     *
+     * @param event
+     *            the event triggered the popup of the tooltip
+     * @return the style
+     */
+    protected int getStyle(Event event) {
+        return style;
+    }
+
+    /**
+     * The {@link Image} displayed in the {@link CLabel} in the default
+     * implementation implementation
+     *
+     * @param event
+     *            the event triggered the popup of the tooltip
+     * @return the {@link Image} or <code>null</code> if no image should be
+     *         displayed
+     */
+    protected Image getImage(Event event) {
+        return image;
+    }
+
+    /**
+     * The foreground {@link Color} used by {@link CLabel} in the default
+     * implementation
+     *
+     * @param event
+     *            the event triggered the popup of the tooltip
+     * @return the {@link Color} or <code>null</code> if default foreground
+     *         color should be used
+     */
+    protected Color getForegroundColor(Event event) {
+        return (foregroundColor is null) ? event.widget.getDisplay()
+                .getSystemColor(DWT.COLOR_INFO_FOREGROUND) : foregroundColor;
+    }
+
+    /**
+     * The background {@link Color} used by {@link CLabel} in the default
+     * implementation
+     *
+     * @param event
+     *            the event triggered the popup of the tooltip
+     * @return the {@link Color} or <code>null</code> if default background
+     *         color should be used
+     */
+    protected Color getBackgroundColor(Event event) {
+        return (backgroundColor is null) ? event.widget.getDisplay()
+                .getSystemColor(DWT.COLOR_INFO_BACKGROUND) : backgroundColor;
+    }
+
+    /**
+     * The background {@link Image} used by {@link CLabel} in the default
+     * implementation
+     *
+     * @param event
+     *            the event triggered the popup of the tooltip
+     * @return the {@link Image} or <code>null</code> if no image should be
+     *         displayed in the background
+     */
+    protected Image getBackgroundImage(Event event) {
+        return backgroundImage;
+    }
+
+    /**
+     * The {@link Font} used by {@link CLabel} in the default implementation
+     *
+     * @param event
+     *            the event triggered the popup of the tooltip
+     * @return the {@link Font} or <code>null</code> if the default font
+     *         should be used
+     */
+    protected Font getFont(Event event) {
+        return font;
+    }
+
+    /**
+     * The text displayed in the {@link CLabel} in the default implementation
+     *
+     * @param event
+     *            the event triggered the popup of the tooltip
+     * @return the text or <code>null</code> if no text has to be displayed
+     */
+    protected String getText(Event event) {
+        return text;
+    }
+
+    /**
+     * The background {@link Image} used by {@link CLabel} in the default
+     * implementation
+     *
+     * @param backgroundColor
+     *            the {@link Color} or <code>null</code> if default background
+     *            color ({@link DWT#COLOR_INFO_BACKGROUND}) should be used
+     */
+    public void setBackgroundColor(Color backgroundColor) {
+        this.backgroundColor = backgroundColor;
+    }
+
+    /**
+     * The background {@link Image} used by {@link CLabel} in the default
+     * implementation
+     *
+     * @param backgroundImage
+     *            the {@link Image} or <code>null</code> if no image should be
+     *            displayed in the background
+     */
+    public void setBackgroundImage(Image backgroundImage) {
+        this.backgroundImage = backgroundImage;
+    }
+
+    /**
+     * The {@link Font} used by {@link CLabel} in the default implementation
+     *
+     * @param font
+     *            the {@link Font} or <code>null</code> if the default font
+     *            should be used
+     */
+    public void setFont(Font font) {
+        this.font = font;
+    }
+
+    /**
+     * The foreground {@link Color} used by {@link CLabel} in the default
+     * implementation
+     *
+     * @param foregroundColor
+     *            the {@link Color} or <code>null</code> if default foreground
+     *            color should be used
+     */
+    public void setForegroundColor(Color foregroundColor) {
+        this.foregroundColor = foregroundColor;
+    }
+
+    /**
+     * The {@link Image} displayed in the {@link CLabel} in the default
+     * implementation implementation
+     *
+     * @param image
+     *            the {@link Image} or <code>null</code> if no image should be
+     *            displayed
+     */
+    public void setImage(Image image) {
+        this.image = image;
+    }
+
+    /**
+     * The style used to create the {@link CLabel} in the default implementation
+     *
+     * @param style
+     *            the event triggered the popup of the tooltip
+     */
+    public void setStyle(int style) {
+        this.style = style;
+    }
+
+    /**
+     * The text displayed in the {@link CLabel} in the default implementation
+     *
+     * @param text
+     *            the text or <code>null</code> if no text has to be displayed
+     */
+    public void setText(String text) {
+        this.text = text;
+    }
+
+}