package awtExtExamples ; /* * * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.awt.Panel; import java.awt.Event; import java.awt.TextField; import awtExt.*; import sysExt.*; /** * A test of an awtExt.List controlled via user defined methods. * The list has 2 callback methods registered in its eventDispatch object. * Each button on the bottom of the window allows you to add, delete, clear, * select, deselect items. Each one of these * buttons invokes a user registered method. An awtExt.Checkbox * controls the list's single/multiple selection mechanism. * */ public class ListTest extends Frame { awtExt.List list; TextField text; public ListTest() { super("ListTest"); list = new awtExt.List(); try { list.addItem("one"); list.addItem("two"); list.addItem("three"); list.addItem("four"); list.addItem("five"); list.addItem("six"); list.addItem("seven"); list.addItem("eight"); list.addItem("nine"); list.addItem("ten"); /* ** Add two event handlers to the list object. One for the LIST_SELECT event, ** and one for the LIST_DESELECT event. */ list.eventDispatch.LIST_SELECT = Callback.newRef( this, "listSelect" ); list.eventDispatch.LIST_DESELECT = Callback.newRef( this, "listDeselect" ); add("Center", list); Panel p = new Panel(); p.add(text=new TextField(20)); p.add(new Checkbox("multiple",this, "multiple")); p.add(new Button("add", this, "add")); p.add(new Button("del", this, "delete" ) ); p.add(new Button("clear", this, "clear" ) ); p.add(new Button("select", this, "select" ) ); p.add(new Button("deselect", this, "deselect" )); p.add(new Button("print", this, "print")); add("South", p); move(400, 200); show(); } catch( Exception e ) { e.printStackTrace();} ; } /** * Called back method from List. Invoked when LIST_SELECT event occurs **/ public void listSelect( CallbackInfo cbi ) { System.out.println(" List select callback: " + ((Integer)cbi.evt.arg).intValue()); } /** * Called back method from List. Invoked when LIST_DESELECT event occurs **/ public void listDeselect( CallbackInfo cbi ) { System.out.println("List deselect callback: " + ((Integer)cbi.evt.arg).intValue()); } /** * Called back method from Checkbox **/ public void multiple( CallbackInfo cbi ) { list.setMultipleSelections(((Boolean)cbi.evt.arg).booleanValue()); } /** * Called back method from button **/ public void print ( CallbackInfo cbi ) { System.out.println("-- list contents --"); int n = list.countItems(); for (int i=0; i < n; i++) { System.out.println(list.getItem(i)); } System.out.println("-- list selected --"); String sel[] = list.getSelectedItems(); for (int i = 0 ; i < sel.length ; i++) { System.out.println(sel[i]); } } /** * Called back method from button **/ public void add( CallbackInfo cbi ) { list.addItem(text.getText()); } /** * Called back method from button **/ public void delete( CallbackInfo cbi ) { int index = argToIndex(text.getText()); if (index == -1) { System.out.println("Invalid index"); } else { list.delItem(index); } } /** * Called back method from button **/ public void select( CallbackInfo cbi ) { int index = argToIndex(text.getText()); if (index == -1) { System.out.println("Invalid index"); } else { list.select(index); } } /** * Called back method from button **/ public void deselect( CallbackInfo cbi ) { int index = argToIndex(text.getText()); if (index == -1) { System.out.println("Invalid index"); } else { list.deselect(index); } } /** * Called back method from button **/ public void clear( CallbackInfo cbi) { list.clear(); } int argToIndex(String arg) { int n = list.countItems(); for (int i = 0; i < n; i++) { if (arg.equals(list.getItem(i))) { return i; } } return -1; } public static void main(String args[]) { new ListTest(); } }