View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.imageloop;
20  
21  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIGraphic;
25  import javax.faces.el.ValueBinding;
26  import java.util.Arrays;
27  import java.util.Collection;
28  import java.util.Iterator;
29  import java.util.NoSuchElementException;
30  
31  /**
32   * Image items iterator.
33   * @author Felix Röthenbacher (latest modification by $Author:$)
34   * @version $Revision:$ $Date:$
35   */
36  public class ImageLoopItemsIterator implements Iterator {
37  
38      private final Iterator _childs;
39      private Iterator _nestedItems;
40      private Object _nextItem;
41      private ImageLoopItems _currentImageLoopItems;
42      private String _collectionLabel;
43      
44      public ImageLoopItemsIterator(UIComponent htmlImageLoopItemsParent) {
45          _childs = htmlImageLoopItemsParent.getChildren().iterator();
46      }
47  
48      public boolean hasNext() {
49          if(_nextItem != null)
50          {
51              return true;
52          }
53          if(_nestedItems != null)
54          {
55              if(_nestedItems.hasNext())
56              {
57                  return true;
58              }
59              _nestedItems = null;
60          }
61          if (_childs.hasNext())
62          {
63              UIComponent child = (UIComponent) _childs.next();
64              if (child instanceof UIGraphic)
65              {
66                  UIGraphic uiGraphic = (UIGraphic) child;
67                  // UIGraphic.getUrl() is an alias for UIGraphic.getValue()
68                  String url = uiGraphic.getUrl();
69                  _nextItem = new GraphicItem(url);
70                  return true;
71              }
72              else if (child instanceof ImageLoopItems)
73              {
74                  _currentImageLoopItems = ((ImageLoopItems) child);
75                  Object value = _currentImageLoopItems.getValue();
76  
77                  if (value instanceof GraphicItem)
78                  {
79                      _nextItem = value;
80                      return true;
81                  }
82                  else if (value instanceof GraphicItem[])
83                  {
84                      _nestedItems = Arrays.asList((GraphicItem[]) value)
85                                      .iterator();
86                      _collectionLabel = "Array";
87                      return hasNext();
88                  }
89                  else if (value instanceof Collection)
90                  {
91                      _nestedItems = ((Collection)value).iterator();
92                      _collectionLabel = "Collection";
93                      return hasNext();
94                  }
95                  else
96                  {
97                      ValueBinding binding = _currentImageLoopItems.getValueBinding("value");
98  
99                      throw new IllegalArgumentException(
100                         "Value binding '"
101                         + (binding == null ? null : binding
102                                         .getExpressionString())
103                         + "'of ImageLoopItems with component-path "
104                         + RendererUtils.getPathToComponent(child)
105                         + " does not reference an Object of type GraphicItem, GraphicItem[] or Collection but of type : "
106                         + ((value == null) ? null : value.getClass().getName()));
107                 }
108             }
109             else
110             {
111             }
112         }
113         return false;
114 
115     }
116 
117     public Object next() {
118         if (!hasNext())
119         {
120             throw new NoSuchElementException();
121         }
122         if(_nextItem != null)
123         {
124             Object value = _nextItem;
125             _nextItem = null;
126             return value;
127         }
128         if (_nestedItems != null)
129         {
130             Object item = _nestedItems.next();
131             if (!(item instanceof GraphicItem))
132             {
133                 ValueBinding binding = _currentImageLoopItems
134                                 .getValueBinding("value");
135                 throw new IllegalArgumentException(
136                 _collectionLabel + " referenced by ImageLoopItems with binding '"
137                 + binding.getExpressionString()
138                 + "' and Component-Path : " + RendererUtils.getPathToComponent(_currentImageLoopItems)
139                 + " does not contain objects of type GraphicItem");
140             }
141             return item;
142         }
143         throw new NoSuchElementException();
144     }
145 
146     public void remove() {
147         throw new UnsupportedOperationException();
148     }
149 }