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 com.google.gwt.user.client.DOM;
019    import com.google.gwt.user.client.Element;
020    
021    /**
022     * An absolute panel positions all of its children absolutely, allowing them to
023     * overlap as well.
024     */
025    public class AbsolutePanel extends ComplexPanel {
026    
027      /**
028       * Creates an empty absolute panel.
029       */
030      public AbsolutePanel() {
031        setElement(DOM.createDiv());
032        DOM.setStyleAttribute(getElement(), "overflow", "hidden");
033      }
034    
035      public boolean add(Widget w) {
036        super.add(w);
037        DOM.appendChild(getElement(), w.getElement());
038        return true;
039      }
040    
041      /**
042       * Adds a widget to the panel at the specified position.
043       * 
044       * @param w the widget to be added
045       * @param left the widget's left position
046       * @param top the widget's top position
047       * @return <code>true</code>
048       */
049      public boolean add(Widget w, int left, int top) {
050        add(w);
051        setWidgetPosition(w, left, top);
052        return true;
053      }
054    
055      public boolean remove(Widget w) {
056        if (super.remove(w)) {
057          DOM.removeChild(getElement(), w.getElement());
058        }
059        return true;
060      }
061    
062      /**
063       * Sets the position of the specified child widget.
064       * 
065       * @param w the child widget to be positioned
066       * @param left the widget's left position
067       * @param top the widget's top position
068       */
069      public void setWidgetPosition(Widget w, int left, int top) {
070        Element h = w.getElement();
071        if ((left == -1) && (top == -1)) {
072          DOM.setStyleAttribute(h, "left", "");
073          DOM.setStyleAttribute(h, "top", "");
074          DOM.setStyleAttribute(h, "position", "static");
075        } else {
076          DOM.setStyleAttribute(h, "position", "absolute");
077          DOM.setIntStyleAttribute(h, "left", left);
078          DOM.setIntStyleAttribute(h, "top", top);
079        }
080      }
081    
082    }