/* Copyright 2000 Charles G. Wright * This software may be distributed under the terms of the * GNU General Public License. * * $Id: CWselect.java,v 1.1 2000-06-15 10:14:54-05 chuckles Exp chuckles $ */ import java.awt.*; import java.awt.event.*; import java.io.*; /** This class implements a selector with buttons to increment/decrement selection */ class CWselect extends Panel implements ActionListener, ItemListener{ Label lab; Button up; Button down; Choice sel; int maxIndex; ActionListener parent; boolean wrap_up = false; boolean wrap_down = false; ActionEvent ae; public CWselect(String[] choices, String label, int init_value){ lab = new Label(label); down = new Button("<"); up = new Button(">"); sel = new Choice(); for (int i = 0; i < choices.length; i++) sel.addItem(choices[i]); sel.select(init_value); maxIndex = sel.getItemCount() - 1; this.parent = parent; up.addActionListener(this); down.addActionListener(this); sel.addItemListener(this); add(lab); add(down); add(sel); add(up); ae = new ActionEvent(this, 0, label); //drawRect(0, 0, Component.getSize().width, Component.getSize().height); } // returns the value in the text box of the selectGizmo public int getSelectedIndex(){ return(sel.getSelectedIndex()); } public void setMaxIndex(int i){ maxIndex = i; //System.out.println("Setting max index to " + i); if(sel.getSelectedIndex() > maxIndex) sel.select(maxIndex); } public void incrementIndex(){ int i; if ((i = sel.getSelectedIndex()) < maxIndex){ sel.select(i+1); wrap_up = false; } else { sel.select(0); wrap_up = true; } } public void decrementIndex(){ int i; if ((i = sel.getSelectedIndex()) > 0){ sel.select(i - 1); wrap_down = false; } else { sel.select(maxIndex); wrap_down = true; } } // This method is called after this object is created, // to register the object that provides the actionPerformed() // method. I might have just passed "parent" in the // constructor, but this gives the flexibility to // put the event handler elsewhere, such as in a // central event handler object. public void addActionListener(ActionListener parent){ this.parent = parent; } public void actionPerformed(ActionEvent e){ Object source = e.getSource(); int i = sel.getSelectedIndex(); if (source == up) { incrementIndex(); //System.out.println("getting ready to update"); //System.out.println(parent); //System.out.println(this); //parent.updateSomething(this); parent.actionPerformed(ae); } if (source == down){ decrementIndex(); //parent.updateSomething(this); parent.actionPerformed(ae); } } public void itemStateChanged(ItemEvent e){ //parent.updateSomething(this); parent.actionPerformed(ae); //System.out.println("select box changed"); } }