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 public Object getValue(String name) {
104 return this.getValue(name, this.current);
105 }
106
107
108
109
110
111
112 protected Object getValue(String name, Object node) {
113 return this.getValueProvider().getValue(name, node);
114 }
115
116
117
118
119 public Object getValueObject() {
120 return this.current;
121 }
122
123
124
125
126
127 public String getGroupName() {
128 if (StringUtils.isEmpty(this.groupKey)) {
129 return StringUtils.EMPTY;
130 }
131 return (String) this.getValue(this.groupKey, this.current);
132 }
133
134
135
136
137 public Object next() {
138 if (this.next == null) {
139 throw new NoSuchElementException();
140 }
141 this.current = this.next;
142 this.pos++;
143 prefetchNext();
144
145 return this.current;
146 }
147
148
149
150
151 public Object nextGroup() {
152 Object tmp = null;
153 while (this.hasNextInGroup()) {
154 tmp = this.next();
155 }
156 return tmp;
157 }
158
159
160
161
162
163 public boolean hasNext() {
164 return this.next != null;
165 }
166
167
168
169
170
171 public boolean hasNextInGroup() {
172 if (StringUtils.isEmpty(this.groupKey)) {
173 return this.hasNext();
174 }
175 else if (this.hasNext()) {
176 if (this.current != null) {
177 String currentValue = (String) this.getValue(this.groupKey, this.current);
178 String nextValue = (String) this.getValue(this.groupKey, this.next);
179 return StringUtils.equalsIgnoreCase(currentValue, nextValue);
180 }
181 }
182 else {
183 return false;
184 }
185 return true;
186 }
187
188 public String getId() {
189 return this.model.resolveId(pos-1, this.getValueObject());
190 }
191
192 public void remove() {
193
194 }
195
196 public void setValueProvider(ValueProvider valueProvider) {
197 this.valueProvider = valueProvider;
198 }
199
200 public ValueProvider getValueProvider() {
201 return valueProvider;
202 }
203
204 }