/*
* 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.client.gwt.ui;
import java.util.ArrayList;
import java.util.List;
import org.onemind.swingweb.client.core.AbstractClient;
import org.onemind.swingweb.client.dom.DomNode;
import com.google.gwt.user.client.ui.*;
public class ListUIHandler extends ComponentUIHandler implements ChangeListener
{
public void onChange(Widget sender)
{
handleEvent(sender, null);
}
public ListUIHandler(AbstractClient client)
{
super(client);
}
public Object createComponentInstance(Object parent, DomNode element)
{
ListBox lb = new ListBox();
lb.setMultipleSelect(true);
return lb;
}
protected void populateItems(ListBox list, DomNode element)
{
list.clear();
List nodes = element.getChildNodes();
int count = 0;
for (int i = 0; i < nodes.size(); i++)
{
DomNode childElement = (DomNode) nodes.get(i);
if ("item".equals(childElement.getName()))
{
String value = childElement.getAttribute("value");
String selected = childElement.getAttribute("selected");
list.addItem(value, String.valueOf(count));
if (Boolean.valueOf(selected).booleanValue())
{
list.setItemSelected(count, true);
}
count++;
}
}
}
public Object updateUI(Object com, DomNode element)
{
ListBox list = (ListBox) com;
String multipleMode = element.getAttribute("multipleMode");
if (Boolean.valueOf(multipleMode).booleanValue())
{
list.setMultipleSelect(true);
}
populateItems(list, element);
return super.updateUI(com, element);
}
/**
* {@inheritDoc}
*/
protected void registerListeners(Object com)
{
((ListBox) com).addChangeListener(this);
super.registerListeners(com);
}
public void postEvent(Object sender, Object data)
{
ListBox l = (ListBox) sender;
String id = getClient().getComponentId(l);
if (l.isMultipleSelect())
{
List selectedIndices = new ArrayList();
for (int i = 0; i < l.getItemCount(); i++)
{
if (l.isItemSelected(i))
{
selectedIndices.add(String.valueOf(i));
}
}
String selectionStr[] = new String[selectedIndices.size()];
for (int i = 0; i < selectedIndices.size(); i++)
{
selectionStr[i] = (String) selectedIndices.get(i);
}
postEvent(l, selectionStr, true);
} else
{
String selected = String.valueOf(l.getSelectedIndex());
postEvent(l, selected, true);
}
}
}