KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > controlx > list > AbstractListModel


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.controlx.list;
14
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.Comparator JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28 /**
29  * @author Sameer Charles $Id: AbstractListModel.java 6546 2006-10-04 09:53:05Z philipp $
30  */

31 public abstract class AbstractListModel implements ListModel {
32
33     private static Logger log = LoggerFactory.getLogger(AbstractListModel.class);
34
35     /**
36      * sort or group by order
37      */

38     public static final String JavaDoc DESCENDING = "DESC";
39
40     /**
41      * sort or group by order
42      */

43     public static final String JavaDoc ASCENDING = "ASC";
44
45     /**
46      * sort by field name
47      */

48     protected String JavaDoc sortBy;
49
50     /**
51      * sort by order
52      */

53     protected String JavaDoc sortByOrder;
54
55     /**
56      * group by field name
57      */

58     protected String JavaDoc groupBy;
59
60     /**
61      * group by order
62      */

63     protected String JavaDoc groupByOrder;
64     
65     /**
66      * Used to get values out of the nodes/objects handled by the list
67      */

68     private ValueProvider valueProvider;
69
70     /**
71      * this must be implemented by implementing classes
72      * @return Iterator over found records
73      * @see ListModelIterator
74      */

75     public ListModelIterator getListModelIterator() {
76         try {
77             return createIterator(getResult());
78         }
79         catch (Exception JavaDoc re) {
80             log.error("can't create the list model iterator, will return an empty list", re);
81             return new ListModelIteratorImpl(new ArrayList JavaDoc(), this.getGroupBy());
82         }
83     }
84     
85     public Iterator JavaDoc iterator() {
86         return getListModelIterator();
87     }
88
89     /**
90      * @return the collection of the items passed to the iterator
91      */

92     protected abstract Collection JavaDoc getResult() throws Exception JavaDoc;
93
94     /**
95      * Create the iterator
96      * @param items
97      * @return
98      */

99     protected ListModelIterator createIterator(Collection JavaDoc items) {
100         return new ListModelIteratorImpl((List JavaDoc) this.doSort(items), this.getGroupBy(), this.getValueProvider());
101     }
102
103     /**
104      * set sort by field
105      * @param name
106      */

107     public void setSortBy(String JavaDoc name) {
108         this.sortBy = name;
109     }
110
111     /**
112      * set sort by field and order ('ASCENDING' | 'DESCENDING')
113      * @param name
114      * @param order
115      */

116     public void setSortBy(String JavaDoc name, String JavaDoc order) {
117         this.sortBy = name;
118         this.sortByOrder = order;
119     }
120
121     /**
122      * set group by field
123      * @param name
124      */

125     public void setGroupBy(String JavaDoc name) {
126         this.groupBy = name;
127     }
128
129     /**
130      * set group by field and order ('ASCENDING' | 'DESCENDING')
131      * @param name
132      * @param order
133      */

134     public void setGroupBy(String JavaDoc name, String JavaDoc order) {
135         this.groupBy = name;
136         this.groupByOrder = order;
137     }
138
139     /**
140      * get sort on field name
141      * @return String field name
142      */

143     public String JavaDoc getSortBy() {
144         return this.sortBy;
145     }
146
147     /**
148      * get sort by ordering
149      * @return order ('ASCENDING' | 'DESCENDING')
150      */

151     public String JavaDoc getSortByOrder() {
152         return this.sortByOrder;
153     }
154
155     /**
156      * get group on field name
157      * @return String field name
158      */

159     public String JavaDoc getGroupBy() {
160         return this.groupBy;
161     }
162
163     /**
164      * get group by ordering
165      * @return order ('ASCENDING' | 'DESCENDING')
166      */

167     public String JavaDoc getGroupByOrder() {
168         return this.groupByOrder;
169     }
170
171     /**
172      * sort
173      * @param collection
174      * @return sorted collection
175      */

176     protected Collection JavaDoc doSort(Collection JavaDoc collection) {
177         if (StringUtils.isNotEmpty(this.getGroupBy())) {
178             ListComparator comparator = new ListComparator();
179             comparator.setSortBy(this.getGroupBy());
180             comparator.setOrder(this.getGroupByOrder());
181             Collections.sort((List JavaDoc) collection, comparator);
182         }
183         if (StringUtils.isNotEmpty(this.getGroupBy()) && StringUtils.isNotEmpty(this.getSortBy())) { // sub sort
184
ListComparator comparator = new ListComparator();
185             comparator.setPreSort(this.getGroupBy());
186             comparator.setSortBy(this.getSortBy());
187             comparator.setOrder(this.getSortByOrder());
188             Collections.sort((List JavaDoc) collection, comparator);
189         }
190         if (StringUtils.isEmpty(this.getGroupBy()) && StringUtils.isNotEmpty(this.getSortBy())) {
191             ListComparator comparator = new ListComparator();
192             comparator.setSortBy(this.getSortBy());
193             comparator.setOrder(this.getSortByOrder());
194             Collections.sort((List JavaDoc) collection, comparator);
195         }
196         return collection;
197     }
198
199     /**
200      * @param valueProvider the valueProvider to set
201      */

202     public void setValueProvider(ValueProvider valueProvider) {
203         this.valueProvider = valueProvider;
204     }
205
206     /**
207      * @return the valueProvider
208      */

209     public ValueProvider getValueProvider() {
210         if (valueProvider == null) {
211             valueProvider = DefaultValueProvider.getInstance();
212         }
213         return valueProvider;
214     }
215
216     /**
217      * Does simple or sub ordering
218      */

219     protected class ListComparator implements Comparator JavaDoc {
220
221         private String JavaDoc preSort;
222
223         private String JavaDoc sortBy;
224
225         private String JavaDoc order;
226
227         public int compare(Object JavaDoc object, Object JavaDoc object1) {
228             if (StringUtils.isNotEmpty(this.sortBy) && StringUtils.isEmpty(this.preSort)) {
229                 return this.sort(object, object1);
230             }
231             else if (StringUtils.isNotEmpty(this.sortBy) && StringUtils.isNotEmpty(this.preSort)) {
232                 return this.subSort(object, object1);
233             }
234             return 0;
235         }
236
237         /**
238          * group by
239          * @param object to be compared
240          * @param object1 to be compared
241          */

242         private int sort(Object JavaDoc object, Object JavaDoc object1) {
243             Comparable JavaDoc firstKey = (Comparable JavaDoc) getValueProvider().getValue(this.sortBy, object);
244             Comparable JavaDoc secondKey = (Comparable JavaDoc) getValueProvider().getValue(this.sortBy, object1);
245             if (this.getOrder().equalsIgnoreCase(ASCENDING)) {
246                 return firstKey.compareTo(secondKey);
247             }
248
249             return secondKey.compareTo(firstKey);
250         }
251
252         /**
253          * sub sort
254          * @param object to be compared
255          * @param object1 to be compared
256          */

257         private int subSort(Object JavaDoc object, Object JavaDoc object1) {
258             String JavaDoc firstKey = (String JavaDoc) getValueProvider().getValue(this.preSort, object);
259             String JavaDoc secondKey = (String JavaDoc) getValueProvider().getValue(this.preSort, object1);
260             Comparable JavaDoc subSortFirstKey = (Comparable JavaDoc) getValueProvider().getValue(this.sortBy, object);
261             Comparable JavaDoc subSortSecondKey = (Comparable JavaDoc) getValueProvider().getValue(this.sortBy, object1);
262             if (firstKey.equalsIgnoreCase(secondKey)) {
263                 if (this.getOrder().equalsIgnoreCase(ASCENDING)) {
264                     return subSortFirstKey.compareTo(subSortSecondKey);
265                 }
266                 return subSortSecondKey.compareTo(subSortFirstKey);
267             }
268             return -1;
269         }
270
271         public String JavaDoc getPreSort() {
272             return preSort;
273         }
274
275         public void setPreSort(String JavaDoc preSort) {
276             this.preSort = preSort;
277         }
278
279         public String JavaDoc getSortBy() {
280             return sortBy;
281         }
282
283         public void setSortBy(String JavaDoc sortBy) {
284             this.sortBy = sortBy;
285         }
286
287         public String JavaDoc getOrder() {
288             if (order == null) {
289                 return ASCENDING;
290             }
291             return order;
292         }
293
294         public void setOrder(String JavaDoc order) {
295             this.order = order;
296         }
297
298     }
299 }
300
Popular Tags