src/java.desktop/share/classes/javax/swing/DefaultRowSorter.java
author pbansal
Thu, 01 Feb 2018 16:38:25 +0530
changeset 48733 8850bd195bbd
parent 47216 71c04702a3d5
permissions -rw-r--r--
7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache Reviewed-by: serb, jdv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36914
f790ef973830 8078514: Nightly: api/javax_swing/DefaultRowSorter/index_ModelStructChanged failure
ssadetsky
parents: 25859
diff changeset
     2
 * Copyright (c) 2005, 2016, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.text.Collator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * An implementation of <code>RowSorter</code> that provides sorting and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * filtering around a grid-based data model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Beyond creating and installing a <code>RowSorter</code>, you very rarely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * need to interact with one directly.  Refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * {@link javax.swing.table.TableRowSorter TableRowSorter} for a concrete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * implementation of <code>RowSorter</code> for <code>JTable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Sorting is done based on the current <code>SortKey</code>s, in order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * If two objects are equal (the <code>Comparator</code> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * column returns 0) the next <code>SortKey</code> is used.  If no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <code>SortKey</code>s remain or the order is <code>UNSORTED</code>, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the order of the rows in the model is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Sorting of each column is done by way of a <code>Comparator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * that you can specify using the <code>setComparator</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * If a <code>Comparator</code> has not been specified, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <code>Comparator</code> returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>Collator.getInstance()</code> is used on the results of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * calling <code>toString</code> on the underlying objects.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <code>Comparator</code> is never passed <code>null</code>.  A
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
    55
 * <code>null</code> value is treated as occurring before a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * non-<code>null</code> value, and two <code>null</code> values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * considered equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * If you specify a <code>Comparator</code> that casts its argument to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * a type other than that provided by the model, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <code>ClassCastException</code> will be thrown when the data is sorted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * In addition to sorting, <code>DefaultRowSorter</code> provides the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * ability to filter rows.  Filtering is done by way of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <code>RowFilter</code> that is specified using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <code>setRowFilter</code> method.  If no filter has been specified all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * rows are included.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * By default, rows are in unsorted order (the same as the model) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * every column is sortable. The default <code>Comparator</code>s are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * documented in the subclasses (for example, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * javax.swing.table.TableRowSorter TableRowSorter}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * If the underlying model structure changes (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <code>modelStructureChanged</code> method is invoked) the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * are reset to their default values: <code>Comparator</code>s by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * column, current sort order, and whether each column is sortable. To
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * find the default <code>Comparator</code>s, see the concrete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * implementation (for example, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * javax.swing.table.TableRowSorter TableRowSorter}).  The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * sort order is unsorted (the same as the model), and columns are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * sortable by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <code>DefaultRowSorter</code> is an abstract class.  Concrete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * subclasses must provide access to the underlying data by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * {@code setModelWrapper}. The {@code setModelWrapper} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <b>must</b> be invoked soon after the constructor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * called, ideally from within the subclass's constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Undefined behavior will result if you use a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * DefaultRowSorter} without specifying a {@code ModelWrapper}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <code>DefaultRowSorter</code> has two formal type parameters.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * first type parameter corresponds to the class of the model, for example
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <code>DefaultTableModel</code>.  The second type parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * corresponds to the class of the identifier passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <code>RowFilter</code>.  Refer to <code>TableRowSorter</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <code>RowFilter</code> for more details on the type parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @param <M> the type of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @param <I> the type of the identifier passed to the <code>RowFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @see javax.swing.table.TableRowSorter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @see javax.swing.table.DefaultTableModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @see java.text.Collator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
public abstract class DefaultRowSorter<M, I> extends RowSorter<M> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Whether or not we resort on TableModelEvent.UPDATEs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private boolean sortsOnUpdates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * View (JTable) -> model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private Row[] viewToModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * model -> view (JTable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private int[] modelToView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Comparators specified by column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
   125
    private Comparator<?>[] comparators;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Whether or not the specified column is sortable, by column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private boolean[] isSortable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Cached SortKeys for the current sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private SortKey[] cachedSortKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Cached comparators for the current sort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
   140
    private Comparator<?>[] sortComparators;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Developer supplied Filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private RowFilter<? super M,? super I> filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Value passed to the filter.  The same instance is passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * filter for different rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private FilterEntry filterEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * The sort keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private List<SortKey> sortKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Whether or not to use getStringValueAt.  This is indexed by column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private boolean[] useToString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Indicates the contents are sorted.  This is used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * getSortsOnUpdates is false and an update event is received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private boolean sorted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Maximum number of sort keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private int maxSortKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Provides access to the data we're sorting/filtering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private ModelWrapper<M,I> modelWrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Size of the model. This is used to enforce error checking within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * the table changed notification methods (such as rowsInserted).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private int modelRowCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
42728
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   185
    // Whether to print warning about JDK-8160087
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   186
    private static boolean warning8160087 = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Creates an empty <code>DefaultRowSorter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public DefaultRowSorter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        sortKeys = Collections.emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        maxSortKeys = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Sets the model wrapper providing the data that is being sorted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * filtered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param modelWrapper the model wrapper responsible for providing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *         data that gets sorted and filtered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @throws IllegalArgumentException if {@code modelWrapper} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *         {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    protected final void setModelWrapper(ModelWrapper<M,I> modelWrapper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (modelWrapper == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                "modelWrapper most be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        ModelWrapper<M,I> last = this.modelWrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        this.modelWrapper = modelWrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (last != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            modelStructureChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // If last is null, we're in the constructor. If we're in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            // the constructor we don't want to call to overridable methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            modelRowCount = getModelWrapper().getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Returns the model wrapper providing the data that is being sorted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * filtered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return the model wrapper responsible for providing the data that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *         gets sorted and filtered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    protected final ModelWrapper<M,I> getModelWrapper() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return modelWrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Returns the underlying model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return the underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public final M getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return getModelWrapper().getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Sets whether or not the specified column is sortable.  The specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * value is only checked when <code>toggleSortOrder</code> is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * It is still possible to sort on a column that has been marked as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * unsortable by directly setting the sort keys.  The default is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param column the column to enable or disable sorting on, in terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *        of the underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param sortable whether or not the specified column is sortable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @throws IndexOutOfBoundsException if <code>column</code> is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *         the range of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @see #toggleSortOrder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @see #setSortKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public void setSortable(int column, boolean sortable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        checkColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (isSortable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            isSortable = new boolean[getModelWrapper().getColumnCount()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            for (int i = isSortable.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                isSortable[i] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        isSortable[column] = sortable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Returns true if the specified column is sortable; otherwise, false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param column the column to check sorting for, in terms of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *        underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return true if the column is sortable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @throws IndexOutOfBoundsException if column is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *         the range of the underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public boolean isSortable(int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        checkColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return (isSortable == null) ? true : isSortable[column];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Sets the sort keys. This creates a copy of the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * {@code List}; subsequent changes to the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * {@code List} do not effect this {@code DefaultRowSorter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * If the sort keys have changed this triggers a sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param sortKeys the new <code>SortKeys</code>; <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *        is a shorthand for specifying an empty list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *        indicating that the view should be unsorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @throws IllegalArgumentException if any of the values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *         <code>sortKeys</code> are null or have a column index outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *         the range of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public void setSortKeys(List<? extends SortKey> sortKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        List<SortKey> old = this.sortKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (sortKeys != null && sortKeys.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            int max = getModelWrapper().getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            for (SortKey key : sortKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                if (key == null || key.getColumn() < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        key.getColumn() >= max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    throw new IllegalArgumentException("Invalid SortKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            this.sortKeys = Collections.unmodifiableList(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    new ArrayList<SortKey>(sortKeys));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            this.sortKeys = Collections.emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (!this.sortKeys.equals(old)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            fireSortOrderChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (viewToModel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                // Currently unsorted, use sort so that internal fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                // are correctly set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                sort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                sortExistingData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Returns the current sort keys.  This returns an unmodifiable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * {@code non-null List}. If you need to change the sort keys,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * make a copy of the returned {@code List}, mutate the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * and invoke {@code setSortKeys} with the new list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @return the current sort order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public List<? extends SortKey> getSortKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return sortKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Sets the maximum number of sort keys.  The number of sort keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * determines how equal values are resolved when sorting.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * example, assume a table row sorter is created and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <code>setMaxSortKeys(2)</code> is invoked on it. The user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * clicks the header for column 1, causing the table rows to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * sorted based on the items in column 1.  Next, the user clicks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * the header for column 2, causing the table to be sorted based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * on the items in column 2; if any items in column 2 are equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * then those particular rows are ordered based on the items in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * column 1. In this case, we say that the rows are primarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * sorted on column 2, and secondarily on column 1.  If the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * then clicks the header for column 3, then the items are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * primarily sorted on column 3 and secondarily sorted on column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * 2.  Because the maximum number of sort keys has been set to 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * with <code>setMaxSortKeys</code>, column 1 no longer has an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * effect on the order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * The maximum number of sort keys is enforced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <code>toggleSortOrder</code>.  You can specify more sort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * keys by invoking <code>setSortKeys</code> directly and they will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * all be honored.  However if <code>toggleSortOrder</code> is subsequently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * invoked the maximum number of sort keys will be enforced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * The default value is 3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param max the maximum number of sort keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @throws IllegalArgumentException if <code>max</code> &lt; 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public void setMaxSortKeys(int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (max < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            throw new IllegalArgumentException("Invalid max");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        maxSortKeys = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Returns the maximum number of sort keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @return the maximum number of sort keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public int getMaxSortKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return maxSortKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * If true, specifies that a sort should happen when the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * model is updated (<code>rowsUpdated</code> is invoked).  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * example, if this is true and the user edits an entry the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * location of that item in the view may change.  The default is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param sortsOnUpdates whether or not to sort on update events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public void setSortsOnUpdates(boolean sortsOnUpdates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        this.sortsOnUpdates = sortsOnUpdates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Returns true if  a sort should happen when the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * model is updated; otherwise, returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @return whether or not to sort when the model is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public boolean getSortsOnUpdates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return sortsOnUpdates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Sets the filter that determines which rows, if any, should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * hidden from the view.  The filter is applied before sorting.  A value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * of <code>null</code> indicates all values from the model should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * included.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * <code>RowFilter</code>'s <code>include</code> method is passed an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * <code>Entry</code> that wraps the underlying model.  The number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * of columns in the <code>Entry</code> corresponds to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * number of columns in the <code>ModelWrapper</code>.  The identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * comes from the <code>ModelWrapper</code> as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * This method triggers a sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @param filter the filter used to determine what entries should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *        included
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    public void setRowFilter(RowFilter<? super M,? super I> filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        sort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Returns the filter that determines which rows, if any, should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * be hidden from view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @return the filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public RowFilter<? super M,? super I> getRowFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        return filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Reverses the sort order from ascending to descending (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * descending to ascending) if the specified column is already the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * primary sorted column; otherwise, makes the specified column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * the primary sorted column, with an ascending sort order.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * the specified column is not sortable, this method has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param column index of the column to make the primary sorted column,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *        in terms of the underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @see #setSortable(int,boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @see #setMaxSortKeys(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public void toggleSortOrder(int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        checkColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (isSortable(column)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            List<SortKey> keys = new ArrayList<SortKey>(getSortKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            SortKey sortKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            int sortIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            for (sortIndex = keys.size() - 1; sortIndex >= 0; sortIndex--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                if (keys.get(sortIndex).getColumn() == column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (sortIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                // Key doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                sortKey = new SortKey(column, SortOrder.ASCENDING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                keys.add(0, sortKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            else if (sortIndex == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                // It's the primary sorting key, toggle it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                keys.set(0, toggle(keys.get(0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                // It's not the first, but was sorted on, remove old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                // entry, insert as first with ascending.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                keys.remove(sortIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                keys.add(0, new SortKey(column, SortOrder.ASCENDING));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (keys.size() > getMaxSortKeys()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                keys = keys.subList(0, getMaxSortKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            setSortKeys(keys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    private SortKey toggle(SortKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (key.getSortOrder() == SortOrder.ASCENDING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            return new SortKey(key.getColumn(), SortOrder.DESCENDING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return new SortKey(key.getColumn(), SortOrder.ASCENDING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public int convertRowIndexToView(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (modelToView == null) {
42728
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   494
            return convertUnsortedUnfiltered(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        return modelToView[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public int convertRowIndexToModel(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (viewToModel == null) {
42728
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   506
            return convertUnsortedUnfiltered(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        return viewToModel[index].modelIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
42728
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   511
    private int convertUnsortedUnfiltered(int index) {
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   512
        if (index < 0 || index >= modelRowCount) {
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   513
            if(index >= modelRowCount &&
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   514
                                      index < getModelWrapper().getRowCount()) {
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   515
                // 8160087
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   516
                if(warning8160087) {
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   517
                    warning8160087 = false;
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   518
                    System.err.println("WARNING: row index is bigger than " +
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   519
                            "sorter's row count. Most likely this is a wrong " +
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   520
                            "sorter usage.");
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   521
                }
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   522
            } else {
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   523
                throw new IndexOutOfBoundsException("Invalid index");
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   524
            }
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   525
        }
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   526
        return index;
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   527
    }
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   528
f61c320cee03 8160087: Change IOOBE to warning in the scenarios when it had not being thrown before the JDK-8078514
ssadetsky
parents: 40426
diff changeset
   529
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    private boolean isUnsorted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        List<? extends SortKey> keys = getSortKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        int keySize = keys.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return (keySize == 0 || keys.get(0).getSortOrder() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                SortOrder.UNSORTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Sorts the existing filtered data.  This should only be used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * the filter hasn't changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    private void sortExistingData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        int[] lastViewToModel = getViewToModelAsInts(viewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        updateUseToString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        cacheSortKeys(getSortKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (isUnsorted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if (getRowFilter() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                viewToModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                modelToView = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                int included = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                for (int i = 0; i < modelToView.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    if (modelToView[i] != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                        viewToModel[included].modelIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                        modelToView[i] = included++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            // sort the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            Arrays.sort(viewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            // Update the modelToView array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            setModelToViewFromViewToModel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        fireRowSorterChanged(lastViewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Sorts and filters the rows in the view based on the sort keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * of the columns currently being sorted and the filter, if any,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * associated with this sorter.  An empty <code>sortKeys</code> list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * indicates that the view should unsorted, the same as the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @see #setRowFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @see #setSortKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    public void sort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        sorted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        int[] lastViewToModel = getViewToModelAsInts(viewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        updateUseToString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if (isUnsorted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            // Unsorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            cachedSortKeys = new SortKey[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            if (getRowFilter() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                // No filter & unsorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                if (viewToModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    // sorted -> unsorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    viewToModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    modelToView = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    // unsorted -> unsorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    // No need to do anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                // There is filter, reset mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                initializeFilteredMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            cacheSortKeys(getSortKeys());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            if (getRowFilter() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                initializeFilteredMapping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                createModelToView(getModelWrapper().getRowCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                createViewToModel(getModelWrapper().getRowCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            // sort them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            Arrays.sort(viewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            // Update the modelToView array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            setModelToViewFromViewToModel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        fireRowSorterChanged(lastViewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Updates the useToString mapping before a sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    private void updateUseToString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        int i = getModelWrapper().getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if (useToString == null || useToString.length != i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            useToString = new boolean[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        for (--i; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            useToString[i] = useToString(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * Resets the viewToModel and modelToView mappings based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * the current Filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    private void initializeFilteredMapping() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        int rowCount = getModelWrapper().getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        int excludedCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        // Update model -> view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        createModelToView(rowCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        for (i = 0; i < rowCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (include(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                modelToView[i] = i - excludedCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                modelToView[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                excludedCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        // Update view -> model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        createViewToModel(rowCount - excludedCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        for (i = 0, j = 0; i < rowCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            if (modelToView[i] != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                viewToModel[j++].modelIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Makes sure the modelToView array is of size rowCount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    private void createModelToView(int rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        if (modelToView == null || modelToView.length != rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            modelToView = new int[rowCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Resets the viewToModel array to be of size rowCount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    private void createViewToModel(int rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        int recreateFrom = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if (viewToModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            recreateFrom = Math.min(rowCount, viewToModel.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            if (viewToModel.length != rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                Row[] oldViewToModel = viewToModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                viewToModel = new Row[rowCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                System.arraycopy(oldViewToModel, 0, viewToModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                                 0, recreateFrom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            viewToModel = new Row[rowCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        for (i = 0; i < recreateFrom; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            viewToModel[i].modelIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        for (i = recreateFrom; i < rowCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            viewToModel[i] = new Row(this, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * Caches the sort keys before a sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    private void cacheSortKeys(List<? extends SortKey> keys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        int keySize = keys.size();
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
   707
        sortComparators = new Comparator<?>[keySize];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        for (int i = 0; i < keySize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            sortComparators[i] = getComparator0(keys.get(i).getColumn());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        cachedSortKeys = keys.toArray(new SortKey[keySize]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Returns whether or not to convert the value to a string before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * doing comparisons when sorting.  If true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * <code>ModelWrapper.getStringValueAt</code> will be used, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * <code>ModelWrapper.getValueAt</code> will be used.  It is up to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * subclasses, such as <code>TableRowSorter</code>, to honor this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * in their <code>ModelWrapper</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @param column the index of the column to test, in terms of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *        underlying model
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   724
     * @return true if values are to be converted to strings before doing
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23010
diff changeset
   725
     *              comparisons when sorting
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @throws IndexOutOfBoundsException if <code>column</code> is not valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    protected boolean useToString(int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        return (getComparator(column) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * Refreshes the modelToView mapping from that of viewToModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * If <code>unsetFirst</code> is true, all indices in modelToView are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * first set to -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    private void setModelToViewFromViewToModel(boolean unsetFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        if (unsetFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            for (i = modelToView.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                modelToView[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        for (i = viewToModel.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            modelToView[viewToModel[i].modelIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    private int[] getViewToModelAsInts(Row[] viewToModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        if (viewToModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            int[] viewToModelI = new int[viewToModel.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            for (int i = viewToModel.length - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                viewToModelI[i] = viewToModel[i].modelIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            return viewToModelI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        return new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * Sets the <code>Comparator</code> to use when sorting the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * column.  This does not trigger a sort.  If you want to sort after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * setting the comparator you need to explicitly invoke <code>sort</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @param column the index of the column the <code>Comparator</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *        to be used for, in terms of the underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @param comparator the <code>Comparator</code> to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * @throws IndexOutOfBoundsException if <code>column</code> is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *         the range of the underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    public void setComparator(int column, Comparator<?> comparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        checkColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if (comparators == null) {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
   774
            comparators = new Comparator<?>[getModelWrapper().getColumnCount()];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        comparators[column] = comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * Returns the <code>Comparator</code> for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * column.  This will return <code>null</code> if a <code>Comparator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * has not been specified for the column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @param column the column to fetch the <code>Comparator</code> for, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *        terms of the underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @return the <code>Comparator</code> for the specified column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @throws IndexOutOfBoundsException if column is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *         the range of the underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    public Comparator<?> getComparator(int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        checkColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        if (comparators != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            return comparators[column];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    // Returns the Comparator to use during sorting.  Where as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    // getComparator() may return null, this will never return null.
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
   800
    private Comparator<?> getComparator0(int column) {
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
   801
        Comparator<?> comparator = getComparator(column);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        if (comparator != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            return comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        // This should be ok as useToString(column) should have returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        // true in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        return Collator.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    private RowFilter.Entry<M,I> getFilterEntry(int modelIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        if (filterEntry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            filterEntry = new FilterEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        filterEntry.modelIndex = modelIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        return filterEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    public int getViewRowCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        if (viewToModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            // When filtering this may differ from getModelWrapper().getRowCount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            return viewToModel.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
36914
f790ef973830 8078514: Nightly: api/javax_swing/DefaultRowSorter/index_ModelStructChanged failure
ssadetsky
parents: 25859
diff changeset
   826
        return Math.max(getModelWrapper().getRowCount(), modelRowCount);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    public int getModelRowCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        return getModelWrapper().getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    private void allChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        modelToView = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        viewToModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        comparators = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        isSortable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        if (isUnsorted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            // Keys are already empty, to force a resort we have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            // call sort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            sort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            setSortKeys(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    public void modelStructureChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        allChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        modelRowCount = getModelWrapper().getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    public void allRowsChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        modelRowCount = getModelWrapper().getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        sort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    public void rowsInserted(int firstRow, int endRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        checkAgainstModel(firstRow, endRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        int newModelRowCount = getModelWrapper().getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        if (endRow >= newModelRowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            throw new IndexOutOfBoundsException("Invalid range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        modelRowCount = newModelRowCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        if (shouldOptimizeChange(firstRow, endRow)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            rowsInserted0(firstRow, endRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    public void rowsDeleted(int firstRow, int endRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        checkAgainstModel(firstRow, endRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        if (firstRow >= modelRowCount || endRow >= modelRowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            throw new IndexOutOfBoundsException("Invalid range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        modelRowCount = getModelWrapper().getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        if (shouldOptimizeChange(firstRow, endRow)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            rowsDeleted0(firstRow, endRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    public void rowsUpdated(int firstRow, int endRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        checkAgainstModel(firstRow, endRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        if (firstRow >= modelRowCount || endRow >= modelRowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            throw new IndexOutOfBoundsException("Invalid range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        if (getSortsOnUpdates()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            if (shouldOptimizeChange(firstRow, endRow)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                rowsUpdated0(firstRow, endRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            sorted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    public void rowsUpdated(int firstRow, int endRow, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        checkColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        rowsUpdated(firstRow, endRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    private void checkAgainstModel(int firstRow, int endRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        if (firstRow > endRow || firstRow < 0 || endRow < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                firstRow > modelRowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            throw new IndexOutOfBoundsException("Invalid range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * Returns true if the specified row should be included.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    private boolean include(int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        RowFilter<? super M, ? super I> filter = getRowFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        if (filter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            return filter.include(getFilterEntry(row));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        // null filter, always include the row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    private int compare(int model1, int model2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        int column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        SortOrder sortOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        Object v1, v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        int result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        for (int counter = 0; counter < cachedSortKeys.length; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            column = cachedSortKeys[counter].getColumn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            sortOrder = cachedSortKeys[counter].getSortOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            if (sortOrder == SortOrder.UNSORTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                result = model1 - model2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                // v1 != null && v2 != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                if (useToString[column]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                    v1 = getModelWrapper().getStringValueAt(model1, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                    v2 = getModelWrapper().getStringValueAt(model2, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                    v1 = getModelWrapper().getValueAt(model1, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                    v2 = getModelWrapper().getValueAt(model2, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                // Treat nulls as < then non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                if (v1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    if (v2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                        result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                        result = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                } else if (v2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                } else {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
   979
                    Comparator<Object> c =
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
   980
                        (Comparator<Object>)sortComparators[counter];
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
   981
                    result = c.compare(v1, v2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                if (sortOrder == SortOrder.DESCENDING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                    result *= -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            if (result != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        // If we get here, they're equal. Fallback to model order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        return model1 - model2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * Whether not we are filtering/sorting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    private boolean isTransformed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        return (viewToModel != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * Insets new set of entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * @param toAdd the Rows to add, sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @param current the array to insert the items into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    private void insertInOrder(List<Row> toAdd, Row[] current) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        int last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        int max = toAdd.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        for (int i = 0; i < max; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            index = Arrays.binarySearch(current, toAdd.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                index = -1 - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            System.arraycopy(current, last,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                             viewToModel, last + i, index - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            viewToModel[index + i] = toAdd.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            last = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        System.arraycopy(current, last, viewToModel, last + max,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                         current.length - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * Returns true if we should try and optimize the processing of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * <code>TableModelEvent</code>.  If this returns false, assume the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * event was dealt with and no further processing needs to happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    private boolean shouldOptimizeChange(int firstRow, int lastRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        if (!isTransformed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            // Not transformed, nothing to do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        }
48733
8850bd195bbd 7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents: 47216
diff changeset
  1036
        if (!sorted || viewToModel.length == 0 ||
8850bd195bbd 7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents: 47216
diff changeset
  1037
                (lastRow - firstRow) > viewToModel.length / 10) {
8850bd195bbd 7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents: 47216
diff changeset
  1038
            // We either weren't sorted, or to much changed, sort it all or
8850bd195bbd 7007967: DefaultRowSorter: incorrect sorting due to not updating comparator cache
pbansal
parents: 47216
diff changeset
  1039
            // this is the first row added and we have to update diffeent caches
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            sort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    private void rowsInserted0(int firstRow, int lastRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        int[] oldViewToModel = getViewToModelAsInts(viewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        int delta = (lastRow - firstRow) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        List<Row> added = new ArrayList<Row>(delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        // Build the list of Rows to add into added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        for (i = firstRow; i <= lastRow; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            if (include(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                added.add(new Row(this, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        // Adjust the model index of rows after the effected region
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        int viewIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        for (i = modelToView.length - 1; i >= firstRow; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            viewIndex = modelToView[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            if (viewIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                viewToModel[viewIndex].modelIndex += delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        // Insert newly added rows into viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (added.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            Collections.sort(added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            Row[] lastViewToModel = viewToModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            viewToModel = new Row[viewToModel.length + added.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            insertInOrder(added, lastViewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        // Update modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        createModelToView(getModelWrapper().getRowCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        setModelToViewFromViewToModel(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        // Notify of change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        fireRowSorterChanged(oldViewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    private void rowsDeleted0(int firstRow, int lastRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        int[] oldViewToModel = getViewToModelAsInts(viewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        int removedFromView = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        int viewIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        // Figure out how many visible rows are going to be effected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        for (i = firstRow; i <= lastRow; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            viewIndex = modelToView[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            if (viewIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                removedFromView++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                viewToModel[viewIndex] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        // Update the model index of rows after the effected region
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        int delta = lastRow - firstRow + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        for (i = modelToView.length - 1; i > lastRow; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            viewIndex = modelToView[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            if (viewIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                viewToModel[viewIndex].modelIndex -= delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        // Then patch up the viewToModel array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        if (removedFromView > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            Row[] newViewToModel = new Row[viewToModel.length -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                                           removedFromView];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            int newIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            int last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            for (i = 0; i < viewToModel.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                if (viewToModel[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                    System.arraycopy(viewToModel, last,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                                     newViewToModel, newIndex, i - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                    newIndex += (i - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    last = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            System.arraycopy(viewToModel, last,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                    newViewToModel, newIndex, viewToModel.length - last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            viewToModel = newViewToModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        // Update the modelToView mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        createModelToView(getModelWrapper().getRowCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        setModelToViewFromViewToModel(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        // And notify of change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        fireRowSorterChanged(oldViewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    private void rowsUpdated0(int firstRow, int lastRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        int[] oldViewToModel = getViewToModelAsInts(viewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        int delta = lastRow - firstRow + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        int modelIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        int last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        if (getRowFilter() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            // Sorting only:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            // Remove the effected rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            Row[] updated = new Row[delta];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            for (j = 0, i = firstRow; i <= lastRow; i++, j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                updated[j] = viewToModel[modelToView[i]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            // Sort the update rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            Arrays.sort(updated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            // Build the intermediary array: the array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            // viewToModel without the effected rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            Row[] intermediary = new Row[viewToModel.length - delta];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            for (i = 0, j = 0; i < viewToModel.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                modelIndex = viewToModel[i].modelIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                if (modelIndex < firstRow || modelIndex > lastRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                    intermediary[j++] = viewToModel[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            // Build the new viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            insertInOrder(Arrays.asList(updated), intermediary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            // Update modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            setModelToViewFromViewToModel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            // Sorting & filtering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            // Remove the effected rows, adding them to updated and setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            // modelToView to -2 for any rows that were not filtered out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            List<Row> updated = new ArrayList<Row>(delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            int newlyVisible = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            int newlyHidden = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            int effected = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            for (i = firstRow; i <= lastRow; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                if (modelToView[i] == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                    // This row was filtered out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                    if (include(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                        // No longer filtered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                        updated.add(new Row(this, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                        newlyVisible++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                    // This row was visible, make sure it should still be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                    // visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                    if (!include(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                        newlyHidden++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                        updated.add(viewToModel[modelToView[i]]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                    modelToView[i] = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                    effected++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            // Sort the updated rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            Collections.sort(updated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            // Build the intermediary array: the array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            // viewToModel without the updated rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            Row[] intermediary = new Row[viewToModel.length - effected];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            for (i = 0, j = 0; i < viewToModel.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                modelIndex = viewToModel[i].modelIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                if (modelToView[modelIndex] != -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                    intermediary[j++] = viewToModel[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            // Recreate viewToModel, if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            if (newlyVisible != newlyHidden) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                viewToModel = new Row[viewToModel.length + newlyVisible -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                                      newlyHidden];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            // Rebuild the new viewToModel array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            insertInOrder(updated, intermediary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            // Update modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            setModelToViewFromViewToModel(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        // And finally fire a sort event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        fireRowSorterChanged(oldViewToModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    private void checkColumn(int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        if (column < 0 || column >= getModelWrapper().getColumnCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            throw new IndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                    "column beyond range of TableModel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * <code>DefaultRowSorter.ModelWrapper</code> is responsible for providing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * the data that gets sorted by <code>DefaultRowSorter</code>.  You
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * normally do not interact directly with <code>ModelWrapper</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * Subclasses of <code>DefaultRowSorter</code> provide an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * implementation of <code>ModelWrapper</code> wrapping another model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * <code>TableRowSorter</code> provides a <code>ModelWrapper</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * wraps a <code>TableModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * <code>ModelWrapper</code> makes a distinction between values as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * <code>Object</code>s and <code>String</code>s.  This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * implementations to provide a custom string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * converter to be used instead of invoking <code>toString</code> on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * @param <M> the type of the underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * @param <I> the identifier supplied to the filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * @see RowFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * @see RowFilter.Entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    protected abstract static class ModelWrapper<M,I> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
         * Creates a new <code>ModelWrapper</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        protected ModelWrapper() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
         * Returns the underlying model that this <code>Model</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
         * wrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
         * @return the underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        public abstract M getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
         * Returns the number of columns in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
         * @return the number of columns in the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        public abstract int getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
         * Returns the number of rows in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
         * @return the number of rows in the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        public abstract int getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
         * Returns the value at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
         * @param row the row index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
         * @param column the column index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
         * @return the value at the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
         * @throws IndexOutOfBoundsException if the indices are outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
         *         the range of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        public abstract Object getValueAt(int row, int column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
         * Returns the value as a <code>String</code> at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
         * index.  This implementation uses <code>toString</code> on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
         * the result from <code>getValueAt</code> (making sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
         * to return an empty string for null values).  Subclasses that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
         * override this method should never return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
         * @param row the row index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
         * @param column the column index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
         * @return the value at the specified index as a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
         * @throws IndexOutOfBoundsException if the indices are outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
         *         the range of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        public String getStringValueAt(int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            Object o = getValueAt(row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            String string = o.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            if (string == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            return string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
         * Returns the identifier for the specified row.  The return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
         * of this is used as the identifier for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
         * <code>RowFilter.Entry</code> that is passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
         * <code>RowFilter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
         * @param row the row to return the identifier for, in terms of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
         *            the underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
         * @return the identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
         * @see RowFilter.Entry#getIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        public abstract I getIdentifier(int row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * RowFilter.Entry implementation that delegates to the ModelWrapper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * getFilterEntry(int) creates the single instance of this that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * passed to the Filter.  Only call getFilterEntry(int) to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * the instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    private class FilterEntry extends RowFilter.Entry<M,I> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
         * The index into the model, set in getFilterEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        int modelIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        public M getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            return getModelWrapper().getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        public int getValueCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            return getModelWrapper().getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        public Object getValue(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            return getModelWrapper().getValueAt(modelIndex, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        public String getStringValue(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            return getModelWrapper().getStringValueAt(modelIndex, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        public I getIdentifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            return getModelWrapper().getIdentifier(modelIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * Row is used to handle the actual sorting by way of Comparable.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * will use the sortKeys to do the actual comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    // NOTE: this class is static so that it can be placed in an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    private static class Row implements Comparable<Row> {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
  1382
        private DefaultRowSorter<?, ?> sorter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        int modelIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
  1385
        public Row(DefaultRowSorter<?, ?> sorter, int index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            this.sorter = sorter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            modelIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        public int compareTo(Row o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            return sorter.compare(modelIndex, o.modelIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
}