KickJava   Java API By Example, From Geeks To Geeks.

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


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 horizontal column.
23  *
24  * <p>
25  * <img class='gallery' SRC='HorizontalPanel.png'/>
26  * </p>
27  */

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

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

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

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

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

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