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: import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
021: import com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant;
022:
023: /**
024: * A panel whose child widgets are contained within the cells of a table. Each
025: * cell's size may be set independently. Each child widget can take up a subset
026: * of its cell and can be aligned within it.
027: */
028: public abstract class CellPanel extends ComplexPanel {
029:
030: private int spacing;
031: private Element table, body;
032:
033: public CellPanel() {
034: table = DOM.createTable();
035: body = DOM.createTBody();
036: DOM.appendChild(table, body);
037: setElement(table);
038: }
039:
040: /**
041: * Gets the amount of spacing between this panel's cells.
042: *
043: * @return the inter-cell spacing, in pixels
044: */
045: public int getSpacing() {
046: return spacing;
047: }
048:
049: /**
050: * Sets the width of the border to be applied to all cells in this panel. This
051: * is particularly useful when debugging layouts, in that it allows you to see
052: * explicitly the cells that contain this panel's children.
053: *
054: * @param width the width of the panel's cell borders, in pixels
055: */
056: public void setBorderWidth(int width) {
057: DOM.setElementProperty(table, "border", "" + width);
058: }
059:
060: /**
061: * Sets the height of the cell associated with the given widget, related to
062: * the panel as a whole.
063: *
064: * @param w the widget whose cell height is to be set
065: * @param height the cell's height, in CSS units
066: */
067: public void setCellHeight(Widget w, String height) {
068: Element td = DOM.getParent(w.getElement());
069: DOM.setElementProperty(td, "height", height);
070: }
071:
072: /**
073: * Sets the horizontal alignment of the given widget within its cell.
074: *
075: * @param w the widget whose horizontal alignment is to be set
076: * @param align the widget's horizontal alignment, as defined in
077: * {@link HasHorizontalAlignment}.
078: */
079: public void setCellHorizontalAlignment(Widget w,
080: HorizontalAlignmentConstant align) {
081: Element td = getWidgetTd(w);
082: if (td != null) {
083: setCellHorizontalAlignment(td, align);
084: }
085: }
086:
087: /**
088: * Sets the vertical alignment of the given widget within its cell.
089: *
090: * @param w the widget whose vertical alignment is to be set
091: * @param align the widget's vertical alignment, as defined in
092: * {@link HasVerticalAlignment}.
093: */
094: public void setCellVerticalAlignment(Widget w,
095: VerticalAlignmentConstant align) {
096: Element td = getWidgetTd(w);
097: if (td != null) {
098: setCellVerticalAlignment(td, align);
099: }
100: }
101:
102: /**
103: * Sets the width of the cell associated with the given widget, related to the
104: * panel as a whole.
105: *
106: * @param w the widget whose cell width is to be set
107: * @param width the cell's width, in CSS units
108: */
109: public void setCellWidth(Widget w, String width) {
110: Element td = DOM.getParent(w.getElement());
111: DOM.setElementProperty(td, "width", width);
112: }
113:
114: /**
115: * Sets the amount of spacing between this panel's cells.
116: *
117: * @param spacing the inter-cell spacing, in pixels
118: */
119: public void setSpacing(int spacing) {
120: this .spacing = spacing;
121: DOM.setElementPropertyInt(table, "cellSpacing", spacing);
122: }
123:
124: protected Element getBody() {
125: return body;
126: }
127:
128: protected Element getTable() {
129: return table;
130: }
131:
132: protected void setCellHorizontalAlignment(Element td,
133: HorizontalAlignmentConstant align) {
134: DOM.setElementProperty(td, "align", align.getTextAlignString());
135: }
136:
137: protected void setCellVerticalAlignment(Element td,
138: VerticalAlignmentConstant align) {
139: DOM.setStyleAttribute(td, "verticalAlign", align
140: .getVerticalAlignString());
141: }
142:
143: private Element getWidgetTd(Widget w) {
144: if (w.getParent() != this) {
145: return null;
146: }
147: return DOM.getParent(w.getElement());
148: }
149: }
|