001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.client.gwt.ui;
022:
023: import java.util.ArrayList;
024: import java.util.List;
025: import org.onemind.swingweb.client.core.AbstractClient;
026: import org.onemind.swingweb.client.dom.DomNode;
027: import com.google.gwt.user.client.ui.*;
028:
029: public class ListUIHandler extends ComponentUIHandler implements
030: ChangeListener {
031:
032: public void onChange(Widget sender) {
033: handleEvent(sender, null);
034: }
035:
036: public ListUIHandler(AbstractClient client) {
037: super (client);
038: }
039:
040: public Object createComponentInstance(Object parent, DomNode element) {
041: ListBox lb = new ListBox();
042: lb.setMultipleSelect(true);
043: return lb;
044: }
045:
046: protected void populateItems(ListBox list, DomNode element) {
047: list.clear();
048: List nodes = element.getChildNodes();
049: int count = 0;
050: for (int i = 0; i < nodes.size(); i++) {
051: DomNode childElement = (DomNode) nodes.get(i);
052: if ("item".equals(childElement.getName())) {
053: String value = childElement.getAttribute("value");
054: String selected = childElement.getAttribute("selected");
055: list.addItem(value, String.valueOf(count));
056: if (Boolean.valueOf(selected).booleanValue()) {
057: list.setItemSelected(count, true);
058: }
059: count++;
060: }
061: }
062: }
063:
064: public Object updateUI(Object com, DomNode element) {
065: ListBox list = (ListBox) com;
066: String multipleMode = element.getAttribute("multipleMode");
067: if (Boolean.valueOf(multipleMode).booleanValue()) {
068: list.setMultipleSelect(true);
069: }
070: populateItems(list, element);
071: return super .updateUI(com, element);
072: }
073:
074: /**
075: * {@inheritDoc}
076: */
077: protected void registerListeners(Object com) {
078: ((ListBox) com).addChangeListener(this );
079: super .registerListeners(com);
080: }
081:
082: public void postEvent(Object sender, Object data) {
083: ListBox l = (ListBox) sender;
084: String id = getClient().getComponentId(l);
085: if (l.isMultipleSelect()) {
086: List selectedIndices = new ArrayList();
087: for (int i = 0; i < l.getItemCount(); i++) {
088: if (l.isItemSelected(i)) {
089: selectedIndices.add(String.valueOf(i));
090: }
091: }
092: String selectionStr[] = new String[selectedIndices.size()];
093: for (int i = 0; i < selectedIndices.size(); i++) {
094: selectionStr[i] = (String) selectedIndices.get(i);
095: }
096: postEvent(l, selectionStr, true);
097: } else {
098: String selected = String.valueOf(l.getSelectedIndex());
099: postEvent(l, selected, true);
100: }
101: }
102: }
|