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