KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.google.gwt.user.client.Element;
19
20 import java.util.Iterator JavaDoc;
21
22 /**
23  * Abstract base class for panels that can contain multiple child widgets.
24  */

25 public abstract class ComplexPanel extends Panel implements IndexedPanel {
26
27   private WidgetCollection children = new WidgetCollection(this);
28
29   public Widget getWidget(int index) {
30     return getChildren().get(index);
31   }
32
33   public int getWidgetCount() {
34     return getChildren().size();
35   }
36
37   public int getWidgetIndex(Widget child) {
38     return getChildren().indexOf(child);
39   }
40
41   public Iterator JavaDoc iterator() {
42     return getChildren().iterator();
43   }
44
45   public boolean remove(int index) {
46     return remove(getWidget(index));
47   }
48
49   public boolean remove(Widget w) {
50     // Make sure this panel actually contains the child widget.
51
if (!getChildren().contains(w)) {
52       return false;
53     }
54
55     // Disown it.
56
disown(w);
57
58     getChildren().remove(w);
59     return true;
60   }
61
62   /**
63    * Adds a new child widget to the panel.
64    *
65    * @param w the child widget to be added
66    * @param container the element within which the child will be contained
67    * @return the index at which the widget was added
68    */

69   protected int add(Widget w, Element container) {
70     return insert(w, container, getChildren().size());
71   }
72
73   /**
74    * Gets the list of children contained in this panel.
75    *
76    * @return a collection of child widgets
77    */

78   protected WidgetCollection getChildren() {
79     return children;
80   }
81
82   /**
83    * Inserts a new child widget into the panel.
84    *
85    * @param w the child widget to be added
86    * @param container the element within which the child will be contained
87    * @param beforeIndex the index before which the widget will be added
88    * @return the index at which the widget was added
89    */

90   protected int insert(Widget w, Element container, int beforeIndex) {
91     if ((beforeIndex < 0) || (beforeIndex > getWidgetCount())) {
92       throw new IndexOutOfBoundsException JavaDoc();
93     }
94     // Call this early to ensure that the table doesn't end up partially
95
// constructed when an exception is thrown from adopt().
96
int idx = getWidgetIndex(w);
97     if (idx == -1) {
98       w.removeFromParent();
99     } else {
100       remove(w);
101       // If the Widget's previous position was left of the desired new position
102
// shift the desired position left to reflect the removal
103
if (idx < beforeIndex) {
104         beforeIndex--;
105       }
106     }
107     adopt(w, container);
108     getChildren().insert(w, beforeIndex);
109     return beforeIndex;
110   }
111 }
112
Popular Tags