001    /*
002     * Copyright 2006 Google Inc.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package com.google.gwt.user.client.ui;
017    
018    import java.util.Iterator;
019    import java.util.Vector;
020    
021    /**
022     * Abstract base class for panels that can contain multiple child widgets.
023     */
024    public abstract class ComplexPanel extends Panel {
025    
026      private Vector children = new Vector();
027    
028      public boolean add(Widget w) {
029        children.add(w);
030        w.setParent(this);
031        return true;
032      }
033    
034      public void clear() {
035        int count = getWidgetCount();
036        for (int i = 0; i < count; i++) {
037          remove(getWidget(0));
038        }
039      }
040    
041      public Widget getWidget(int index) {
042        return (Widget) children.get(index);
043      }
044    
045      public int getWidgetCount() {
046        return children.size();
047      }
048    
049      public int getWidgetIndex(Widget child) {
050        return children.indexOf(child);
051      }
052    
053      /**
054       * Inserts a widget into the panel
055       * 
056       * @param w the widget to be inserted
057       * @param beforeIndex the index before which it will be inserted
058       * @return <code>true</code> on success (some panels place restrictions on
059       *         how children may be added or inserted)
060       */
061      public boolean insert(Widget w, int beforeIndex) {
062        if (beforeIndex > children.size())
063          return false;
064    
065        children.insertElementAt(w, beforeIndex);
066        w.setParent(this);
067        return true;
068      }
069    
070      public Iterator iterator() {
071        return children.iterator();
072      }
073    
074      public boolean remove(Widget w) {
075        w.setParent(null);
076        return children.remove(w);
077      }
078    }