Package org.onemind.swingweb.mapinput.peerdelegate.java.awt

Source Code of org.onemind.swingweb.mapinput.peerdelegate.java.awt.CheckboxInputDelegate

/*
* Copyright (C) 2004 TiongHiang Lee
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not,  write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Email: thlee@onemindsoft.org
*/

package org.onemind.swingweb.mapinput.peerdelegate.java.awt;

import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.event.ItemEvent;
import java.util.Arrays;
import java.util.Map;
import java.util.logging.Logger;
import org.onemind.awtbridge.input.BridgeInputContext;
import org.onemind.awtbridge.peer.*;
import org.onemind.swingweb.mapinput.peerdelegate.MapInputDelegate;
/**
* Checkbox input delegate
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
public class CheckboxInputDelegate extends ComponentInputDelegate
{

    /** the logger * */
    private static final Logger _logger = Logger.getLogger(CheckboxInputDelegate.class.getName());

    /** the instance **/
    public static MapInputDelegate INSTANCE = new CheckboxInputDelegate();

    /**
     * {@inheritDoc}
     */
    public void processInput(BridgeComponentPeer peer, BridgeInputContext context, Map inputForm)
    {
        //the checking
        if (hasEvent(peer, inputForm))
        {
            Checkbox cbox = (Checkbox) peer.getComponent();
            cbox.setState(!cbox.getState()); //toggle the checkbox
            postEvent(context, new ItemEvent(cbox, ItemEvent.ITEM_STATE_CHANGED, cbox, cbox.getState()
                    ? ItemEvent.SELECTED
                    : ItemEvent.DESELECTED));
        } else if (hasGroupEvent(peer, inputForm))
        {
            Checkbox cbox = (Checkbox) peer.getComponentObject();
            CheckboxGroup grp = cbox.getCheckboxGroup();
            /*
             * should not need to fire unselection Checkbox selected = grp.getSelectedCheckbox(); context.getEventQueue().postEvent(
             * new ItemEvent(selected, ItemEvent.ITEM_STATE_CHANGED, selected, ItemEvent.DESELECTED));
             */
            grp.setSelectedCheckbox(cbox);
            postEvent(context, new ItemEvent(cbox, ItemEvent.ITEM_STATE_CHANGED, cbox, ItemEvent.SELECTED));
        }
    }

    /**
     * Whether has group event
     * @param peer the peer
     * @param form the input form
     * @return true if there's group event
     */
    protected boolean hasGroupEvent(BridgePeer peer, Map form)
    {
        Checkbox cbox = (Checkbox) peer.getComponentObject();
        CheckboxGroup grp = cbox.getCheckboxGroup();
        if (grp != null)
        {
            //in a button group
            BridgeCheckboxGroupPeer grppeer = (BridgeCheckboxGroupPeer) peer.getBridgeToolkit().getContext().getPeer(grp);
            if (grppeer != null)
            {
                Object value = form.get(grppeer.getId());
                _logger.finest("Got input value for group=" + value + " peer id = " + peer.getId());
                if (value instanceof String)
                {
                    return value.equals(peer.getId()) && grp.getSelectedCheckbox() != cbox;
                    //this peer is click
                } else if (value instanceof String[]){
                    return Arrays.asList((String[])value).contains(peer.getId());
                }
            }
        }
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public boolean hasEvent(BridgePeer peer, Map form)
    {
        Checkbox cbox = (Checkbox) peer.getComponentObject();
        if (super.hasEvent(peer, form))
        {
            Object value = form.get(peer.getId() + "-value");
            boolean b = value != null && (value.equals("1") || value.equals("true"));
            return cbox.getState() != b;
        }
        //if state is not the same - like a toggle
        //(only applies if the form is always submitted)
        return false;
    }
}
TOP

Related Classes of org.onemind.swingweb.mapinput.peerdelegate.java.awt.CheckboxInputDelegate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.