/*
* 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.List;
import java.awt.event.ItemEvent;
import java.util.BitSet;
import java.util.Map;
import java.util.logging.Logger;
import org.onemind.awtbridge.input.BridgeInputContext;
import org.onemind.awtbridge.input.InputException;
import org.onemind.awtbridge.peer.BridgeComponentPeer;
import org.onemind.swingweb.mapinput.peerdelegate.MapInputDelegate;
/**
* The List input delegate
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
public class ListInputDelegate extends ComponentInputDelegate
{
/** the logger * */
private static final Logger _logger = Logger.getLogger(ListInputDelegate.class.getName());
/** the instance **/
public static MapInputDelegate INSTANCE = new ListInputDelegate();
/**
* {@inheritDoc}
*/
public void processInput(BridgeComponentPeer peer, BridgeInputContext context, Map inputForm) throws InputException
{
if (hasEvent(peer, inputForm))
{
Object indicesObj = inputForm.get(peer.getId());
List l = (List) peer.getComponentObject();
BitSet selected = new BitSet(l.getItemCount());
if (indicesObj instanceof String)
{
int i = Integer.parseInt((String) indicesObj);
selected.set(i, true);
} else if (indicesObj instanceof Object[])
{
Object[] indicesStr = (Object[]) indicesObj;
for (int i = 0; i < indicesStr.length; i++)
{
int set = Integer.parseInt(indicesStr[i].toString());
selected.set(set, true);
}
}
try
{
int event = 0;
for (int i = 0; i < l.getItemCount(); i++)
{
if (selected.get(i))
{
if (!l.isIndexSelected(i))
{
event = 1;
l.select(i);
_logger.finest("made " + i + " selected" + l.isIndexSelected(i));
} //else ignore
} else //not selected
if (l.isIndexSelected(i))
{
event = -1;
l.deselect(i);
_logger.finest("made " + i + " deselected " + l.isIndexSelected(i));
}
}
//the firing of event could be random
//when there's no listener to this list component
//because the user could do several selection/deselection
//before submiting the changes, there's no way to keep track
//of the order
if (event == -1)
{
postEvent(context, new ItemEvent(l, ItemEvent.ITEM_STATE_CHANGED, l, ItemEvent.DESELECTED));
} else if (event == 1)
{
postEvent(context, new ItemEvent(l, ItemEvent.ITEM_STATE_CHANGED, l, ItemEvent.SELECTED));
}
} catch (NumberFormatException e)
{
throw new InputException("Choice input value is not an integer", e);
}
}
}
}