All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.gwt.test.internal.utils.RadioButtonManager Maven / Gradle / Ivy

There is a newer version: 0.63
Show newest version
package com.googlecode.gwt.test.internal.utils;

import com.google.gwt.user.client.ui.RadioButton;
import com.googlecode.gwt.test.internal.AfterTestCallback;
import com.googlecode.gwt.test.internal.AfterTestCallbackManager;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * RadioButton instance Manager, to emulate radigroup behaviour. For internal use
 * only.
 *
 * @author Gael Lazzari
 */
public class RadioButtonManager implements AfterTestCallback {

    private static RadioButtonManager INSTANCE;

    public static void beforeSetName(RadioButton rb, String newName) {

        Set rbs = get().getRadioButtons(rb);

        if (rbs == null) {
            // not attached, do nothing
            return;
        }

        // 1. remove from the old radiogroup
        rbs.remove(rb);

        // 3. register in the new radiogroup
        get().registerWithNewName(rb, newName);

    }

    public static RadioButtonManager get() {
        if (INSTANCE == null) {
            INSTANCE = new RadioButtonManager();
        }

        return INSTANCE;
    }

    public static void onLoad(RadioButton rb) {
        get().register(rb);
    }

    public static void onRadioGroupChanged(RadioButton rb, Boolean value, boolean fireEvents) {
        if (value != null && value) {
            Set rbs = get().getRadioButtons(rb);

            if (rbs == null) {
                // not attached, do nothing
                return;
            }
            for (RadioButton radioButton : rbs) {
                if (!rb.equals(radioButton) && radioButton.getValue()) {
                    radioButton.setValue(false, fireEvents);
                }
            }
        }
    }

    public static void onUnload(RadioButton rb) {
        get().deregister(rb);
    }

    private final Map> map = new HashMap>();

    private RadioButtonManager() {
        AfterTestCallbackManager.get().registerCallback(this);
    }

    public void afterTest() {
        map.clear();
    }

    private void deregister(RadioButton rb) {
        Set rbs = getRadioButtons(rb);
        if (rbs != null) {
            rbs.remove(rb);
        }
    }

    private Set getRadioButtons(RadioButton rb) {
        if (!rb.isAttached()) {
            return null;
        }

        return getRadioButtons(rb.getName());
    }

    private Set getRadioButtons(String groupName) {
        Set set = map.get(groupName);
        if (set == null) {
            set = new HashSet();
            map.put(groupName, set);
        }

        return set;
    }

    private void register(RadioButton rb) {
        getRadioButtons(rb).add(rb);
    }

    private void registerWithNewName(RadioButton rb, String newName) {
        getRadioButtons(newName).add(rb);
    }

}