001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.addressbook.gui.table.model;
019:
020: import java.util.Map;
021:
022: import javax.swing.event.TableModelEvent;
023: import javax.swing.table.TableModel;
024:
025: import org.columba.addressbook.model.IContactModelPartial;
026:
027: /**
028: * Decorates TableModel which additional sorting functionality.
029: * <p>
030: * Using bubble-sort. Note, that we use an index array, which maps to the
031: * real-model decorated by this class. So, we don't change the order of the real
032: * model data.
033: *
034: * @author fdietz
035: */
036: public class SortDecorator extends TableModelDecorator {
037: /** ****************** sorting algorithm ******************* */
038: private int[] indexes;
039:
040: private int selectedColumn;
041:
042: private boolean sortOrder;
043:
044: public SortDecorator(ContactItemTableModel model) {
045: super (model);
046:
047: selectedColumn = 0;
048: sortOrder = false;
049:
050: allocate();
051: }
052:
053: /**
054: * @see org.columba.addressbook.gui.table.model.ContactItemTableModel#setHeaderItemList(org.columba.addressbook.folder.HeaderItemList)
055: */
056: public void setContactItemMap(Map<String, IContactModelPartial> list) {
057:
058: super .setContactItemMap(list);
059:
060: sort(selectedColumn);
061:
062: }
063:
064: /**
065: * @see org.columba.addressbook.gui.table.model.ContactItemTableModel#getHeaderItem(int)
066: */
067: public IContactModelPartial getContactItem(int index) {
068: return getRealModel().getContactItem(indexes[index]);
069: }
070:
071: public void tableChanged(TableModelEvent e) {
072: allocate();
073: }
074:
075: public Object getValueAt(int row, int column) {
076: return getRealModel().getValueAt(indexes[row], column);
077: }
078:
079: public void setValueAt(Object aValue, int row, int column) {
080: getRealModel().setValueAt(aValue, indexes[row], column);
081: }
082:
083: public void sort(int column) {
084:
085: selectedColumn = column;
086:
087: int rowCount = getRowCount();
088:
089: for (int i = 0; i < rowCount; i++) {
090: for (int j = i + 1; j < rowCount; j++) {
091: int c = compare(indexes[i], indexes[j], column);
092: if (!sortOrder) {
093: if (c < 0)
094: swap(i, j);
095: } else {
096: if (c > 0)
097: swap(i, j);
098: }
099: }
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 realModel = getRealModel();
111: Object io = realModel.getValueAt(i, column);
112: Object 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: }
|