001: package org.swingml.tablebrowser.ext;
002:
003: import java.text.*;
004: import java.util.*;
005:
006: import org.swingml.system.*;
007: import org.swingml.util.*;
008:
009: /**
010: * @author NumberSix
011: */
012: public class TableSort {
013:
014: public class RowComparator implements Comparator {
015:
016: public int compare(Object o1, Object o2) {
017: int result = 0;
018: Object[] row1 = (Object[]) o1;
019: Object[] row2 = (Object[]) o2;
020: Iterator i = sortOrder.iterator();
021: while (i.hasNext()) {
022: int sortColumn = ((Integer) i.next()).intValue();
023: boolean descending = direction(sortColumn);
024:
025: CellDataValue cdRow1 = (CellDataValue) row1[sortColumn];
026: CellDataValue cdRow2 = (CellDataValue) row2[sortColumn];
027: Comparable valueFromRow1 = null;
028: Comparable valueFromRow2 = null;
029:
030: // get values to compare
031: Object type = sortType(sortColumn);
032: if (type instanceof DateConverter) {
033: DateConverter converter = (DateConverter) type;
034: try {
035: String val1 = cdRow1.getValue();
036: if (val1 != null && val1.trim().length() > 0) {
037: valueFromRow1 = converter.convert(val1);
038: } else {
039: valueFromRow1 = getEarlyDate();
040: }
041:
042: String val2 = cdRow2.getValue();
043: if (val2 != null && val2.trim().length() > 0) {
044: valueFromRow2 = converter.convert(val2);
045: } else {
046: valueFromRow2 = getEarlyDate();
047: }
048: } catch (ParseException e) {
049: SwingMLLogger.getInstance().log(
050: "Invalid date format for Column "
051: + sortColumn);
052: }
053: } else {
054: valueFromRow1 = cdRow1.getDisplayValue();
055: valueFromRow2 = cdRow2.getDisplayValue();
056: }
057:
058: // do comparison
059: if (valueFromRow1 != null && valueFromRow2 != null) {
060: if (!valueFromRow1.equals(valueFromRow2)) {
061: if (descending) {
062: result = valueFromRow2
063: .compareTo(valueFromRow1);
064: } else {
065: result = valueFromRow1
066: .compareTo(valueFromRow2);
067: }
068: break;
069: }
070: } else {
071: if (valueFromRow1 != null && valueFromRow2 == null) {
072: result = -1;
073: } else if (valueFromRow1 == null
074: && valueFromRow2 != null) {
075: result = 1;
076: } else {
077: result = 0;
078: }
079:
080: }
081: }
082: return result;
083: }
084: }
085:
086: public static Date EARLY_DATE;
087:
088: private static Date getEarlyDate() {
089: if (EARLY_DATE == null) {
090: Calendar calendar = Calendar.getInstance();
091: calendar.set(0, 0, 0, 0, 0, 0);
092: EARLY_DATE = calendar.getTime();
093: }
094:
095: return EARLY_DATE;
096: }
097:
098: private TableBrowserModel model = null;
099: private BitSet sortDirection = new BitSet(3); // 0 == ascending 1== descending
100: private List sortOrder = new ArrayList();
101:
102: public void addColumn(int col) {
103: addColumn(new Integer(col));
104: }
105:
106: public void addColumn(Integer col) {
107: sortOrder.add(col);
108: }
109:
110: public boolean direction(int col) {
111: return sortDirection.get(col);
112: }
113:
114: public TableBrowserModel getModel() {
115: return model;
116: }
117:
118: public List getSortOrder() {
119: return sortOrder;
120: }
121:
122: public boolean isSortColumn(int col) {
123: Iterator itr = sortOrder.iterator();
124: while (itr.hasNext()) {
125: Integer i = (Integer) itr.next();
126: if (i.intValue() == col) {
127: return true;
128: }
129: }
130: return false;
131: }
132:
133: public void removeColumn(int col) {
134: removeColumn(new Integer(col));
135: }
136:
137: public void removeColumn(Integer col) {
138: for (int i = 0; i < sortOrder.size(); i++) {
139: Integer value = (Integer) sortOrder.get(i);
140: if (value.intValue() == col.intValue()) {
141: sortOrder.remove(i);
142: }
143: }
144: }
145:
146: public void setModel(TableBrowserModel model) {
147: this .model = model;
148: }
149:
150: public void setSortDirection(int col) {
151: if (sortDirection.get(col)) {
152: sortDirection.clear(col);
153: } else {
154: sortDirection.set(col);
155: }
156: }
157:
158: public void setSortOrder(ArrayList sortOrder) {
159: this .sortOrder = sortOrder;
160: }
161:
162: public Object[][] sort(Object[][] dataToSort) {
163: List listForSort = new ArrayList();
164: for (int i = 0; i < dataToSort.length; i++) {
165: listForSort.add(dataToSort[i]);
166: }
167:
168: Collections.sort(listForSort, new RowComparator());
169:
170: return (Object[][]) listForSort.toArray(new Object[listForSort
171: .size()][]);
172: }
173:
174: private Object sortType(int column) {
175: if (column >= 0 && column <= getModel().getColumns().size()) {
176: TableBrowserColumnModel tcm = (TableBrowserColumnModel) getModel()
177: .getColumns().get(column);
178: return tcm.getType();
179: }
180:
181: return null;
182: }
183:
184: public void toggleSortDirection(int col) {
185: if (sortDirection.get(col)) {
186: sortDirection.clear(col);
187: } else {
188: sortDirection.set(col);
189: }
190: }
191:
192: }
|