KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2007 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.DOM;
19 import com.google.gwt.user.client.Element;
20
21 /**
22  * An absolute panel positions all of its children absolutely, allowing them to
23  * overlap.
24  *
25  * <p>
26  * Note that this panel will not automatically resize itself to allow enough
27  * room for its absolutely-positioned children. It must be explicitly sized in
28  * order to make room for them.
29  * </p>
30  *
31  * <p>
32  * Once a widget has been added to an absolute panel, the panel effectively
33  * "owns" the positioning of the widget. Any existing positioning attributes
34  * on the widget may be modified by the panel.
35  * </p>
36  */

37 public class AbsolutePanel extends ComplexPanel {
38
39   /**
40    * Changes a DOM element's positioning to static.
41    *
42    * @param elem the DOM element
43    */

44   private static void changeToStaticPositioning(Element elem) {
45     DOM.setStyleAttribute(elem, "left", "");
46     DOM.setStyleAttribute(elem, "top", "");
47     DOM.setStyleAttribute(elem, "position", "static");
48   }
49
50   /**
51    * Creates an empty absolute panel.
52    */

53   public AbsolutePanel() {
54     setElement(DOM.createDiv());
55
56     // Setting the panel's position style to 'relative' causes it to be treated
57
// as a new positioning context for its children.
58
DOM.setStyleAttribute(getElement(), "position", "relative");
59     DOM.setStyleAttribute(getElement(), "overflow", "hidden");
60   }
61
62   /**
63    * Adds a child widget to this panel.
64    *
65    * @param w the child widget to be added
66    */

67   public void add(Widget w) {
68     super.add(w, getElement());
69   }
70
71   /**
72    * Adds a widget to the panel at the specified position. Setting a position
73    * of <code>(-1, -1)</code> will cause the child widget to be positioned
74    * statically.
75    *
76    * @param w the widget to be added
77    * @param left the widget's left position
78    * @param top the widget's top position
79    */

80   public void add(Widget w, int left, int top) {
81     // In order to avoid the potential for a flicker effect, it is necessary
82
// to set the position of the widget before adding it to the AbsolutePanel.
83
// The Widget should be removed from its parent before any positional
84
// changes are made to prevent flickering.
85
w.removeFromParent();
86     setWidgetPositionImpl(w, left, top);
87     add(w);
88   }
89
90   /**
91    * Gets the position of the left outer border edge of the widget relative to
92    * the left outer border edge of the panel.
93    *
94    * @param w the widget whose position is to be retrieved
95    * @return the widget's left position
96    */

97   public int getWidgetLeft(Widget w) {
98     checkWidgetParent(w);
99     return DOM.getAbsoluteLeft(w.getElement()) - DOM.getAbsoluteLeft(getElement());
100   }
101
102   /**
103    * Gets the position of the top outer border edge of the widget relative to
104    * the top outer border edge of the panel.
105    *
106    * @param w the widget whose position is to be retrieved
107    * @return the widget's top position
108    */

109   public int getWidgetTop(Widget w) {
110     checkWidgetParent(w);
111     return DOM.getAbsoluteTop(w.getElement()) - DOM.getAbsoluteTop(getElement());
112   }
113
114   /**
115    * Sets the position of the specified child widget. Setting a position of
116    * <code>(-1, -1)</code> will cause the child widget to be positioned
117    * statically.
118    *
119    * @param w the child widget to be positioned
120    * @param left the widget's left position
121    * @param top the widget's top position
122    */

123   public void setWidgetPosition(Widget w, int left, int top) {
124     checkWidgetParent(w);
125     setWidgetPositionImpl(w, left, top);
126   }
127
128   /**
129    * Calls the superclass' <code>disown(Widget)</code> method, and sets the
130    * the positioning of the widget to static. This is done so that any
131    * positioning changes to the widget that were done by the panel are undone
132    * when the widget is disowned from the panel.
133    *
134    * @param w the widget to be disowned
135    */

136   protected void disown(Widget w) {
137     super.disown(w);
138     changeToStaticPositioning(w.getElement());
139   }
140
141   private void checkWidgetParent(Widget w) {
142     if (w.getParent() != this) {
143       throw new IllegalArgumentException JavaDoc(
144           "Widget must be a child of this panel.");
145     }
146   }
147
148   private void setWidgetPositionImpl(Widget w, int left, int top) {
149     Element h = w.getElement();
150     if ((left == -1) && (top == -1)) {
151       changeToStaticPositioning(h);
152     } else {
153       DOM.setStyleAttribute(h, "position", "absolute");
154       DOM.setStyleAttribute(h, "left", left + "px");
155       DOM.setStyleAttribute(h, "top", top + "px");
156     }
157   }
158 }
159
Popular Tags