/*
* 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.javax.swing;
import java.awt.AWTEvent;
import java.awt.ActiveEvent;
import java.util.BitSet;
import java.util.Map;
import java.util.logging.Logger;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
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 JList input delegate
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
public class JListInputDelegate extends JComponentInputDelegate
{
/** the logger * */
private static final Logger _logger = Logger.getLogger(JListInputDelegate.class.getName());
/** the instance **/
public static MapInputDelegate INSTANCE = new JListInputDelegate();
/**
* The delayed event
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
private class DelayedEvent extends AWTEvent implements ActiveEvent
{
/** the indices * */
private BitSet _selected;
/**
* {@inheritDoc}
*/
public DelayedEvent(Object source, BitSet selected)
{
super(source, 0);
_selected = selected;
}
/**
* Set the selected indices to the JList {@inheritDoc}
*/
public void dispatch()
{
JList list = (JList) getSource();
ListSelectionModel selModel = list.getSelectionModel();
int n = list.getModel().getSize();
for (int i = 0; i < n; i++)
{
if (selModel.isSelectedIndex(i) != _selected.get(i))
{
if (_selected.get(i))
{
selModel.addSelectionInterval(i, i);
} else
{
selModel.removeSelectionInterval(i, i);
}
}
}
}
}
/**
* {@inheritDoc}
*/
public void processInput(BridgeComponentPeer peer, BridgeInputContext context, Map inputForm) throws InputException
{
if (hasEvent(peer, inputForm))
{
Object valueObj = inputForm.get(peer.getId());
if (valueObj == null)
{
return;
}
JList l = (JList) peer.getComponentObject();
BitSet selected = new BitSet(l.getModel().getSize());
String[] indicesStr = null;
if ((valueObj instanceof String && ((String) valueObj).trim().length() != 0))
{
indicesStr = ((String) valueObj).split(",");
} else if (valueObj instanceof String[]){
indicesStr = (String[]) valueObj;
} else return;
for (int i = 0; i < indicesStr.length; i++)
{
int set = Integer.parseInt(indicesStr[i].toString());
selected.set(set, true);
}
int[] selectedIndices = l.getSelectedIndices();
// postEvent(context, peer, new DelayedEvent(l, selected), ActiveEventDispatcher.getInstance(),
// BridgeEvent.HIGH_PRIORITY);
new DelayedEvent(l, selected).dispatch();
}
}
}