/*
* 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.Map;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import org.onemind.awtbridge.event.BridgeEventQueue;
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 JComboBox Input delegate
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
public class JComboBoxInputDelegate extends JComponentInputDelegate
{
/** the logger * */
private static final Logger _logger = Logger.getLogger(JComboBoxInputDelegate.class.getName());
/** the instance **/
public static MapInputDelegate INSTANCE = new JComboBoxInputDelegate();
/**
* The delayed event
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
private class DelayedEvent extends AWTEvent implements ActiveEvent
{
/** the index to set * */
private int _i;
/**
* Constructor
* @param source the source
* @param i the index
*/
public DelayedEvent(Object source, int i)
{
super(source, 0);
_i = i;
}
/**
* Set the index to the JComboBox {@inheritDoc}
*/
public void dispatch()
{
((JComboBox) getSource()).setSelectedIndex(_i);
}
};
/**
* {@inheritDoc}
*/
public void processInput(BridgeComponentPeer peer, BridgeInputContext context, Map inputForm) throws InputException
{
if (hasEvent(peer, inputForm))
{
String indexStr = (String) inputForm.get(peer.getId());
try
{
int i = Integer.parseInt(indexStr);
JComboBox combo = (JComboBox) peer.getComponentObject();
if (combo.getSelectedIndex() != i)
{
_logger.finest("item changes detected on component " + combo);
postEvent(context, new DelayedEvent(combo, i), BridgeEventQueue.HIGH_PRIORITY);
}
} catch (NumberFormatException e)
{
throw new InputException("combobox input value is not an integer", e);
}
}
}
}