import java.awt.*;

class combo_list extends mainform{
	combo cm;
	public combo_list (combo cm){
		super("");
		this.cm = cm;
	}
	public boolean handleEvent(Event e) {
		if (e.id == Event.LIST_SELECT){
			List l = (List)e.target;
			int sel = l.getSelectedIndex();
			if (sel != -1){
				cm.select(sel);
				cm.mf = null;
				dispose();
			}
			return true;
		}
		return super.handleEvent (e);
	}
}

class combo_item{
	public String val1;
	public String val2;
	public combo_item next;
	public combo_item(String val1, String val2){
		this.val1 = val1;
		this.val2 = val2;
		this.next = null;
	}
	public void set (String val1, String val2){
		this.val1 = val1;
		this.val2 = val2;
	}
}
class combo extends mform {
	public TextField text;
	Button but;
	public combo_list mf;
	combo_item first;
	combo_item last;
	int nbitem;
	public combo (mform _parent, String initval, int cols){
		super (_parent,"");
		text = New_string (cols,initval);
		but = New_button (" ");
		mf = null;
		last = first = null;
		nbitem = 0;
	}
	public void addItem (String val1, String val2){
		combo_item item = new combo_item (val1,val2);
		if (first == null) first = item;
		if (last != null) last.next = item;
		last = item;
		nbitem++;
	}
	public void setItem (int no, String val1, String val2){
		if (no >= nbitem){
			addItem (val1,val2);
		}else{
			combo_item pt = first;
			while (no != 0 && pt != null){
				pt = pt.next;
				no--;
			}
			if (pt != null) pt.set (val1,val2);
		}
	}

	public boolean action(Event e, Object arg) {
		boolean ret = false;
		if (e.target == but){
			if (mf != null){
				mf.dispose();
				mf = null;
			}else {
				String cur = text.getText();
				mf = new combo_list (this);
				List l = mf.New_list ("l1",nbitem < 10 ? nbitem+1 : 10,"");
				combo_item item = first;
				int no = 0;
				while (item != null){
					mf.New_list_item ("l1",item.val1 + " " + item.val2);
					if (item.val1.compareTo(cur)==0){
						l.select (no);
					}	
					no++;
					item = item.next;
				}
				mf.Popup();
			}
			ret = true;
		} 
		return ret;
	}
	public String getText(){
		return text.getText();
	}
	public void select (int no){
		combo_item item = first;
		while (item != null){
			if (no == 0){
				text.setText (item.val1);
				break;
			}
			item = item.next;
			no--;
		}
	}
}

