// $Id: ChoiceId.java 3865 2012-10-16 14:21:29Z fabien $ import java.awt.*; import java.util.*; import java.sql.*; /** a AWT Choice with an added integer id. * an additionnal list is maintained to store this information. * all operations are performed on both data structures. * @author Fabien Coelho */ public class ChoiceId extends Choice { // added ATTRIBUTE // keep track of item numbers here private java.util.List ids; // CONSTRUCTORS /** build an empty ChoiceId. */ public ChoiceId() { super(); ids = new ArrayList(); // indexed accesses are expected } /** build a ChoiceId from an sql query. * @param r result set to build from * @throws SQLException on sql errors (e.g. bad column type) * the result set shoud provide 2 columns: * the first one is used as the integer id, * the second as the string item. */ public ChoiceId(ResultSet r) throws SQLException { this(); // À compléter exploitation du "ResultSet" pour construire le "ChoiceId" // - le "ResultSet" à deux colonnes (attributs) : // 1. entier // 2. texte // - on appelle addItemId avec chaque élément // en précisant son texte et son numéro } // NEW METHODS public void addItemId(String item, int id) { super.addItem(item); ids.add(new Integer(id)); } public int getSelectedId() { return ((Integer) ids.get(super.getSelectedIndex())).intValue(); } // INHERITED METHODS are redefined public void insert(String item, int index) { super.insert(item, index); ids.add(index, new Integer(-1)); } public void add(String item) { this.addItemId(item, -1); } public void addItem(String item) { this.addItemId(item, -1); } public void remove(int position) { super.remove(position); ids.remove(position); } public void remove(String item) { for (int i=0; i