001 /*
002 * Copyright 2006 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 Element body = DOM.createTBody();
039 tableRow = DOM.createTR();
040 DOM.appendChild(getTable(), body);
041 DOM.appendChild(body, tableRow);
042
043 DOM.setAttribute(getTable(), "cellSpacing", "0");
044 DOM.setAttribute(getTable(), "cellPadding", "0");
045
046 setElement(getTable());
047 }
048
049 public boolean add(Widget w) {
050 return insert(w, getWidgetCount());
051 }
052
053 public HorizontalAlignmentConstant getHorizontalAlignment() {
054 return horzAlign;
055 }
056
057 public VerticalAlignmentConstant getVerticalAlignment() {
058 return vertAlign;
059 }
060
061 public boolean insert(Widget w, int beforeIndex) {
062 if (!super.insert(w, beforeIndex))
063 return false;
064
065 Element td = DOM.createTD();
066 DOM.insertChild(tableRow, td, beforeIndex);
067 DOM.appendChild(td, w.getElement());
068
069 DOM.setStyleAttribute(w.getElement(), "position", "static");
070 setCellHorizontalAlignment(w, horzAlign);
071 setCellVerticalAlignment(w, vertAlign);
072 return true;
073 }
074
075 public boolean remove(Widget w) {
076 if (w.getParent() != this)
077 return false;
078
079 Element td = DOM.getParent(w.getElement());
080 DOM.removeChild(tableRow, td);
081
082 super.remove(w);
083 return true;
084 }
085
086 /**
087 * Sets the default horizontal alignment to be used for widgets added to this
088 * panel. It only applies to widgets added after this property is set.
089 *
090 * @see HasHorizontalAlignment#setHorizontalAlignment(HorizontalAlignmentConstant)
091 */
092 public void setHorizontalAlignment(HorizontalAlignmentConstant align) {
093 horzAlign = align;
094 }
095
096 /**
097 * Sets the default vertical alignment to be used for widgets added to this
098 * panel. It only applies to widgets added after this property is set.
099 *
100 * @see HasVerticalAlignment#setVerticalAlignment(VerticalAlignmentConstant)
101 */
102 public void setVerticalAlignment(VerticalAlignmentConstant align) {
103 vertAlign = align;
104 }
105
106 public void setCellHorizontalAlignment(Widget w,
107 HorizontalAlignmentConstant align) {
108 Element td = getWidgetTd(w);
109 if (td != null) {
110 DOM.setAttribute(td, "align", align.getTextAlignString());
111 }
112 }
113
114 public void setCellVerticalAlignment(Widget w, VerticalAlignmentConstant align) {
115 Element td = getWidgetTd(w);
116 if (td != null) {
117 DOM
118 .setStyleAttribute(td, "verticalAlign", align.getVerticalAlignString());
119 }
120 }
121
122 public void setCellHeight(Widget w, String height) {
123 Element td = DOM.getParent(w.getElement());
124 DOM.setAttribute(td, "height", height);
125 }
126
127 public void setCellWidth(Widget w, String width) {
128 Element td = DOM.getParent(w.getElement());
129 DOM.setAttribute(td, "width", width);
130 }
131
132 private Element getWidgetTd(Widget w) {
133 if (w.getParent() != this)
134 return null;
135 return DOM.getParent(w.getElement());
136 }
137 }