/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.topspin.ui.client;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.TableCellElement;
import com.google.gwt.dom.client.TableElement;
import com.google.gwt.dom.client.TableRowElement;
/**
* A container that positions its children horizontally, using a table.
*
* TODO(jgw): This panel needs to provide an inserting container.
*/
public class HorizontalPanel extends Widget implements HasContainer {
private TableRowElement tr;
private ContainerImpl defaultContainer = new ContainerImpl() {
public void add(Widget widget) {
TableCellElement td = tr.insertCell(-1);
td.appendChild(widget.getElement());
}
public Document getDocument() {
return getElement().getOwnerDocument();
}
public void remove(Widget widget) {
tr.removeChild(getWidgetCell(widget));
}
};
/**
* Creates an new AbsolutePanel in the given container.
*
* @param container the container in which the widget will be created
*/
public HorizontalPanel(Container container) {
super(container.getDocument().createTableElement(), container);
TableElement table = (TableElement) getElement();
table.setCellSpacing(0);
table.setCellPadding(0);
tr = table.insertRow(-1);
}
/**
* Sets the width of the cell containing the given widget.
*
* @param widget the widget whose table cell is to be modified
* @param width the cell width, in CSS units
*/
public void setCellWidth(Widget widget, String width) {
getWidgetCell(widget).getStyle().setProperty("width", width);
}
/**
* Sets the height of the cell containing the given widget.
*
* @param widget the widget whose table cell is to be modified
* @param height the cell height, in CSS units
*/
public void setCellHeight(Widget widget, String height) {
getWidgetCell(widget).getStyle().setProperty("height", height);
}
/**
* Sets the horizontal alignment of the cell containing the given widget.
*
* @param widget the widget whose table cell is to be modified
* @param align the CSS alignment value (one of "left", "right", or "center")
*/
public void setCellHorizontalAlignment(Widget widget, String align) {
getWidgetCell(widget).setAttribute("align", align);
}
/**
* Sets the vertical alignment of the cell containing the given widget.
*
* @param widget the widget whose table cell is to be modified
* @param valign the CSS alignment value (one of "top", "bottom", or "middle")
*/
public void setCellVerticalAlignment(Widget widget, String valign) {
getWidgetCell(widget).setAttribute("valign", valign);
}
/**
* Gets a container that appends child widgets to this panel.
*
* @return a container
*/
public Container getContainer() {
return defaultContainer;
}
private Element getWidgetCell(Widget widget) {
// TODO(jgw): assert that we own this widget somehow.
return widget.getElement().getParentElement();
}
}