KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > gui > table > model > SortDecorator


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.addressbook.gui.table.model;
19
20 import java.util.Map JavaDoc;
21
22 import javax.swing.event.TableModelEvent JavaDoc;
23 import javax.swing.table.TableModel JavaDoc;
24
25 import org.columba.addressbook.model.IContactModelPartial;
26
27 /**
28  * Decorates TableModel which additional sorting functionality.
29  * <p>
30  * Using bubble-sort. Note, that we use an index array, which maps to the
31  * real-model decorated by this class. So, we don't change the order of the real
32  * model data.
33  *
34  * @author fdietz
35  */

36 public class SortDecorator extends TableModelDecorator {
37     /** ****************** sorting algorithm ******************* */
38     private int[] indexes;
39
40     private int selectedColumn;
41
42     private boolean sortOrder;
43
44     public SortDecorator(ContactItemTableModel model) {
45         super(model);
46
47         selectedColumn = 0;
48         sortOrder = false;
49
50         allocate();
51     }
52
53     /**
54      * @see org.columba.addressbook.gui.table.model.ContactItemTableModel#setHeaderItemList(org.columba.addressbook.folder.HeaderItemList)
55      */

56     public void setContactItemMap(Map JavaDoc<String JavaDoc,IContactModelPartial> list) {
57
58         super.setContactItemMap(list);
59
60         sort(selectedColumn);
61
62     }
63
64     /**
65      * @see org.columba.addressbook.gui.table.model.ContactItemTableModel#getHeaderItem(int)
66      */

67     public IContactModelPartial getContactItem(int index) {
68         return getRealModel().getContactItem(indexes[index]);
69     }
70
71     public void tableChanged(TableModelEvent JavaDoc e) {
72         allocate();
73     }
74
75     public Object JavaDoc getValueAt(int row, int column) {
76         return getRealModel().getValueAt(indexes[row], column);
77     }
78
79     public void setValueAt(Object JavaDoc aValue, int row, int column) {
80         getRealModel().setValueAt(aValue, indexes[row], column);
81     }
82
83     public void sort(int column) {
84
85         selectedColumn = column;
86
87         int rowCount = getRowCount();
88
89         for (int i = 0; i < rowCount; i++) {
90             for (int j = i + 1; j < rowCount; j++) {
91                 int c = compare(indexes[i], indexes[j], column);
92                 if (!sortOrder) {
93                     if (c < 0)
94                         swap(i, j);
95                 } else {
96                     if (c > 0)
97                         swap(i, j);
98                 }
99             }
100         }
101     }
102
103     private void swap(int i, int j) {
104         int tmp = indexes[i];
105         indexes[i] = indexes[j];
106         indexes[j] = tmp;
107     }
108
109     private int compare(int i, int j, int column) {
110         TableModel JavaDoc realModel = getRealModel();
111         Object JavaDoc io = realModel.getValueAt(i, column);
112         Object JavaDoc jo = realModel.getValueAt(j, column);
113
114         if ((io == null) || (jo == null)) {
115             return 0;
116         }
117
118         int c = jo.toString().compareTo(io.toString());
119
120         return (c < 0) ? (-1) : ((c > 0) ? 1 : 0);
121     }
122
123     private void allocate() {
124         indexes = new int[getRowCount()];
125
126         for (int i = 0; i < indexes.length; ++i) {
127             indexes[i] = i;
128         }
129     }
130
131     /**
132      * @return Returns the sortOrder.
133      */

134     public boolean isSortOrder() {
135         return sortOrder;
136     }
137
138     /**
139      * @param sortOrder
140      * The sortOrder to set.
141      */

142     public void setSortOrder(boolean sortOrder) {
143         this.sortOrder = sortOrder;
144     }
145
146     /**
147      * @return Returns the selectedColumn.
148      */

149     public int getSelectedColumn() {
150         return selectedColumn;
151     }
152
153     /**
154      * @param selectedColumn
155      * The selectedColumn to set.
156      */

157     public void setSelectedColumn(int selectedColumn) {
158         this.selectedColumn = selectedColumn;
159     }
160 }
Popular Tags