001: /* ListableChoiceGroup.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: May 22, 2007 9:40:16 AM, Created by henrichen
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zkmob.ui;
020:
021: import java.util.Enumeration;
022: import java.util.Vector;
023:
024: import javax.microedition.lcdui.ChoiceGroup;
025: import javax.microedition.lcdui.Form;
026: import javax.microedition.lcdui.Image;
027:
028: import org.zkoss.zkmob.Imageable;
029: import org.zkoss.zkmob.Itemable;
030: import org.zkoss.zkmob.Listable;
031: import org.zkoss.zkmob.UiManager;
032: import org.zkoss.zkmob.ZkComponent;
033:
034: /**
035: * Implement Listable ChoiceGroup.
036: *
037: * @author henrichen
038: *
039: */
040: public class ZkChoiceGroup extends ChoiceGroup implements Listable,
041: ZkComponent, Itemable {
042: private String _id;
043: private Boolean _onSelect;
044: private ZkDesktop _zk;
045: private ZkForm _form;
046: private Vector _listitems = new Vector(32);
047: private boolean _handlekid;
048:
049: public ZkChoiceGroup(ZkDesktop zk, String id, String title,
050: int choiceType, Boolean onSelect) {
051: super (title, choiceType);
052: _id = id;
053: _zk = zk;
054: _onSelect = onSelect;
055: }
056:
057: public Vector getItems() {
058: return _listitems;
059: }
060:
061: //--Listable--//
062: public void appendChild(ZkComponent li) {
063: if (_handlekid) { //avoid dead loop
064: return;
065: }
066: try {
067: _handlekid = true;
068: if (li instanceof ZkListItem) {
069: final ZkListItem comp = (ZkListItem) li;
070: super .append(comp.getLabel(), null);
071: _listitems.addElement(li);
072: li.setParent(this );
073: UiManager.loadImageOnThread(comp, li.getZkDesktop()
074: .getHostURL(), li.getZkDesktop().getPathURL(),
075: comp.getImageSrc());
076: } else {
077: addCommand((ZkCommand) li);
078: li.setParent(this );
079: }
080: } finally {
081: _handlekid = false;
082: }
083: }
084:
085: public void insertChild(int index, ZkComponent li) {
086: try {
087: _handlekid = true;
088: if (li instanceof ZkListItem) {
089: final ZkListItem comp = (ZkListItem) li;
090: super .insert(index, comp.getLabel(), null);
091: _listitems.insertElementAt(li, index);
092: li.setParent(this );
093: UiManager.loadImageOnThread(comp, li.getZkDesktop()
094: .getHostURL(), li.getZkDesktop().getPathURL(),
095: comp.getImageSrc());
096: } else {
097: addCommand((ZkCommand) li);
098: li.setParent(this );
099: }
100: } finally {
101: _handlekid = false;
102: }
103: }
104:
105: public int indexOf(ZkComponent li) {
106: return _listitems.indexOf(li);
107: }
108:
109: public void delete(int index) {
110: super .delete(index);
111: _listitems.removeElementAt(index);
112: }
113:
114: public Boolean getOnSelect() {
115: return _onSelect;
116: }
117:
118: //--ZkComponent--//
119: public String getId() {
120: return _id;
121: }
122:
123: public ZkComponent getParent() {
124: return (ZkComponent) _form;
125: }
126:
127: public void setParent(ZkComponent parent) {
128: if (_form != parent) { //yes, !=, not !equals
129: if (_form != null) {
130: _form.removeItem(this );
131: }
132: _form = (ZkForm) parent;
133: ZkDesktop newzk = null;
134: if (_form != null) {
135: _form.appendChild(this );
136: newzk = _form.getZkDesktop();
137: }
138: if (_zk != newzk) {
139: _zk = newzk;
140: }
141: }
142: }
143:
144: public ZkDesktop getZkDesktop() {
145: return _zk;
146: }
147:
148: public void setAttr(String attr, String val) {
149: }
150:
151: //--Itemable--//
152: public Form getForm() {
153: return _form;
154: }
155: }
|