View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * @author Sameer Charles $Id:ListModelIteratorImpl.java 2492 2006-03-30 08:30:43Z scharles $
46   */
47  public class ListModelIteratorImpl implements ListModelIterator {
48  
49      private AbstractListModel model;
50  
51      /**
52       * list holding all objects/records
53       */
54      private final List list;
55  
56      /**
57       * next position
58       */
59      private int pos;
60  
61      /**
62       * next content object (prefetched)
63       */
64      private Object next;
65  
66      /**
67       * object on current pointer
68       */
69      private Object current;
70  
71      /**
72       * key name on which provided list is grouped
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          // prefetch next object
86          prefetchNext();
87      }
88  
89      /**
90       * prefetch object for the list
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      * get named value
101      * @param name its a key to which value is attached in this record
102      */
103     @Override
104     public Object getValue(String name) {
105         return this.getValue(name, this.current);
106     }
107 
108     /**
109      * get value from a specified object
110      * @param name its a key to which value is attached in this record
111      * @param node
112      */
113     protected Object getValue(String name, Object node) {
114         return this.getValueProvider().getValue(name, node);
115     }
116 
117     /**
118      * @see info.magnolia.cms.gui.controlx.list.ListModelIterator#getValueObject()
119      */
120     @Override
121     public Object getValueObject() {
122         return this.current;
123     }
124 
125     /**
126      * get group name
127      * @return name of the group of the current record
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      * move next
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      * jump to next group
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      * checks if there is next record
166      * @return true if not EOF
167      */
168     @Override
169     public boolean hasNext() {
170         return this.next != null;
171     }
172 
173     /**
174      * checks if there are more records in the current group
175      * @return true if not EOF
176      */
177     @Override
178     public boolean hasNextInGroup() {
179         if (StringUtils.isEmpty(this.groupKey)) {
180             return this.hasNext(); // no group key defined, its all one group
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         // not implemented
203     }
204 
205     public void setValueProvider(ValueProvider valueProvider) {
206         this.valueProvider = valueProvider;
207     }
208 
209     public ValueProvider getValueProvider() {
210         return valueProvider;
211     }
212 
213 }