View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas laag
3    * This file is part of lib-gwt-file.
4    * 
5    * lib-gwt-file is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * lib-gwt-file is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with lib-gwt-file.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  /**
19   * Documentation is adapted from W3C spec and content available from
20   * http://developer.mozilla.org under http://creativecommons.org/licenses/by-sa/2.5/
21   */
22  package org.vectomatic.file;
23  
24  import java.util.Iterator;
25  
26  import org.vectomatic.file.impl.FileListImpl;
27  
28  /**
29   * An object of this type is returned by the files property of the HTML input element; 
30   * this lets you access the list of files selected with the <input type="file"> element. 
31   * It's also used for a list of files dropped into web content when using the drag and drop API; 
32   * see the {@link org.vectomatic.dnd.DataTransferExt} object for details on this usage.
33   */
34  public class FileList implements Iterable<File> {
35  	private FileListImpl impl;
36  	
37  	/**
38  	 * Constructor. Do not call this constructor directly
39  	 * new FileList are generated automatically where needed in the API.
40  	 * @param impl the underlying implementation object.
41  	 */
42  	public FileList(FileListImpl impl) {
43  		this.impl = impl;
44  	}
45  	
46  	  /**
47  	   * Returns an iterator over the {@link org.vectomatic.file.File}
48  	   * elements in this list in proper sequence.
49  	   *
50  	   * <p>This implementation returns a straightforward implementation of the
51  	   * iterator interface, relying on the backing list's {@code getLength()},
52  	   * and {@code getItem(int)} methods.
53  	   *
54  	   * <p>Note that the iterator returned by this method will throw an
55  	   * {@code UnsupportedOperationException} in response to its
56  	   * {@code remove} method.
57  	   *
58  	   * @return an iterator over the {@link org.vectomatic.file.File}
59  	   * elements in this list in proper sequence
60  	   */
61  	@Override
62  	public Iterator<File> iterator() {
63  		return new Iterator<File>() {
64  			private int index;
65  
66  			@Override
67  			public boolean hasNext() {
68  				return index < getLength();
69  			}
70  
71  			@Override
72  			public File next() {
73  				return getItem(index++);
74  			}
75  
76  			@Override
77  			public void remove() {
78  				throw new UnsupportedOperationException();
79  			}			
80  		};
81  	}
82      /**
83       * Returns the <code>index</code>th item in the collection. If 
84       * <code>index</code> is greater than or equal to the number of nodes in 
85       * the list, this returns <code>null</code>.
86       * @param index Index into the collection.
87       * @return The node at the <code>index</code>th position in the 
88       *   <code>FileList</code>, or <code>null</code> if that is not a valid 
89       *   index.
90       */
91  	public final File getItem(int index) {
92  		return impl.getItem(index);
93  	}
94      /**
95       * The number of nodes in the list. The range of valid child node indices 
96       * is 0 to <code>length-1</code> inclusive.
97       */
98  	public final int getLength() {
99  		return impl.getLength();
100 	}
101 }