001: /*
002: * Copyright 2007 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: * A panel that lays all of its widgets out in a single horizontal column.
023: *
024: * <p>
025: * <img class='gallery' src='HorizontalPanel.png'/>
026: * </p>
027: */
028: public class HorizontalPanel extends CellPanel implements HasAlignment {
029:
030: private HorizontalAlignmentConstant horzAlign = ALIGN_LEFT;
031: private Element tableRow;
032: private VerticalAlignmentConstant vertAlign = ALIGN_TOP;
033:
034: /**
035: * Creates an empty horizontal panel.
036: */
037: public HorizontalPanel() {
038: tableRow = DOM.createTR();
039: DOM.appendChild(getBody(), tableRow);
040:
041: DOM.setElementProperty(getTable(), "cellSpacing", "0");
042: DOM.setElementProperty(getTable(), "cellPadding", "0");
043: }
044:
045: /**
046: * Adds a child widget to the panel. If the Widget is already attached to the
047: * HorizontalPanel, it will be moved to the end of the panel.
048: *
049: * @param w the widget to be added
050: */
051: @Override
052: public void add(Widget w) {
053: Element td = createAlignedTd();
054: DOM.appendChild(tableRow, td);
055: super .add(w, td);
056: }
057:
058: public HorizontalAlignmentConstant getHorizontalAlignment() {
059: return horzAlign;
060: }
061:
062: public VerticalAlignmentConstant getVerticalAlignment() {
063: return vertAlign;
064: }
065:
066: /**
067: * Inserts a widget before the specified index. If the Widget is already
068: * attached to the HorizontalPanel, it will be moved to the specified index.
069: *
070: * @param w the widget to be inserted
071: * @param beforeIndex the index before which it will be inserted
072: * @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of
073: * range
074: */
075: public void insert(Widget w, int beforeIndex) {
076: checkIndexBoundsForInsertion(beforeIndex);
077:
078: /*
079: * The case where we reinsert an already existing child is tricky.
080: *
081: * For the WIDGET, it ultimately removes first and inserts second, so we
082: * have to adjust the index within ComplexPanel.insert(). But for the DOM,
083: * we insert first and remove second, which means we DON'T need to adjust
084: * the index.
085: */
086: Element td = createAlignedTd();
087: DOM.insertChild(tableRow, td, beforeIndex);
088: super .insert(w, td, beforeIndex, false);
089: }
090:
091: @Override
092: public boolean remove(Widget w) {
093: // Get the TD to be removed, before calling super.remove(), because
094: // super.remove() will detach the child widget's element from its parent.
095: Element td = DOM.getParent(w.getElement());
096: boolean removed = super .remove(w);
097: if (removed) {
098: DOM.removeChild(tableRow, td);
099: }
100: return removed;
101: }
102:
103: /**
104: * Sets the default horizontal 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 HasHorizontalAlignment#setHorizontalAlignment(HasHorizontalAlignment.HorizontalAlignmentConstant)
108: */
109: public void setHorizontalAlignment(HorizontalAlignmentConstant align) {
110: horzAlign = align;
111: }
112:
113: /**
114: * Sets the default vertical alignment to be used for widgets added to this
115: * panel. It only applies to widgets added after this property is set.
116: *
117: * @see HasVerticalAlignment#setVerticalAlignment(HasVerticalAlignment.VerticalAlignmentConstant)
118: */
119: public void setVerticalAlignment(VerticalAlignmentConstant align) {
120: vertAlign = align;
121: }
122:
123: private Element createAlignedTd() {
124: Element td = DOM.createTD();
125: setCellHorizontalAlignment(td, horzAlign);
126: setCellVerticalAlignment(td, vertAlign);
127: return td;
128: }
129: }
|