KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > WidgetCollection


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client.ui;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.NoSuchElementException JavaDoc;
20
21 /**
22  * A simple collection of widgets to be used by
23  * {@link com.google.gwt.user.client.ui.Panel panels} and
24  * {@link com.google.gwt.user.client.ui.Composite composites}.
25  *
26  * <p>
27  * The main purpose of this specialized collection is to implement
28  * {@link java.util.Iterator#remove()} in a way that delegates removal to its
29  * panel. This makes it much easier for the panel to implement an
30  * {@link com.google.gwt.user.client.ui.HasWidgets#iterator() iterator} that
31  * supports removal of widgets.
32  * </p>
33  */

34 public class WidgetCollection {
35
36   private class WidgetIterator implements Iterator JavaDoc {
37
38     private int index = -1;
39
40     public boolean hasNext() {
41       return index < (size - 1);
42     }
43
44     public Object JavaDoc next() {
45       if (index >= size) {
46         throw new NoSuchElementException JavaDoc();
47       }
48       return array[++index];
49     }
50
51     public void remove() {
52       if ((index < 0) || (index >= size)) {
53         throw new IllegalStateException JavaDoc();
54       }
55       parent.remove(array[index--]);
56     }
57   }
58
59   private static final int INITIAL_SIZE = 4;
60
61   private Widget[] array;
62   private HasWidgets parent;
63   private int size;
64
65   /**
66    * Constructs a new widget collection.
67    *
68    * @param parent the container whose {@link HasWidgets#remove(Widget)} will be
69    * delegated to by the iterator's {@link Iterator#remove()} method.
70    */

71   public WidgetCollection(HasWidgets parent) {
72     this.parent = parent;
73     array = new Widget[INITIAL_SIZE];
74   }
75
76   /**
77    * Adds a widget to the end of this collection.
78    *
79    * @param w the widget to be added
80    */

81   public void add(Widget w) {
82     insert(w, size);
83   }
84
85   /**
86    * Determines whether a given widget is contained in this collection.
87    *
88    * @param w the widget to be searched for
89    * @return <code>true</code> if the widget is present
90    */

91   public boolean contains(Widget w) {
92     return (indexOf(w) != -1);
93   }
94
95   /**
96    * Gets the widget at the given index.
97    *
98    * @param index the index to be retrieved
99    * @return the widget at the specified index
100    * @throws IndexOutOfBoundsException if the index is out of range
101    */

102   public Widget get(int index) {
103     if ((index < 0) || (index >= size)) {
104       throw new IndexOutOfBoundsException JavaDoc();
105     }
106
107     return array[index];
108   }
109
110   /**
111    * Gets the index of the specified index.
112    *
113    * @param w the widget to be found
114    * @return the index of the specified widget, or <code>-1</code> if it is
115    * not found
116    */

117   public int indexOf(Widget w) {
118     for (int i = 0; i < size; ++i) {
119       if (array[i] == w) {
120         return i;
121       }
122     }
123
124     return -1;
125   }
126
127   /**
128    * Inserts a widget before the specified index.
129    *
130    * @param w the widget to be inserted
131    * @param beforeIndex the index before which the widget will be inserted
132    * @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of
133    * range
134    */

135   public void insert(Widget w, int beforeIndex) {
136     if ((beforeIndex < 0) || (beforeIndex > size)) {
137       throw new IndexOutOfBoundsException JavaDoc();
138     }
139
140     // Realloc array if necessary (doubling).
141
if (size == array.length) {
142       Widget[] newArray = new Widget[array.length * 2];
143       for (int i = 0; i < array.length; ++i) {
144         newArray[i] = array[i];
145       }
146       array = newArray;
147     }
148
149     ++size;
150
151     // Move all widgets after 'beforeIndex' back a slot.
152
for (int i = size - 1; i > beforeIndex; --i) {
153       array[i] = array[i - 1];
154     }
155
156     array[beforeIndex] = w;
157   }
158
159   /**
160    * Gets an iterator on this widget collection. This iterator is guaranteed to
161    * implement remove() in terms of its containing {@link HasWidgets}.
162    *
163    * @return an iterator
164    */

165   public Iterator JavaDoc iterator() {
166     return new WidgetIterator();
167   }
168
169   /**
170    * Removes the widget at the specified index.
171    *
172    * @param index the index of the widget to be removed
173    * @throws IndexOutOfBoundsException if <code>index</code> is out of range
174    */

175   public void remove(int index) {
176     if ((index < 0) || (index >= size)) {
177       throw new IndexOutOfBoundsException JavaDoc();
178     }
179
180     --size;
181     for (int i = index; i < size; ++i) {
182       array[i] = array[i + 1];
183     }
184
185     array[size] = null;
186   }
187
188   /**
189    * Removes the specified widget.
190    *
191    * @param w the widget to be removed
192    * @throws NoSuchElementException if the widget is not present
193    */

194   public void remove(Widget w) {
195     int index = indexOf(w);
196     if (index == -1) {
197       throw new NoSuchElementException JavaDoc();
198     }
199
200     remove(index);
201   }
202
203   /**
204    * Gets the number of widgets in this collection.
205    *
206    * @return the number of widgets
207    */

208   public int size() {
209     return size;
210   }
211 }
212
Popular Tags