KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
18 import java.util.NoSuchElementException JavaDoc;
19
20 import org.apache.commons.lang.StringUtils;
21
22
23 /**
24  * @author Sameer Charles $Id:ListModelIteratorImpl.java 2492 2006-03-30 08:30:43Z scharles $
25  */

26 public class ListModelIteratorImpl implements ListModelIterator {
27
28     /**
29      * list holding all objects/records
30      */

31     private final List JavaDoc list;
32
33     /**
34      * next position
35      */

36     private int pos;
37
38     /**
39      * next content object (prefetched)
40      */

41     private Object JavaDoc next;
42
43     /**
44      * object on current pointer
45      */

46     private Object JavaDoc current;
47
48     /**
49      * key name on which provided list is grouped
50      */

51     private String JavaDoc groupKey;
52     
53     private ValueProvider valueProvider;
54
55     public ListModelIteratorImpl(List JavaDoc list, String JavaDoc groupKey, ValueProvider valueProvider) {
56         this.list = new ArrayList JavaDoc(list);
57         this.groupKey = groupKey;
58         this.pos = 0;
59         this.setValueProvider(valueProvider);
60
61         // prefetch next object
62
prefetchNext();
63     }
64
65     /**
66      * creates a new ListModelIterator
67      * @param list of content objects
68      */

69     public ListModelIteratorImpl(List JavaDoc list, String JavaDoc groupKey) {
70         this(list, groupKey, DefaultValueProvider.getInstance());
71     }
72
73     /**
74      * prefetch object for the list
75      */

76     private void prefetchNext() {
77         this.next = null;
78         while (this.next == null && this.pos < this.list.size()) {
79             this.next = this.list.get(pos);
80         }
81     }
82
83     /**
84      * get named value
85      * @param name its a key to which value is attached in this record
86      */

87     public Object JavaDoc getValue(String JavaDoc name) {
88         return this.getValue(name, this.current);
89     }
90
91     /**
92      * get value from a specified object
93      * @param name its a key to which value is attached in this record
94      * @param node
95      */

96     protected Object JavaDoc getValue(String JavaDoc name, Object JavaDoc node) {
97         return this.getValueProvider().getValue(name, node);
98     }
99
100     /**
101      * @see info.magnolia.cms.gui.controlx.list.ListModelIterator#getValueObject()
102      */

103     public Object JavaDoc getValueObject() {
104         return this.current;
105     }
106
107     /**
108      * get group name
109      * @return name of the group of the current record
110      */

111     public String JavaDoc getGroupName() {
112         if (StringUtils.isEmpty(this.groupKey)) {
113             return StringUtils.EMPTY;
114         }
115         return (String JavaDoc) this.getValue(this.groupKey, this.current);
116     }
117
118     /**
119      * move next
120      */

121     public Object JavaDoc next() {
122         if (this.next == null) {
123             throw new NoSuchElementException JavaDoc();
124         }
125         this.current = this.next;
126         this.pos++;
127         prefetchNext();
128
129         return this.current;
130     }
131
132     /**
133      * jump to next group
134      */

135     public Object JavaDoc nextGroup() {
136         Object JavaDoc tmp = null;
137         while (this.hasNextInGroup()) {
138             tmp = this.next();
139         }
140         return tmp;
141     }
142
143     /**
144      * checks if there is next record
145      * @return true if not EOF
146      */

147     public boolean hasNext() {
148         return this.next != null;
149     }
150
151     /**
152      * checks if there are more records in the current group
153      * @return true if not EOF
154      */

155     public boolean hasNextInGroup() {
156         if (StringUtils.isEmpty(this.groupKey)) {
157             return this.hasNext(); // no group key defined, its all one group
158
}
159         else if (this.hasNext()) {
160             if (this.current != null) {
161                 String JavaDoc currentValue = (String JavaDoc) this.getValue(this.groupKey, this.current);
162                 String JavaDoc nextValue = (String JavaDoc) this.getValue(this.groupKey, this.next);
163                 return StringUtils.equalsIgnoreCase(currentValue, nextValue);
164             }
165         }
166         else {
167             return false;
168         }
169         return true;
170     }
171
172     /**
173      * @see java.util.Iterator#remove()
174      */

175     public void remove() {
176         // not implemented
177
}
178
179     public void setValueProvider(ValueProvider valueProvider) {
180         this.valueProvider = valueProvider;
181     }
182
183     public ValueProvider getValueProvider() {
184         return valueProvider;
185     }
186
187 }
188
Popular Tags