KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > CellPanel


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client.ui;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.Element;
20 import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
21 import com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant;
22
23 /**
24  * A panel whose child widgets are contained within the cells of a table. Each
25  * cell's size may be set independently. Each child widget can take up a subset
26  * of its cell and can be aligned within it.
27  */

28 public abstract class CellPanel extends ComplexPanel {
29
30   private int spacing;
31   private Element table, body;
32
33   public CellPanel() {
34     table = DOM.createTable();
35     body = DOM.createTBody();
36     DOM.appendChild(table, body);
37     setElement(table);
38   }
39
40   /**
41    * Gets the amount of spacing between this panel's cells.
42    *
43    * @return the inter-cell spacing, in pixels
44    */

45   public int getSpacing() {
46     return spacing;
47   }
48
49   /**
50    * Sets the width of the border to be applied to all cells in this panel. This
51    * is particularly useful when debugging layouts, in that it allows you to see
52    * explicitly the cells that contain this panel's children.
53    *
54    * @param width the width of the panel's cell borders, in pixels
55    */

56   public void setBorderWidth(int width) {
57     DOM.setElementProperty(table, "border", "" + width);
58   }
59
60   /**
61    * Sets the height of the cell associated with the given widget, related to
62    * the panel as a whole.
63    *
64    * @param w the widget whose cell height is to be set
65    * @param height the cell's height, in CSS units
66    */

67   public void setCellHeight(Widget w, String JavaDoc height) {
68     Element td = DOM.getParent(w.getElement());
69     DOM.setElementProperty(td, "height", height);
70   }
71
72   /**
73    * Sets the horizontal alignment of the given widget within its cell.
74    *
75    * @param w the widget whose horizontal alignment is to be set
76    * @param align the widget's horizontal alignment, as defined in
77    * {@link HasHorizontalAlignment}.
78    */

79   public void setCellHorizontalAlignment(Widget w,
80       HorizontalAlignmentConstant align) {
81     Element td = getWidgetTd(w);
82     if (td != null) {
83       DOM.setElementProperty(td, "align", align.getTextAlignString());
84     }
85   }
86
87   /**
88    * Sets the vertical alignment of the given widget within its cell.
89    *
90    * @param w the widget whose vertical alignment is to be set
91    * @param align the widget's vertical alignment, as defined in
92    * {@link HasVerticalAlignment}.
93    */

94   public void setCellVerticalAlignment(Widget w, VerticalAlignmentConstant align) {
95     Element td = getWidgetTd(w);
96     if (td != null) {
97       DOM.setStyleAttribute(td, "verticalAlign", align.getVerticalAlignString());
98     }
99   }
100
101   /**
102    * Sets the width of the cell associated with the given widget, related to the
103    * panel as a whole.
104    *
105    * @param w the widget whose cell width is to be set
106    * @param width the cell's width, in CSS units
107    */

108   public void setCellWidth(Widget w, String JavaDoc width) {
109     Element td = DOM.getParent(w.getElement());
110     DOM.setElementProperty(td, "width", width);
111   }
112
113   /**
114    * Sets the amount of spacing between this panel's cells.
115    *
116    * @param spacing the inter-cell spacing, in pixels
117    */

118   public void setSpacing(int spacing) {
119     this.spacing = spacing;
120     DOM.setElementPropertyInt(table, "cellSpacing", spacing);
121   }
122
123   protected Element getBody() {
124     return body;
125   }
126
127   protected Element getTable() {
128     return table;
129   }
130
131   private Element getWidgetTd(Widget w) {
132     if (w.getParent() != this) {
133       return null;
134     }
135     return DOM.getParent(w.getElement());
136   }
137 }
138
Popular Tags