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.awt.ui;
022:
023: import java.awt.AWTEvent;
024: import java.awt.event.ItemEvent;
025: import java.awt.event.ItemListener;
026: import java.util.List;
027: import org.onemind.swingweb.client.core.AbstractClient;
028: import org.onemind.swingweb.client.dom.DomNode;
029: import org.w3c.dom.Node;
030:
031: public class ListUIHandler extends ComponentUIHandler implements
032: ItemListener {
033:
034: public ListUIHandler(AbstractClient client) {
035: super (client);
036: // TODO Auto-generated constructor stub
037: }
038:
039: public Object createComponentInstance(Object parent, DomNode element) {
040: java.awt.List list = new java.awt.List();
041: return list;
042: }
043:
044: protected void populateItems(java.awt.List list, DomNode element) {
045: list.removeAll();
046: List nodes = element.getChildNodes();
047: int count = 0;
048: for (int i = 0; i < nodes.size(); i++) {
049: DomNode childNode = (DomNode) nodes.get(i);
050: if ("item".equals(childNode.getName())) {
051: DomNode childElement = (DomNode) childNode;
052: String value = childElement.getAttribute("value");
053: String selected = childElement.getAttribute("selected");
054: list.add(value, count);
055: if (Boolean.valueOf(selected).booleanValue()) {
056: list.select(count);
057: }
058: count++;
059: }
060: }
061: }
062:
063: public Object updateUI(Object com, DomNode element) {
064: java.awt.List list = (java.awt.List) com;
065: String multipleMode = element.getAttribute("multipleMode");
066: if (Boolean.valueOf(multipleMode).booleanValue()) {
067: list.setMultipleMode(true);
068: }
069: populateItems(list, element);
070: return com;
071: }
072:
073: /**
074: * {@inheritDoc}
075: */
076: protected void registerListeners(Object com) {
077: super .registerListeners(com);
078: ((java.awt.List) com).addItemListener(this );
079: }
080:
081: public void itemStateChanged(ItemEvent e) {
082: handleEvent(e);
083: }
084:
085: public void handleEvent(AWTEvent e) {
086: if (e instanceof ItemEvent) {
087: java.awt.List l = (java.awt.List) e.getSource();
088: String id = getClient().getComponentId(l);
089: if (l.isMultipleMode()) {
090: int selections[] = l.getSelectedIndexes();
091: String selectionStr[] = new String[selections.length];
092: for (int i = 0; i < selections.length; i++) {
093: selectionStr[i] = String.valueOf(selections[i]);
094: }
095: postEvent(l, selectionStr, true);
096: } else {
097: ItemEvent itemEvent = (ItemEvent) e;
098: Integer clicked = (Integer) itemEvent.getItem();
099: postEvent(l, String.valueOf(clicked), true);
100: }
101: }
102: }
103: }
|