< prev index next >

## src/java.desktop/share/classes/javax/swing/text/html/OptionListModel.java

Print this page

    rev 48358 : Work in progress for JDK-6303622
    Use generic in Swing components like JComboBox
    1. Added generic return type and parameter type for JComboBox.
    2. Modified JList related classes as well.

      26
      27 import javax.swing.*;
      28 import javax.swing.event.*;
      29 import java.util.BitSet;
      30 import java.io.Serializable;
      31
      32
      33 /**
      34  * This class extends DefaultListModel, and also implements
      35  * the ListSelectionModel interface, allowing for it to store state
      36  * relevant to a SELECT form element which is implemented as a List.
      37  * If SELECT has a size attribute whose value is greater than 1,
      38  * or if allows multiple selection then a JList is used to
      39  * represent it and the OptionListModel is used as its model.
      40  * It also stores the initial state of the JList, to ensure an
      41  * accurate reset, if the user requests a reset of the form.
      42  *
      43  * @author Sunita Mani
      44  */
      45 @SuppressWarnings("serial") // Superclass is not serializable across versions
      46 class OptionListModel<E> extends DefaultListModel<E> implements ListSelectionModel, Serializable {
      47
      48
      49     private static final int MIN = -1;
      50     private static final int MAX = Integer.MAX_VALUE;
      51     private int selectionMode = SINGLE_SELECTION;
      52     private int minIndex = MAX;
      53     private int maxIndex = MIN;
      54     private int anchorIndex = -1;
      55     private int leadIndex = -1;
      56     private int firstChangedIndex = MAX;
      57     private int lastChangedIndex = MIN;
      58     private boolean isAdjusting = false;
      59     private BitSet value = new BitSet(32);
      60     private BitSet initialValue = new BitSet(32);
      61     protected EventListenerList listenerList = new EventListenerList();
      62
      63     protected boolean leadAnchorNotificationEnabled = true;
      64
      65     public int getMinSelectionIndex() { return isSelectionEmpty() ? -1 : minIndex; }
      66
     159         int oldFirstChangedIndex = firstChangedIndex;
     160         int oldLastChangedIndex = lastChangedIndex;
     161         firstChangedIndex = MAX;
     162         lastChangedIndex = MIN;
     163         fireValueChanged(oldFirstChangedIndex, oldLastChangedIndex);
     164     }
     165
     166
     167     // Update first and last change indices
     168     private void markAsDirty(int r) {
     169         firstChangedIndex = Math.min(firstChangedIndex, r);
     170         lastChangedIndex =  Math.max(lastChangedIndex, r);
     171     }
     172
     173     // Set the state at this index and update all relevant state.
     174     private void set(int r) {
     175         if (value.get(r)) {
     176             return;
     177         }
     178         value.set(r);
     179         Option option = (Option)get(r);
     180         option.setSelection(true);
     181         markAsDirty(r);
     182
     183         // Update minimum and maximum indices
     184         minIndex = Math.min(minIndex, r);
     185         maxIndex = Math.max(maxIndex, r);
     186     }
     187
     188     // Clear the state at this index and update all relevant state.
     189     private void clear(int r) {
     190         if (!value.get(r)) {
     191             return;
     192         }
     193         value.clear(r);
     194         Option option = (Option)get(r);
     195         option.setSelection(false);
     196         markAsDirty(r);
     197
     198         // Update minimum and maximum indices
     199         /*
     200            If (r > minIndex) the minimum has not changed.
     201            The case (r < minIndex) is not possible because r'th value was set.
     202            We only need to check for the case when lowest entry has been cleared,
     203            and in this case we need to search for the first value set above it.
     204         */
     205         if (r == minIndex) {
     206             for(minIndex = minIndex + 1; minIndex <= maxIndex; minIndex++) {
     207                 if (value.get(minIndex)) {
     208                     break;
     209                 }
     210             }
     211         }
     212         /*
     213            If (r < maxIndex) the maximum has not changed.
     214            The case (r > maxIndex) is not possible because r'th value was set.
     451             this.isAdjusting = isAdjusting;
     452             this.fireValueChanged(isAdjusting);
     453         }
     454     }
     455
     456
     457     public String toString() {
     458         String s =  ((getValueIsAdjusting()) ? "~" : "=") + value.toString();
     459         return getClass().getName() + " " + Integer.toString(hashCode()) + " " + s;
     460     }
     461
     462     /**
     463      * Returns a clone of the receiver with the same selection.
     464      * <code>listenerLists</code> are not duplicated.
     465      *
     466      * @return a clone of the receiver
     467      * @exception CloneNotSupportedException if the receiver does not
     468      *    both (a) implement the <code>Cloneable</code> interface
     469      *    and (b) define a <code>clone</code> method
     470      */
     471     public Object clone() throws CloneNotSupportedException {
     472         @SuppressWarnings("unchecked")
     473         OptionListModel<E> clone = (OptionListModel)super.clone();
     474         clone.value = (BitSet)value.clone();
     475         clone.listenerList = new EventListenerList();
     476         return clone;
     477     }
     478
     479     public int getAnchorSelectionIndex() {
     480         return anchorIndex;
     481     }
     482
     483     public int getLeadSelectionIndex() {
     484         return leadIndex;
     485     }
     486
     487     /**
     488      * Set the anchor selection index, leaving all selection values unchanged.
     489      *
     490      * @see #getAnchorSelectionIndex
     491      * @see #setLeadSelectionIndex
     492      */
     493     public void setAnchorSelectionIndex(int anchorIndex) {
      26
      27 import javax.swing.*;
      28 import javax.swing.event.*;
      29 import java.util.BitSet;
      30 import java.io.Serializable;
      31
      32
      33 /**
      34  * This class extends DefaultListModel, and also implements
      35  * the ListSelectionModel interface, allowing for it to store state
      36  * relevant to a SELECT form element which is implemented as a List.
      37  * If SELECT has a size attribute whose value is greater than 1,
      38  * or if allows multiple selection then a JList is used to
      39  * represent it and the OptionListModel is used as its model.
      40  * It also stores the initial state of the JList, to ensure an
      41  * accurate reset, if the user requests a reset of the form.
      42  *
      43  * @author Sunita Mani
      44  */
      45 @SuppressWarnings("serial") // Superclass is not serializable across versions
      46 class OptionListModel extends DefaultListModel<Option> implements ListSelectionModel, Serializable {
      47
      48
      49     private static final int MIN = -1;
      50     private static final int MAX = Integer.MAX_VALUE;
      51     private int selectionMode = SINGLE_SELECTION;
      52     private int minIndex = MAX;
      53     private int maxIndex = MIN;
      54     private int anchorIndex = -1;
      55     private int leadIndex = -1;
      56     private int firstChangedIndex = MAX;
      57     private int lastChangedIndex = MIN;
      58     private boolean isAdjusting = false;
      59     private BitSet value = new BitSet(32);
      60     private BitSet initialValue = new BitSet(32);
      61     protected EventListenerList listenerList = new EventListenerList();
      62
      63     protected boolean leadAnchorNotificationEnabled = true;
      64
      65     public int getMinSelectionIndex() { return isSelectionEmpty() ? -1 : minIndex; }
      66
     159         int oldFirstChangedIndex = firstChangedIndex;
     160         int oldLastChangedIndex = lastChangedIndex;
     161         firstChangedIndex = MAX;
     162         lastChangedIndex = MIN;
     163         fireValueChanged(oldFirstChangedIndex, oldLastChangedIndex);
     164     }
     165
     166
     167     // Update first and last change indices
     168     private void markAsDirty(int r) {
     169         firstChangedIndex = Math.min(firstChangedIndex, r);
     170         lastChangedIndex =  Math.max(lastChangedIndex, r);
     171     }
     172
     173     // Set the state at this index and update all relevant state.
     174     private void set(int r) {
     175         if (value.get(r)) {
     176             return;
     177         }
     178         value.set(r);
     179         Option option = get(r);
     180         option.setSelection(true);
     181         markAsDirty(r);
     182
     183         // Update minimum and maximum indices
     184         minIndex = Math.min(minIndex, r);
     185         maxIndex = Math.max(maxIndex, r);
     186     }
     187
     188     // Clear the state at this index and update all relevant state.
     189     private void clear(int r) {
     190         if (!value.get(r)) {
     191             return;
     192         }
     193         value.clear(r);
     194         Option option = get(r);
     195         option.setSelection(false);
     196         markAsDirty(r);
     197
     198         // Update minimum and maximum indices
     199         /*
     200            If (r > minIndex) the minimum has not changed.
     201            The case (r < minIndex) is not possible because r'th value was set.
     202            We only need to check for the case when lowest entry has been cleared,
     203            and in this case we need to search for the first value set above it.
     204         */
     205         if (r == minIndex) {
     206             for(minIndex = minIndex + 1; minIndex <= maxIndex; minIndex++) {
     207                 if (value.get(minIndex)) {
     208                     break;
     209                 }
     210             }
     211         }
     212         /*
     213            If (r < maxIndex) the maximum has not changed.
     214            The case (r > maxIndex) is not possible because r'th value was set.
     451             this.isAdjusting = isAdjusting;
     452             this.fireValueChanged(isAdjusting);
     453         }
     454     }
     455
     456
     457     public String toString() {
     458         String s =  ((getValueIsAdjusting()) ? "~" : "=") + value.toString();
     459         return getClass().getName() + " " + Integer.toString(hashCode()) + " " + s;
     460     }
     461
     462     /**
     463      * Returns a clone of the receiver with the same selection.
     464      * <code>listenerLists</code> are not duplicated.
     465      *
     466      * @return a clone of the receiver
     467      * @exception CloneNotSupportedException if the receiver does not
     468      *    both (a) implement the <code>Cloneable</code> interface
     469      *    and (b) define a <code>clone</code> method
     470      */
     471     public OptionListModel clone() throws CloneNotSupportedException {
     472         @SuppressWarnings("unchecked")
     473         OptionListModel clone = (OptionListModel)super.clone();
     474         clone.value = (BitSet)value.clone();
     475         clone.listenerList = new EventListenerList();
     476         return clone;
     477     }
     478
     479     public int getAnchorSelectionIndex() {
     480         return anchorIndex;
     481     }
     482
     483     public int getLeadSelectionIndex() {
     484         return leadIndex;
     485     }
     486
     487     /**
     488      * Set the anchor selection index, leaving all selection values unchanged.
     489      *
     490      * @see #getAnchorSelectionIndex
     491      * @see #setLeadSelectionIndex
     492      */
     493     public void setAnchorSelectionIndex(int anchorIndex) {

< prev index next >