KickJava   Java API By Example, From Geeks To Geeks.

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


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  * A panel that lays all of its widgets out in a single vertical column.
23  *
24  * <p>
25  * <img class='gallery' SRC='VerticalPanel.png'/>
26  * </p>
27  */

28 public class VerticalPanel extends CellPanel implements HasAlignment {
29
30   private HorizontalAlignmentConstant horzAlign = ALIGN_LEFT;
31   private VerticalAlignmentConstant vertAlign = ALIGN_TOP;
32
33   /**
34    * Creates an empty vertical panel.
35    */

36   public VerticalPanel() {
37     DOM.setElementProperty(getTable(), "cellSpacing", "0");
38     DOM.setElementProperty(getTable(), "cellPadding", "0");
39   }
40
41   /**
42    * Adds a child widget to the panel. If the Widget is already attached to the
43    * VerticalPanel, it will be moved to the end of the panel.
44    *
45    * @param w the widget to be added
46    */

47   public void add(Widget w) {
48     insert(w, getWidgetCount());
49   }
50
51   public HorizontalAlignmentConstant getHorizontalAlignment() {
52     return horzAlign;
53   }
54
55   public VerticalAlignmentConstant getVerticalAlignment() {
56     return vertAlign;
57   }
58
59   /**
60    * Inserts a widget before the specified index. If the Widget is already
61    * attached to the VerticalPanel, it will be moved to the specified index.
62    *
63    * @param w the widget to be inserted
64    * @param beforeIndex the index before which it will be inserted
65    * @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of
66    * range
67    */

68   public void insert(Widget w, int beforeIndex) {
69     Element tr = DOM.createTR();
70     Element td = DOM.createTD();
71     beforeIndex = super.insert(w, td, beforeIndex);
72     DOM.appendChild(tr, td);
73     DOM.insertChild(getBody(), tr, beforeIndex);
74
75     setCellHorizontalAlignment(w, horzAlign);
76     setCellVerticalAlignment(w, vertAlign);
77   }
78
79   public boolean remove(Widget w) {
80     if (w.getParent() != this) {
81       return false;
82     }
83
84     Element td = DOM.getParent(w.getElement());
85     Element tr = DOM.getParent(td);
86     DOM.removeChild(getBody(), tr);
87
88     super.remove(w);
89     return true;
90   }
91
92   /**
93    * Sets the default horizontal alignment to be used for widgets added to this
94    * panel. It only applies to widgets added after this property is set.
95    *
96    * @see HasHorizontalAlignment#setHorizontalAlignment(HasHorizontalAlignment.HorizontalAlignmentConstant)
97    */

98   public void setHorizontalAlignment(HorizontalAlignmentConstant align) {
99     horzAlign = align;
100   }
101
102   /**
103    * Sets the default vertical alignment to be used for widgets added to this
104    * panel. It only applies to widgets added after this property is set.
105    *
106    * @see HasVerticalAlignment#setVerticalAlignment(HasVerticalAlignment.VerticalAlignmentConstant)
107    */

108   public void setVerticalAlignment(VerticalAlignmentConstant align) {
109     vertAlign = align;
110   }
111 }
112
Popular Tags