Source Code Cross Referenced for TableSort.java in  » XML-UI » SwingML » org » swingml » tablebrowser » ext » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML UI » SwingML » org.swingml.tablebrowser.ext 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.