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