View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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     public Object getValue(String name) {
104         return this.getValue(name, this.current);
105     }
106 
107     /**
108      * get value from a specified object
109      * @param name its a key to which value is attached in this record
110      * @param node
111      */
112     protected Object getValue(String name, Object node) {
113         return this.getValueProvider().getValue(name, node);
114     }
115 
116     /**
117      * @see info.magnolia.cms.gui.controlx.list.ListModelIterator#getValueObject()
118      */
119     public Object getValueObject() {
120         return this.current;
121     }
122 
123     /**
124      * get group name
125      * @return name of the group of the current record
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      * move next
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      * jump to next group
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      * checks if there is next record
161      * @return true if not EOF
162      */
163     public boolean hasNext() {
164         return this.next != null;
165     }
166 
167     /**
168      * checks if there are more records in the current group
169      * @return true if not EOF
170      */
171     public boolean hasNextInGroup() {
172         if (StringUtils.isEmpty(this.groupKey)) {
173             return this.hasNext(); // no group key defined, its all one group
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         // not implemented
194     }
195 
196     public void setValueProvider(ValueProvider valueProvider) {
197         this.valueProvider = valueProvider;
198     }
199 
200     public ValueProvider getValueProvider() {
201         return valueProvider;
202     }
203 
204 }