KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > editors > ui > SortableDDTableModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * SortableDDTableModel.java -- synopsis.
21  *
22  */

23 package org.netbeans.modules.j2ee.sun.ide.editors.ui;
24
25 import java.util.*;
26
27 import javax.swing.table.*;
28
29 /**
30  * Table model using the composite pattern allowing DDTableModels to be sorted.
31  * The model allows any column containing a java.util.Comparable type to be
32  * sorted. The sorting model is index based and therefore only sorts the view
33  * into the underlying model and not the model itself.
34  *
35  * @author Chris Webster
36  */

37 //
38
// 29-may-2001
39
// Change for bug 4457984 - pass the new methods of DDTableModel onto
40
// the delegate and added parameter to addRowAt. (joecorto)
41
//
42
public class SortableDDTableModel extends AbstractTableModel
43 implements DDTableModel {
44     private DDTableModel modelDelegate;
45     private boolean needsSorting;
46     private List modelIndex;
47     private Comparator comp;
48     private int sortColumn;
49
50     public SortableDDTableModel(DDTableModel model) {
51         modelDelegate = model;
52     modelIndex = new ArrayList(getRowCount());
53     for (int i =0; i < getRowCount(); i++) {
54         modelIndex.add(new DelegateReference(i));
55     }
56
57     comp = new Comparator() {
58         public boolean equals(Object JavaDoc other) {
59             return this == other;
60         }
61
62         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
63                 if (!(o1 instanceof DelegateReference) ||
64             !(o2 instanceof DelegateReference))
65                 {
66             throw new ClassCastException JavaDoc();
67         }
68         DelegateReference d1 = (DelegateReference) o1;
69         DelegateReference d2 = (DelegateReference) o2;
70         Comparable JavaDoc compo1 =
71                     (Comparable JavaDoc) modelDelegate.getValueAt(d1.ref,
72                                                           getSortColumn());
73             Object JavaDoc comp2 = modelDelegate.getValueAt(d2.ref,
74                               getSortColumn());
75
76           
77         if (compo1 == null && comp2 == null) {
78             return 0;
79         }
80               
81         if (compo1 == null) {
82             return -1;
83         }
84
85         if (comp2 == null) {
86             return 1;
87         }
88                 
89         return compo1.compareTo(comp2);
90             }
91         };
92     setSortColumn(-1);
93     }
94
95     public int getSortColumn() {
96         return sortColumn;
97     }
98
99     public boolean isSortable() {
100     return Comparable JavaDoc.class.isAssignableFrom(
101          getColumnClass(getSortColumn()));
102     }
103
104     private static class DelegateReference {
105         public DelegateReference(int ref) {
106         this.ref = ref;
107     }
108     public int ref;
109     }
110
111     public void setSortColumn(int col) {
112         if (col < 0 || col >= getColumnCount()) {
113         sortColumn = col;
114         return;
115         }
116
117         if (sortColumn != col || needsSorting) {
118         sortColumn = col;
119         sort();
120         needsSorting = false;
121     }
122     }
123
124     private void sort() {
125         Collections.sort(modelIndex,comp);
126         fireTableDataChanged();
127     }
128
129     private int getInd(int row) {
130         return ((DelegateReference) modelIndex.get(row)).ref;
131     }
132
133     public int getColumnCount() {
134         return modelDelegate.getColumnCount();
135     }
136
137     public String JavaDoc getColumnName(int col) {
138     return modelDelegate.getColumnName(col);
139     }
140
141     public DDTableModelEditor getEditor() {
142         return modelDelegate.getEditor();
143     }
144     
145     public boolean isEditValid (Object JavaDoc value, int row) {
146     return modelDelegate.isEditValid (value, row);
147     }
148
149     public List canRemoveRow (int row) {
150     return modelDelegate.canRemoveRow (row);
151     }
152
153     public List isValueValid(Object JavaDoc value, int fromRow) {
154         return modelDelegate.isValueValid(value,
155                       fromRow==-1?-1:getInd(fromRow));
156     }
157     
158     public Object JavaDoc getValueAt(int row, int col) {
159     return modelDelegate.getValueAt(getInd(row), col);
160     }
161
162     public String JavaDoc getModelName() {
163     return modelDelegate.getModelName();
164     }
165         
166     public Object JavaDoc [] getValue () {
167     Object JavaDoc[] rv = modelDelegate.getValue();
168     /* XXXX Ask Chris about this.
169     for (int i = 0; i < rv.length; i++) {
170         rv[i] = getValueAt(i);
171     }*/

172     return rv;
173     }
174    
175     public int getRowCount () {
176         return modelDelegate.getRowCount();
177     }
178
179     public Class JavaDoc getColumnClass (int col) {
180     return modelDelegate.getColumnClass(col);
181     }
182
183     public boolean isCellEditable(int row, int col) {
184     return modelDelegate.isCellEditable(getInd(row),col);
185     }
186
187     public Object JavaDoc getValueAt (int row) {
188     return modelDelegate.getValueAt(getInd(row));
189     }
190
191     public void setValueAt (int row, Object JavaDoc value) {
192         modelDelegate.setValueAt(getInd(row), value);
193     needsSorting = true;
194     setSortColumn(getSortColumn());
195     }
196
197     public void setValueAt (Object JavaDoc value, int row, int col) {
198     modelDelegate.setValueAt(value, getInd(row), col);
199     needsSorting = true;
200     setSortColumn(getSortColumn());
201     }
202
203     public Object JavaDoc makeNewElement() {
204     return modelDelegate.makeNewElement();
205     }
206
207     public void newElementCancelled(Object JavaDoc obj) {
208     modelDelegate.newElementCancelled(obj);
209     }
210
211     public void editsCancelled() {
212     modelDelegate.editsCancelled();
213     }
214        
215     public void addRowAt(int row, Object JavaDoc newVal, Object JavaDoc editVal) {
216         if (row == -1) {
217         row = getRowCount();
218     } else {
219         row++;
220     }
221
222     modelIndex.add(row, new DelegateReference(getRowCount()));
223     modelDelegate.addRowAt(-1, newVal, editVal);
224     fireTableRowsInserted(row,row);
225     needsSorting = true;
226     setSortColumn(getSortColumn());
227     }
228
229     public void removeRowAt(int row) {
230     int delegateRow = getInd(row);
231     modelDelegate.removeRowAt(delegateRow);
232     modelIndex.remove(row);
233     Iterator it = modelIndex.iterator();
234     while (it.hasNext()) {
235         DelegateReference del = (DelegateReference) it.next();
236         if (del.ref >= delegateRow) {
237             del.ref--;
238         }
239         fireTableRowsDeleted(row, row);
240         }
241     }
242 }
243
Popular Tags