1 /********************************************** 2 * Copyright (C) 2010 Lukas Laag 3 * This file is part of lib-gwt-svg. 4 * 5 * libgwtsvg 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 * libgwtsvg 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 libgwtsvg. If not, see http://www.gnu.org/licenses/ 17 **********************************************/ 18 /* 19 * Copyright (c) 2004 World Wide Web Consortium, 20 * 21 * (Massachusetts Institute of Technology, European Research Consortium for 22 * Informatics and Mathematics, Keio University). All Rights Reserved. This 23 * work is distributed under the W3C(r) Software License [1] in the hope that 24 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied 25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 26 * 27 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231 28 */ 29 30 package org.vectomatic.dom.svg; 31 32 import java.util.Iterator; 33 34 import com.google.gwt.core.client.JavaScriptException; 35 import com.google.gwt.core.client.JavaScriptObject; 36 37 /** 38 * <p>This interface defines a list of DOMString values.</p> <p>{@link org.vectomatic.dom.svg.OMSVGStringList} 39 * has the same attributes and methods as other SVGxxxList interfaces. Implementers 40 * may consider using a single base class to implement the various SVGxxxList 41 * interfaces.</p> 42 */ 43 public class OMSVGStringList implements Iterable<String> { 44 private JavaScriptObject ot; 45 protected OMSVGStringList(JavaScriptObject ot) { 46 this.ot = ot; 47 } 48 49 // Implementation of the svg::SVGStringList W3C IDL interface 50 /** 51 * The number of items in the list. 52 */ 53 public final native int getNumberOfItems() /*-{ 54 return this.@org.vectomatic.dom.svg.OMSVGStringList::ot.numberOfItems; 55 }-*/; 56 /** 57 * Clears all existing current items from the list, with the result being 58 * an empty list. 59 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the list 60 * cannot be modified. 61 */ 62 public final native void clear() throws JavaScriptException /*-{ 63 this.@org.vectomatic.dom.svg.OMSVGStringList::ot.clear(); 64 }-*/; 65 /** 66 * Clears all existing current items from the list and re-initializes the 67 * list to hold the single item specified by the parameter. 68 * @param newItem The item which should become the only member of the list. 69 * @return The item being inserted into the list. 70 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the list 71 * cannot be modified. 72 */ 73 public final native String initialize(String newItem) throws JavaScriptException /*-{ 74 return this.@org.vectomatic.dom.svg.OMSVGStringList::ot.initialize(newItem); 75 }-*/; 76 /** 77 * Returns the specified item from the list. 78 * @param index The index of the item from the list which is to be returned. 79 * The first item is number 0. 80 * @return The selected item. 81 * @throws DOMException(INDEX_SIZE_ERR) Raised if the index number is greater 82 * than or equal to {@link org.vectomatic.dom.svg.OMSVGStringList#getNumberOfItems()}. 83 */ 84 public final native String getItem(int index) throws JavaScriptException /*-{ 85 return this.@org.vectomatic.dom.svg.OMSVGStringList::ot.getItem(index); 86 }-*/; 87 /** 88 * Inserts a new item into the list at the specified position. The first item 89 * is number 0. 90 * @param newItem The item which is to be inserted into the list. 91 * @param index The index of the item before which the new item is to be 92 * inserted. The first item is number 0. If the index is equal to 0, then 93 * the new item is inserted at the front of the list. If the index is greater 94 * than or equal to {@link org.vectomatic.dom.svg.OMSVGStringList#getNumberOfItems()}, 95 * then the new item is appended to the end of the list. 96 * @return The inserted item. 97 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the list 98 * cannot be modified. 99 */ 100 public final native String insertItemBefore(String newItem, int index) throws JavaScriptException /*-{ 101 return this.@org.vectomatic.dom.svg.OMSVGStringList::ot.insertItemBefore(newItem, index); 102 }-*/; 103 /** 104 * Replaces an existing item in the list with a new item. 105 * @param newItem The item which is to be inserted into the list. 106 * @param index The index of the item which is to be replaced. The first 107 * item is number 0. 108 * @return The inserted item. 109 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the list 110 * cannot be modified. 111 * @throws DOMException(INDEX_SIZE_ERR) Raised if the index number is greater 112 * than or equal to {@link org.vectomatic.dom.svg.OMSVGStringList#getNumberOfItems()}. 113 */ 114 public final native String replaceItem(String newItem, int index) throws JavaScriptException /*-{ 115 return this.@org.vectomatic.dom.svg.OMSVGStringList::ot.replaceItem(newItem, index); 116 }-*/; 117 /** 118 * Removes an existing item from the list. 119 * @param index The index of the item which is to be removed. The first 120 * item is number 0. 121 * @return The removed item. 122 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the list 123 * cannot be modified. 124 * @throws DOMException(INDEX_SIZE_ERR) Raised if the index number is greater 125 * than or equal to {@link org.vectomatic.dom.svg.OMSVGStringList#getNumberOfItems()}. 126 */ 127 public final native String removeItem(int index) throws JavaScriptException /*-{ 128 return this.@org.vectomatic.dom.svg.OMSVGStringList::ot.removeItem(index); 129 }-*/; 130 /** 131 * Inserts a new item at the end of the list. 132 * @param newItem The item which is to be inserted. The first item is number 133 * 0. 134 * @return The inserted item. 135 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the list 136 * cannot be modified. 137 */ 138 public final native String appendItem(String newItem) throws JavaScriptException /*-{ 139 return this.@org.vectomatic.dom.svg.OMSVGStringList::ot.appendItem(newItem); 140 }-*/; 141 142 /** 143 * Returns an iterator over the {@link java.lang.String} 144 * elements in this list in proper sequence. 145 * 146 * <p>This implementation returns a straightforward implementation of the 147 * iterator interface, relying on the backing list's {@code getNumberOfItems()}, 148 * and {@code getItem(int)} methods. 149 * 150 * <p>Note that the iterator returned by this method will throw an 151 * {@code UnsupportedOperationException} in response to its 152 * {@code remove} method. 153 * 154 * @return an iterator over the {@link java.lang.String} 155 * elements in this list in proper sequence 156 */ 157 @Override 158 public final Iterator<String> iterator() { 159 return new Iterator<String>() { 160 private int index; 161 @Override 162 public boolean hasNext() { 163 return index < getNumberOfItems(); 164 } 165 166 @Override 167 public String next() { 168 return getItem(index++); 169 } 170 171 @Override 172 public void remove() { 173 throw new UnsupportedOperationException(); 174 } 175 }; 176 } 177 178 }