1: package ste.pendu.wordeditor;

  3: import java.beans.PropertyChangeListener;
  4: import java.beans.PropertyChangeEvent;
  5: import javax.swing.JPanel;
  6: import javax.swing.JList;
  7: import javax.swing.JScrollPane;
  8: import javax.swing.AbstractListModel;
  9: import java.awt.GridLayout;

 11: /**
 12:  * Liste des mots.
 13:  * 
 14:  * 
 15:  */
 16: public class JListBean extends JPanel
 17: {

 19:   /**
 20:    * Le modèle pour afficher les données dans la liste.
 21:    */
 22:   private JListModel model = null;

 24:   /**
 25:    * La vue du bean.
 26:    */
 27:   private JList list = null;

 29:   /**
 30:    * Modèle de la liste.
 31:    */
 32:   private class JListModel extends AbstractListModel
 33:                                    implements PropertyChangeListener
 34:   {
 35:     private WordListModel wlModel = null;

 37:     private int size=0;

 39:     public JListModel()
 40:     {}

 42:     public void setWordListModel(WordListModel wlModel)
 43:     {
 44:       this.wlModel=wlModel;
 45:       if (this.wlModel != null)
 46:         {
 47:           this.wlModel.addPropertyChangeListener(this);
 48:         }
 49:     }

 51:     public WordListModel getWordListModel()
 52:     {
 53:       return this.wlModel;
 54:     }

 56:     public Object getElementAt(int index)
 57:     {
 58:       Object o = null;
 59:       if (this.wlModel != null)
 60:         {
 61:           o=this.wlModel.getWord(index);
 62:         }
 63:       return o;
 64:     }

 66:     public int getSize()
 67:     {
 68:       return this.size;
 69:     }
 70:     
 71:     public void propertyChange(PropertyChangeEvent evt)
 72:     {
 73:       int newSize=this.wlModel.getWordListSize();
 74:       if (newSize!=this.size)
 75:         {
 76:           super.fireContentsChanged(this.wlModel, this.size, newSize);
 77:         }
 78:       this.size=newSize;
 79:     }
 80:     
 81:   }

 83:   public JListBean(WordListModel wlModel)
 84:   {
 85:     super(new GridLayout());
 86:     
 87:     this.model = new JListModel();
 88:     this.model.setWordListModel(wlModel);
 89:     this.list = new JList(this.model);
 90:     this.add(new JScrollPane(list));
 91:   }

 93:   /**
 94:    * Retourne l'index sélectionné; -1 si aucun item n'est sélectionné.
 95:    * 
 96:    * @return l'index sélectionné; -1 si aucun n'est sélectionné
 97:    */
 98:   public int getSelectedIndex()
 99:   {
100:     return this.list.getSelectedIndex();
101:   }

103:   /**
104:    * Retourne un tableau des indexes sélectionnés.
105:    * 
106:    * @return un tableau d'indices
107:    */
108:   public int[] getSelectedIndices()
109:   {
110:     return this.list.getSelectedIndices();
111:   }

113:   /**
114:    * Retourne le mot sélectionné, ou null s'il n'y en a aucun
115:    * sélectionné.
116:    * 
117:    * @return le mot sélectionné
118:    */
119:   public Object getSelectedValue()
120:   {
121:     return this.list.getSelectedValue();
122:   }

124:   /**
125:    * Retourne un tableau des mots sélectionnés.
126:    * 
127:    * @return un tableau des mots sélectionnés
128:    */
129:   public Object[] getSelectedValues()
130:   {
131:     return this.list.getSelectedValues();
132:   }

134:   /**
135:    * Donne l'état de la sélection.
136:    * 
137:    * @return <code>true</code> si rien n'est sélectionné
138:    */
139:   public boolean isSelectionEmpty()
140:   {
141:     return this.list.isSelectionEmpty();
142:   }

144: }