src/java.desktop/share/classes/javax/swing/table/TableRowSorter.java
author pbansal
Thu, 29 Mar 2018 17:52:32 +0530
changeset 49501 5daa8ef17089
parent 47216 71c04702a3d5
permissions -rw-r--r--
8074286: Add getSelectedIndices() to ListSelectionModel Reviewed-by: serb, psadhukhan
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.table;
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.DefaultRowSorter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.RowFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.SortOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * An implementation of <code>RowSorter</code> that provides sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * and filtering using a <code>TableModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The following example shows adding sorting to a <code>JTable</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *   TableModel myModel = createMyTableModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *   JTable table = new JTable(myModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *   table.setRowSorter(new TableRowSorter(myModel));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This will do all the wiring such that when the user does the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * gesture, such as clicking on the column header, the table will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * visually sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>JTable</code>'s row-based methods and <code>JTable</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * selection model refer to the view and not the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * model. Therefore, it is necessary to convert between the two.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * example, to get the selection in terms of <code>myModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * you need to convert the indices:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   int[] selection = table.getSelectedRows();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   for (int i = 0; i &lt; selection.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     selection[i] = table.convertRowIndexToModel(selection[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Similarly to select a row in <code>JTable</code> based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * a coordinate from the underlying model do the inverse:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   table.setRowSelectionInterval(table.convertRowIndexToView(row),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *                                 table.convertRowIndexToView(row));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * The previous example assumes you have not enabled filtering.  If you
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * have enabled filtering <code>convertRowIndexToView</code> will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * -1 for locations that are not visible in the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <code>TableRowSorter</code> uses <code>Comparator</code>s for doing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * comparisons. The following defines how a <code>Comparator</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * chosen for a column:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <li>If a <code>Comparator</code> has been specified for the column by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *     <code>setComparator</code> method, use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <li>If the column class as returned by <code>getColumnClass</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *     <code>String</code>, use the <code>Comparator</code> returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *     <code>Collator.getInstance()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <li>If the column class implements <code>Comparable</code>, use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     <code>Comparator</code> that invokes the <code>compareTo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <li>If a <code>TableStringConverter</code> has been specified, use it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *     to convert the values to <code>String</code>s and then use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *     <code>Comparator</code> returned by <code>Collator.getInstance()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <li>Otherwise use the <code>Comparator</code> returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     <code>Collator.getInstance()</code> on the results from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *     calling <code>toString</code> on the objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * In addition to sorting <code>TableRowSorter</code> provides the ability
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * to filter.  A filter is specified using the <code>setFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * method. The following example will only show rows containing the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * "foo":
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   TableModel myModel = createMyTableModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *   TableRowSorter sorter = new TableRowSorter(myModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   sorter.setRowFilter(RowFilter.regexFilter(".*foo.*"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *   JTable table = new JTable(myModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *   table.setRowSorter(sorter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * If the underlying model structure changes (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <code>modelStructureChanged</code> method is invoked) the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * are reset to their default values: <code>Comparator</code>s by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * column, current sort order, and whether each column is sortable. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * sort order is natural (the same as the model), and columns are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * sortable by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <code>TableRowSorter</code> has one formal type parameter: the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * of the model.  Passing in a type that corresponds exactly to your
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * model allows you to filter based on your model without casting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * Refer to the documentation of <code>RowFilter</code> for an example
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * of this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <b>WARNING:</b> <code>DefaultTableModel</code> returns a column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * class of <code>Object</code>.  As such all comparisons will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * be done using <code>toString</code>.  This may be unnecessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * expensive.  If the column only contains one type of value, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * an <code>Integer</code>, you should override <code>getColumnClass</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * return the appropriate <code>Class</code>.  This will dramatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * increase the performance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * @param <M> the type of the model, which must be an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *            <code>TableModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * @see javax.swing.JTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @see javax.swing.RowFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @see javax.swing.table.DefaultTableModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @see java.text.Collator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @see java.util.Comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
public class TableRowSorter<M extends TableModel> extends DefaultRowSorter<M, Integer> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Comparator that uses compareTo on the contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 5506
diff changeset
   134
    private static final Comparator<?> COMPARABLE_COMPARATOR =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            new ComparableComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Underlying model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private M tableModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * For toString conversions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private TableStringConverter stringConverter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Creates a <code>TableRowSorter</code> with an empty model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public TableRowSorter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Creates a <code>TableRowSorter</code> using <code>model</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * as the underlying <code>TableModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param model the underlying <code>TableModel</code> to use,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *        <code>null</code> is treated as an empty model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public TableRowSorter(M model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        setModel(model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Sets the <code>TableModel</code> to use as the underlying model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * for this <code>TableRowSorter</code>.  A value of <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * can be used to set an empty model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param model the underlying model to use, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public void setModel(M model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        tableModel = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        setModelWrapper(new TableRowSorterModelWrapper());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Sets the object responsible for converting values from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * model to strings.  If non-<code>null</code> this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * is used to convert any object values, that do not have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * registered <code>Comparator</code>, to strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param stringConverter the object responsible for converting values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *        from the model to strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public void setStringConverter(TableStringConverter stringConverter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        this.stringConverter = stringConverter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Returns the object responsible for converting values from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * model to strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return object responsible for converting values to strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public TableStringConverter getStringConverter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return stringConverter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Returns the <code>Comparator</code> for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * column.  If a <code>Comparator</code> has not been specified using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * the <code>setComparator</code> method a <code>Comparator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * will be returned based on the column class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * (<code>TableModel.getColumnClass</code>) of the specified column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * If the column class is <code>String</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <code>Collator.getInstance</code> is returned.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * column class implements <code>Comparable</code> a private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <code>Comparator</code> is returned that invokes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <code>compareTo</code> method.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <code>Collator.getInstance</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public Comparator<?> getComparator(int column) {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 5506
diff changeset
   217
        Comparator<?> comparator = super.getComparator(column);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (comparator != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 5506
diff changeset
   221
        Class<?> columnClass = getModel().getColumnClass(column);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (columnClass == String.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return Collator.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (Comparable.class.isAssignableFrom(columnClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return COMPARABLE_COMPARATOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return Collator.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    protected boolean useToString(int column) {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 5506
diff changeset
   237
        Comparator<?> comparator = super.getComparator(column);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (comparator != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 5506
diff changeset
   241
        Class<?> columnClass = getModel().getColumnClass(column);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (columnClass == String.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (Comparable.class.isAssignableFrom(columnClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Implementation of DefaultRowSorter.ModelWrapper that delegates to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * TableModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private class TableRowSorterModelWrapper extends ModelWrapper<M,Integer> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        public M getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return tableModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        public int getColumnCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return (tableModel == null) ? 0 : tableModel.getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        public int getRowCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return (tableModel == null) ? 0 : tableModel.getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        public Object getValueAt(int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return tableModel.getValueAt(row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        public String getStringValueAt(int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            TableStringConverter converter = getStringConverter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (converter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                // Use the converter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                String value = converter.toString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        tableModel, row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // No converter, use getValueAt followed by toString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Object o = getValueAt(row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            String string = o.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (string == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        public Integer getIdentifier(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 5506
diff changeset
   302
    private static class ComparableComparator implements Comparator<Object> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        public int compare(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return ((Comparable)o1).compareTo(o2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}