1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.gui.controlx.list;
35
36
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.NoSuchElementException;
40
41 import org.apache.commons.lang.StringUtils;
42
43
44
45
46
47 public class ListModelIteratorImpl implements ListModelIterator {
48
49 private AbstractListModel model;
50
51
52
53
54 private final List list;
55
56
57
58
59 private int pos;
60
61
62
63
64 private Object next;
65
66
67
68
69 private Object current;
70
71
72
73
74 private String groupKey;
75
76 private ValueProvider valueProvider;
77
78 public ListModelIteratorImpl(AbstractListModel model, List list) {
79 this.model = model;
80 this.list = new ArrayList(list);
81 this.groupKey = model.getGroupBy();
82 this.pos = 0;
83 this.setValueProvider(model.getValueProvider());
84
85
86 prefetchNext();
87 }
88
89
90
91
92 private void prefetchNext() {
93 this.next = null;
94 while (this.next == null && this.pos < this.list.size()) {
95 this.next = this.list.get(pos);
96 }
97 }
98
99
100
101
102
103 @Override
104 public Object getValue(String name) {
105 return this.getValue(name, this.current);
106 }
107
108
109
110
111
112
113 protected Object getValue(String name, Object node) {
114 return this.getValueProvider().getValue(name, node);
115 }
116
117
118
119
120 @Override
121 public Object getValueObject() {
122 return this.current;
123 }
124
125
126
127
128
129 @Override
130 public String getGroupName() {
131 if (StringUtils.isEmpty(this.groupKey)) {
132 return StringUtils.EMPTY;
133 }
134 return (String) this.getValue(this.groupKey, this.current);
135 }
136
137
138
139
140 @Override
141 public Object next() {
142 if (this.next == null) {
143 throw new NoSuchElementException();
144 }
145 this.current = this.next;
146 this.pos++;
147 prefetchNext();
148
149 return this.current;
150 }
151
152
153
154
155 @Override
156 public Object nextGroup() {
157 Object tmp = null;
158 while (this.hasNextInGroup()) {
159 tmp = this.next();
160 }
161 return tmp;
162 }
163
164
165
166
167
168 @Override
169 public boolean hasNext() {
170 return this.next != null;
171 }
172
173
174
175
176
177 @Override
178 public boolean hasNextInGroup() {
179 if (StringUtils.isEmpty(this.groupKey)) {
180 return this.hasNext();
181 }
182 else if (this.hasNext()) {
183 if (this.current != null) {
184 String currentValue = (String) this.getValue(this.groupKey, this.current);
185 String nextValue = (String) this.getValue(this.groupKey, this.next);
186 return StringUtils.equalsIgnoreCase(currentValue, nextValue);
187 }
188 }
189 else {
190 return false;
191 }
192 return true;
193 }
194
195 @Override
196 public String getId() {
197 return this.model.resolveId(pos-1, this.getValueObject());
198 }
199
200 @Override
201 public void remove() {
202
203 }
204
205 public void setValueProvider(ValueProvider valueProvider) {
206 this.valueProvider = valueProvider;
207 }
208
209 public ValueProvider getValueProvider() {
210 return valueProvider;
211 }
212
213 }