View Javadoc
1   /*
2    * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   *
23   */
24  
25  package sun.jvm.hotspot.ui.table;
26  
27  import java.util.*;
28  
29  import javax.swing.table.TableModel;
30  import javax.swing.event.TableModelEvent;
31  
32  /**
33   * A comparator which compares rows in a table model
34   */
35  public abstract class TableModelComparator implements Comparator {
36  
37      private boolean ascending;
38      protected TableModel model;
39  
40      private int[] columns;
41  
42      public TableModelComparator(TableModel model) {
43          this.model = model;
44  
45          // XXX - Should actually listen for column changes and resize
46          columns = new int[model.getColumnCount()];
47          columns[0] = -1;
48      }
49  
50      /**
51       * Add the column to the sort criteria
52       */
53      public void addColumn(int column) {
54          // Shift columns in the array
55          int[] tempArray = new int[model.getColumnCount()];
56          System.arraycopy(columns, 1, tempArray, 0, columns.length - 1);
57  
58          columns = tempArray;
59          columns[0] = column;
60      }
61  
62      /**
63       * Get the last column that was sorted
64       */
65      public int getColumn() {
66          return columns[0];
67      }
68  
69      public void setAscending(boolean ascending) {
70          this.ascending = ascending;
71      }
72  
73      public boolean isAscending() {
74          return ascending;
75      }
76  
77      /**
78       * Implementation of the comparator method. A comparison is
79       * made for rows.
80       */
81      public int compare(Object row1, Object row2) {
82          for (int i = 0; i < columns.length; i++) {
83  
84              Object o1 = getValueForColumn(row1, columns[i]);
85              Object o2 = getValueForColumn(row2, columns[i]);
86  
87              // If both values are null, return 0.
88              if (o1 == null && o2 == null) {
89                  return 0;
90              } else if (o1 == null) { // Define null less than everything.
91                  return -1;
92              } else if (o2 == null) {
93                  return 1;
94              }
95  
96              int result = 0;
97  
98              if (o1 instanceof Comparable) {
99                  Comparable c1 = (Comparable)o1;
100                 Comparable c2 = (Comparable)o2;
101 
102                 result = c1.compareTo(c2);
103             }
104 
105             // XXX Should have some sort of provision for determininte
106             // if there is another way of comparing the objects.
107             // Perhaps we should add the requirement that all table
108             // values be Compabable.
109 
110             if (result != 0) {
111                 return ascending ? result : -result;
112             }
113         }
114         return 0;
115     }
116 
117     /**
118      * Returns the value for the comparing object for the
119      * column.
120      *
121      * @param obj Row object that was passed into Comparator.
122      * @param column the column to retrieve
123      */
124     public abstract Object getValueForColumn(Object obj, int column);
125 
126 }