1 16 package com.google.gwt.user.client.ui; 17 18 import java.util.Iterator ; 19 import java.util.NoSuchElementException ; 20 21 26 class WidgetIterators { 27 28 private static final Widget[] copyWidgetArray(final Widget[] widgets) { 29 final Widget[] clone = new Widget[widgets.length]; 30 for (int i = 0; i < widgets.length; i++) { 31 clone[i] = widgets[i]; 32 } 33 return clone; 34 } 35 36 45 static final Iterator createWidgetIterator(final HasWidgets container, 46 final Widget[] contained) { 47 return new Iterator () { 48 int index = -1, last = -1; 49 boolean widgetsWasCopied = false; 50 Widget[] widgets = contained; 51 52 { 53 gotoNextIndex(); 54 } 55 56 private void gotoNextIndex() { 57 ++index; 58 while (index < contained.length) { 59 if (contained[index] != null) { 60 return; 61 } 62 ++index; 63 } 64 } 65 66 public boolean hasNext() { 67 return (index < contained.length); 68 } 69 70 public Object next() { 71 if (!hasNext()) { 72 throw new NoSuchElementException (); 73 } 74 last = index; 75 final Widget w = contained[index]; 76 gotoNextIndex(); 77 return w; 78 } 79 80 public void remove() { 81 if (last < 0) { 82 throw new IllegalStateException (); 83 } 84 85 if (!widgetsWasCopied) { 86 widgets = copyWidgetArray(widgets); 87 widgetsWasCopied = true; 88 } 89 90 container.remove(contained[last]); 91 last = -1; 92 } 93 }; 94 } 95 96 private WidgetIterators() { 97 } 99 } 100 | Popular Tags |