jdk/src/share/classes/javax/swing/text/html/OptionListModel.java
author darcy
Sun, 23 Mar 2014 13:49:48 -0700
changeset 23697 e556a715949f
parent 23010 6dadb192ad81
child 25193 187a455af8f8
permissions -rw-r--r--
8034169: Fix serial lint warnings in javax.swing Reviewed-by: alanb, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
     2
 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.BitSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class extends DefaultListModel, and also implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the ListSelectionModel interface, allowing for it to store state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * relevant to a SELECT form element which is implemented as a List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * If SELECT has a size attribute whose value is greater than 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * or if allows multiple selection then a JList is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * represent it and the OptionListModel is used as its model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * It also stores the initial state of the JList, to ensure an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * accurate reset, if the user requests a reset of the form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  @author Sunita Mani
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
    45
@SuppressWarnings("serial") // Superclass is not serializable across versions
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 5506
diff changeset
    46
class OptionListModel<E> extends DefaultListModel<E> implements ListSelectionModel, Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int MIN = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final int MAX = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private int selectionMode = SINGLE_SELECTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int minIndex = MAX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int maxIndex = MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int anchorIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private int leadIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int firstChangedIndex = MAX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int lastChangedIndex = MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean isAdjusting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private BitSet value = new BitSet(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private BitSet initialValue = new BitSet(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected EventListenerList listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected boolean leadAnchorNotificationEnabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public int getMinSelectionIndex() { return isSelectionEmpty() ? -1 : minIndex; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public int getMaxSelectionIndex() { return maxIndex; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public boolean getValueIsAdjusting() { return isAdjusting; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public int getSelectionMode() { return selectionMode; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public void setSelectionMode(int selectionMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        switch (selectionMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        case SINGLE_SELECTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        case SINGLE_INTERVAL_SELECTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        case MULTIPLE_INTERVAL_SELECTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            this.selectionMode = selectionMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new IllegalArgumentException("invalid selectionMode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public boolean isSelectedIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return ((index < minIndex) || (index > maxIndex)) ? false : value.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public boolean isSelectionEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return (minIndex > maxIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public void addListSelectionListener(ListSelectionListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        listenerList.add(ListSelectionListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public void removeListSelectionListener(ListSelectionListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        listenerList.remove(ListSelectionListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Returns an array of all the <code>ListSelectionListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * to this OptionListModel with addListSelectionListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @return all of the <code>ListSelectionListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public ListSelectionListener[] getListSelectionListeners() {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   110
        return listenerList.getListeners(ListSelectionListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Notify listeners that we are beginning or ending a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * series of value changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    protected void fireValueChanged(boolean isAdjusting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        fireValueChanged(getMinSelectionIndex(), getMaxSelectionIndex(), isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Notify ListSelectionListeners that the value of the selection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * in the closed interval firstIndex,lastIndex, has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected void fireValueChanged(int firstIndex, int lastIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        fireValueChanged(firstIndex, lastIndex, getValueIsAdjusting());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param firstIndex The first index in the interval.
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   132
     * @param lastIndex The last index in the interval.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param isAdjusting True if this is the final change in a series of them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        ListSelectionEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (listeners[i] == ListSelectionListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    e = new ListSelectionEvent(this, firstIndex, lastIndex, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                ((ListSelectionListener)listeners[i+1]).valueChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private void fireValueChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (lastChangedIndex == MIN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        /* Change the values before sending the event to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         * listeners in case the event causes a listener to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         * another change to the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int oldFirstChangedIndex = firstChangedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int oldLastChangedIndex = lastChangedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        firstChangedIndex = MAX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        lastChangedIndex = MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        fireValueChanged(oldFirstChangedIndex, oldLastChangedIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // Update first and last change indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private void markAsDirty(int r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        firstChangedIndex = Math.min(firstChangedIndex, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        lastChangedIndex =  Math.max(lastChangedIndex, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    // Set the state at this index and update all relevant state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private void set(int r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (value.get(r)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        value.set(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        Option option = (Option)get(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        option.setSelection(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        markAsDirty(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // Update minimum and maximum indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        minIndex = Math.min(minIndex, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        maxIndex = Math.max(maxIndex, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    // Clear the state at this index and update all relevant state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private void clear(int r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (!value.get(r)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        value.clear(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        Option option = (Option)get(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        option.setSelection(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        markAsDirty(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // Update minimum and maximum indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
           If (r > minIndex) the minimum has not changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
           The case (r < minIndex) is not possible because r'th value was set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
           We only need to check for the case when lowest entry has been cleared,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
           and in this case we need to search for the first value set above it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (r == minIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            for(minIndex = minIndex + 1; minIndex <= maxIndex; minIndex++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if (value.get(minIndex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
           If (r < maxIndex) the maximum has not changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
           The case (r > maxIndex) is not possible because r'th value was set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
           We only need to check for the case when highest entry has been cleared,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
           and in this case we need to search for the first value set below it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (r == maxIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            for(maxIndex = maxIndex - 1; minIndex <= maxIndex; maxIndex--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                if (value.get(maxIndex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        /* Performance note: This method is called from inside a loop in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
           changeSelection() but we will only iterate in the loops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
           above on the basis of one iteration per deselected cell - in total.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
           Ie. the next time this method is called the work of the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
           deselection will not be repeated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
           We also don't need to worry about the case when the min and max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
           values are in their unassigned states. This cannot happen because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
           this method's initial check ensures that the selection was not empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
           and therefore that the minIndex and maxIndex had 'real' values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
           If we have cleared the whole selection, set the minIndex and maxIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
           to their cannonical values so that the next set command always works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
           just by using Math.min and Math.max.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (isSelectionEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            minIndex = MAX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            maxIndex = MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Sets the value of the leadAnchorNotificationEnabled flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @see             #isLeadAnchorNotificationEnabled()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public void setLeadAnchorNotificationEnabled(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        leadAnchorNotificationEnabled = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Returns the value of the leadAnchorNotificationEnabled flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * When leadAnchorNotificationEnabled is true the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * generates notification events with bounds that cover all the changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * the selection plus the changes to the lead and anchor indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Setting the flag to false causes a norrowing of the event's bounds to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * include only the elements that have been selected or deselected since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * the last change. Either way, the model continues to maintain the lead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * and anchor variables internally. The default is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return          the value of the leadAnchorNotificationEnabled flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @see             #setLeadAnchorNotificationEnabled(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public boolean isLeadAnchorNotificationEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return leadAnchorNotificationEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private void updateLeadAnchorIndices(int anchorIndex, int leadIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (leadAnchorNotificationEnabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (this.anchorIndex != anchorIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                if (this.anchorIndex != -1) { // The unassigned state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    markAsDirty(this.anchorIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                markAsDirty(anchorIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (this.leadIndex != leadIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                if (this.leadIndex != -1) { // The unassigned state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    markAsDirty(this.leadIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                markAsDirty(leadIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        this.anchorIndex = anchorIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        this.leadIndex = leadIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private boolean contains(int a, int b, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return (i >= a) && (i <= b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private void changeSelection(int clearMin, int clearMax,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                 int setMin, int setMax, boolean clearFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        for(int i = Math.min(setMin, clearMin); i <= Math.max(setMax, clearMax); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            boolean shouldClear = contains(clearMin, clearMax, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            boolean shouldSet = contains(setMin, setMax, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (shouldSet && shouldClear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                if (clearFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    shouldClear = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    shouldSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (shouldSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                set(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (shouldClear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                clear(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        fireValueChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
   /*   Change the selection with the effect of first clearing the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    *   in the inclusive range [clearMin, clearMax] then setting the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    *   in the inclusive range [setMin, setMax]. Do this in one pass so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    *   that no values are cleared if they would later be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private void changeSelection(int clearMin, int clearMax, int setMin, int setMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        changeSelection(clearMin, clearMax, setMin, setMax, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public void clearSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        removeSelectionInterval(minIndex, maxIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public void setSelectionInterval(int index0, int index1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (index0 == -1 || index1 == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (getSelectionMode() == SINGLE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            index0 = index1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        updateLeadAnchorIndices(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        int clearMin = minIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        int clearMax = maxIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int setMin = Math.min(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        int setMax = Math.max(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        changeSelection(clearMin, clearMax, setMin, setMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public void addSelectionInterval(int index0, int index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (index0 == -1 || index1 == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (getSelectionMode() != MULTIPLE_INTERVAL_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            setSelectionInterval(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        updateLeadAnchorIndices(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        int clearMin = MAX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        int clearMax = MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        int setMin = Math.min(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        int setMax = Math.max(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        changeSelection(clearMin, clearMax, setMin, setMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public void removeSelectionInterval(int index0, int index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (index0 == -1 || index1 == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        updateLeadAnchorIndices(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int clearMin = Math.min(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int clearMax = Math.max(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        int setMin = MAX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        int setMax = MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        changeSelection(clearMin, clearMax, setMin, setMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private void setState(int index, boolean state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            set(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            clear(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Insert length indices beginning before/after index. If the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * at index is itself selected, set all of the newly inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * items, otherwise leave them unselected. This method is typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * called to sync the selection model with a corresponding change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * in the data model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public void insertIndexInterval(int index, int length, boolean before)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        /* The first new index will appear at insMinIndex and the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * one will appear at insMaxIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        int insMinIndex = (before) ? index : index + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        int insMaxIndex = (insMinIndex + length) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        /* Right shift the entire bitset by length, beginning with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * index-1 if before is true, index+1 if it's false (i.e. with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * insMinIndex).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        for(int i = maxIndex; i >= insMinIndex; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            setState(i + length, value.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        /* Initialize the newly inserted indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        boolean setInsertedValues = value.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        for(int i = insMinIndex; i <= insMaxIndex; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            setState(i, setInsertedValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Remove the indices in the interval index0,index1 (inclusive) from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * the selection model.  This is typically called to sync the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * model width a corresponding change in the data model.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * that (as always) index0 can be greater than index1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public void removeIndexInterval(int index0, int index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        int rmMinIndex = Math.min(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        int rmMaxIndex = Math.max(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        int gapLength = (rmMaxIndex - rmMinIndex) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        /* Shift the entire bitset to the left to close the index0, index1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * gap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        for(int i = rmMinIndex; i <= maxIndex; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            setState(i, value.get(i + gapLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public void setValueIsAdjusting(boolean isAdjusting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (isAdjusting != this.isAdjusting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            this.isAdjusting = isAdjusting;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            this.fireValueChanged(isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        String s =  ((getValueIsAdjusting()) ? "~" : "=") + value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        return getClass().getName() + " " + Integer.toString(hashCode()) + " " + s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Returns a clone of the receiver with the same selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * <code>listenerLists</code> are not duplicated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @return a clone of the receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @exception CloneNotSupportedException if the receiver does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *    both (a) implement the <code>Cloneable</code> interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *    and (b) define a <code>clone</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        OptionListModel clone = (OptionListModel)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        clone.value = (BitSet)value.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        clone.listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public int getAnchorSelectionIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return anchorIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public int getLeadSelectionIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        return leadIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Set the anchor selection index, leaving all selection values unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @see #getAnchorSelectionIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @see #setLeadSelectionIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public void setAnchorSelectionIndex(int anchorIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        this.anchorIndex = anchorIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Set the lead selection index, ensuring that values between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * anchor and the new lead are either all selected or all deselected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * If the value at the anchor index is selected, first clear all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * values in the range [anchor, oldLeadIndex], then select all the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * values in the range [anchor, newLeadIndex], where oldLeadIndex is the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * leadIndex and newLeadIndex is the new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * If the value at the anchor index is not selected, do the same thing in reverse,
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 10100
diff changeset
   505
     * selecting values in the old range and deselecting values in the new one.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Generate a single event for this change and notify all listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * For the purposes of generating minimal bounds in this event, do the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * operation in a single pass; that way the first and last index inside the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * ListSelectionEvent that is broadcast will refer to cells that actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * changed value because of this method. If, instead, this operation were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * done in two steps the effect on the selection state would be the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * but two events would be generated and the bounds around the changed values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * would be wider, including cells that had been first cleared and only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * to later be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * This method can be used in the mouseDragged() method of a UI class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * to extend a selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @see #getLeadSelectionIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @see #setAnchorSelectionIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    public void setLeadSelectionIndex(int leadIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        int anchorIndex = this.anchorIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (getSelectionMode() == SINGLE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            anchorIndex = leadIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   529
        int oldMin = Math.min(this.anchorIndex, this.leadIndex);
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   530
        int oldMax = Math.max(this.anchorIndex, this.leadIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        int newMin = Math.min(anchorIndex, leadIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        int newMax = Math.max(anchorIndex, leadIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (value.get(this.anchorIndex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            changeSelection(oldMin, oldMax, newMin, newMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            changeSelection(newMin, newMax, oldMin, oldMax, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        this.anchorIndex = anchorIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        this.leadIndex = leadIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * This method is responsible for storing the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * of the initial selection.  If the selectionMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * is the default, i.e allowing only for SINGLE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * then the very last OPTION that has the selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * attribute set wins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public void setInitialSelection(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        if (initialValue.get(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        if (selectionMode == SINGLE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            // reset to empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            initialValue.and(new BitSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        initialValue.set(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Fetches the BitSet that represents the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * set of selected items in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public BitSet getInitialSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return initialValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
}