view dwtx/ui/forms/HyperlinkGroup.d @ 192:c3583c6ec027

Added missing default cases for switch statements
author Frank Benoit <benoit@tionex.de>
date Mon, 03 Nov 2008 22:52:26 +0100
parents 04b47443bb01
children
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 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.ui.forms.HyperlinkGroup;

import dwtx.ui.forms.HyperlinkSettings;

import dwt.DWT;
import dwt.graphics.Color;
import dwt.widgets.Display;
import dwt.widgets.Event;
import dwt.widgets.Listener;
import dwtx.ui.forms.events.HyperlinkEvent;
import dwtx.ui.forms.events.IHyperlinkListener;
import dwtx.ui.forms.widgets.Hyperlink;

import dwt.dwthelper.utils;
import dwtx.dwtxhelper.Collection;

/**
 * Manages a group of hyperlinks. It tracks activation, updates normal and
 * active colors and updates underline state depending on the underline
 * preference. Hyperlink labels are added to the group after creation and are
 * automatically removed from the group when they are disposed.
 *
 * @since 3.0
 */

public final class HyperlinkGroup : HyperlinkSettings {
    private ArrayList links;
    private Hyperlink lastActivated;
    private Hyperlink lastEntered;
    private GroupListener listener;
    private bool isActiveBackgroundSet;
    private bool isActiveForegroundSet;
    private bool isBackgroundSet;
    private bool isForegroundSet;

    private class GroupListener : Listener, IHyperlinkListener {

        private Color previousBackground;
        private Color previousForeground;

        public void handleEvent(Event e) {
            switch (e.type) {
                case DWT.MouseEnter :
                    onMouseEnter(e);
                    break;
                case DWT.MouseExit :
                    onMouseExit(e);
                    break;
                case DWT.MouseDown :
                    onMouseDown(e);
                    break;
                case DWT.Dispose :
                    unhook(cast(Hyperlink) e.widget);
                    break;
                default:
            }
        }
        private void onMouseEnter(Event e) {
            Hyperlink link = cast(Hyperlink) e.widget;
            previousBackground = link.getBackground();
            previousForeground = link.getForeground();
            if (isActiveBackgroundSet)
                link.setBackground(getActiveBackground());
            if (isActiveForegroundSet)
                link.setForeground(getActiveForeground());
            if (getHyperlinkUnderlineMode() is UNDERLINE_HOVER)
                link.setUnderlined(true);
            link.setCursor(getHyperlinkCursor());
        }
        private void onMouseExit(Event e) {
            Hyperlink link = cast(Hyperlink) e.widget;
            if (isActiveBackgroundSet)
                link.setBackground(previousBackground);
            if (isActiveForegroundSet)
                link.setForeground(previousForeground);
            if (getHyperlinkUnderlineMode() is UNDERLINE_HOVER)
                link.setUnderlined(false);
        }
        public void linkActivated(HyperlinkEvent e) {
        }

        public void linkEntered(HyperlinkEvent e) {
            Hyperlink link = cast(Hyperlink) e.widget;
            if (lastEntered !is null) {
                linkExited(lastEntered);
            }
            lastEntered = link;
        }

        public void linkExited(HyperlinkEvent e) {
            linkExited(cast(Hyperlink) e.widget);
        }
        private void linkExited(Hyperlink link) {
            link.setCursor(null);
            if (lastEntered is link)
                lastEntered = null;
        }
    }

    /**
     * Creates a hyperlink group.
     */

    public this(Display display) {
        links = new ArrayList();
        super(display);
        listener = new GroupListener();
    }

    /**
     * Returns the link that has been active the last, or <code>null</code>
     * if no link has been active yet or the last active link has been
     * disposed.
     *
     * @return the last active link or <code>null</code>
     */
    public Hyperlink getLastActivated() {
        return lastActivated;
    }
    /**
     * Adds a hyperlink to the group to be jointly managed. Hyperlink will be
     * managed until it is disposed. Settings like colors, cursors and modes
     * will affect all managed hyperlinks.
     *
     * @param link
     */

    public void add(Hyperlink link) {
        if (isBackgroundSet)
            link.setBackground(getBackground());
        if (isForegroundSet)
            link.setForeground(getForeground());
        if (getHyperlinkUnderlineMode() is UNDERLINE_ALWAYS)
            link.setUnderlined(true);
        hook(link);
    }

    /**
     * Sets the new active hyperlink background for all the links.
     *
     * @param newActiveBackground
     *            the new active background
     */
    public void setActiveBackground(Color newActiveBackground) {
        super.setActiveBackground(newActiveBackground);
        isActiveBackgroundSet = true;
    }

    /**
     * Sets the new active hyperlink foreground for all the links.
     *
     * @param newActiveForeground
     *            the new active foreground
     */
    public void setActiveForeground(Color newActiveForeground) {
        super.setActiveForeground(newActiveForeground);
        isActiveForegroundSet = true;
    }

    /**
     * Sets the group background and also sets the background of all the
     * currently managed links.
     *
     * @param bg
     *            the new background
     */
    public void setBackground(Color bg) {
        super.setBackground(bg);
        isBackgroundSet = true;
        if (links !is null) {
            for (int i = 0; i < links.size(); i++) {
                Hyperlink label = cast(Hyperlink) links.get(i);
                label.setBackground(bg);
            }
        }
    }
    /**
     * Sets the group foreground and also sets the background of all the
     * currently managed links.
     *
     * @param fg
     *            the new foreground
     */
    public void setForeground(Color fg) {
        super.setForeground(fg);
        isForegroundSet = true;
        if (links !is null) {
            for (int i = 0; i < links.size(); i++) {
                Hyperlink label = cast(Hyperlink) links.get(i);
                label.setForeground(fg);
            }
        }
    }
    /**
     * Sets the hyperlink underline mode.
     *
     * @param mode
     *            the new hyperlink underline mode
     * @see HyperlinkSettings
     */
    public void setHyperlinkUnderlineMode(int mode) {
        super.setHyperlinkUnderlineMode(mode);
        if (links !is null) {
            for (int i = 0; i < links.size(); i++) {
                Hyperlink label = cast(Hyperlink) links.get(i);
                label.setUnderlined(mode is UNDERLINE_ALWAYS);
            }
        }
    }

    private void hook(Hyperlink link) {
        link.addListener(DWT.MouseDown, listener);
        link.addHyperlinkListener(listener);
        link.addListener(DWT.Dispose, listener);
        link.addListener(DWT.MouseEnter, listener);
        link.addListener(DWT.MouseExit, listener);
        links.add(link);
    }

    private void unhook(Hyperlink link) {
        link.removeListener(DWT.MouseDown, listener);
        link.removeHyperlinkListener(listener);
        link.removeListener(DWT.MouseEnter, listener);
        link.removeListener(DWT.MouseExit, listener);
        if (lastActivated is link)
            lastActivated = null;
        if (lastEntered is link)
            lastEntered = null;
        links.remove(link);
    }

    private void onMouseDown(Event e) {
        if (e.button is 1)
            return;
        lastActivated = cast(Hyperlink) e.widget;
    }
}