001 /*
002 * Copyright 2006 Mat Gessel <mat.gessel@gmail.com>
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 asquare.gwt.tk.client.ui;
017
018 import asquare.gwt.tk.client.util.DomUtil;
019 import asquare.gwt.tk.client.util.GwtUtil;
020
021 import com.google.gwt.user.client.DOM;
022 import com.google.gwt.user.client.Element;
023 import com.google.gwt.user.client.ui.IndexedPanel;
024 import com.google.gwt.user.client.ui.Widget;
025
026 /**
027 * A barebones {@link com.google.gwt.user.client.ui.ComplexPanel ComplexPanel}
028 * which allows specification of the root element and <code>display</code> style of
029 * added children.
030 *
031 * @see <a
032 * href="http://www.w3.org/TR/CSS21/visuren.html#inline-formatting">Inline
033 * formatting context</a>
034 */
035 public class BasicPanel extends CComplexPanel implements IndexedPanel
036 {
037 /**
038 * The default element (<code>DIV</code>), if no element is specified.
039 */
040 public static final String ELEMENT_DEFAULT = "div";
041
042 /**
043 * Do not specify a display style for added children.
044 */
045 public static final String DISPLAY_DEFAULT = null;
046
047 /**
048 * Specifies <code>display:block</code> style for added children.
049 */
050 public static final String DISPLAY_BLOCK = "block";
051
052 /**
053 * Specifies <code>display:inline</code> style for added children.
054 */
055 public static final String DISPLAY_INLINE = "inline";
056
057 /**
058 * Specifies <code>display:inherit</code> style for added children.
059 * <p><em>By default the display style is not inherited.</em></p>
060 */
061 public static final String DISPLAY_INHERIT = "inherit";
062
063 private String m_childrenDisplay = null;
064
065 /**
066 * Constructs a panel based on a <code>DIV</code> element and default children
067 * display property.
068 */
069 public BasicPanel()
070 {
071 this(ELEMENT_DEFAULT, DISPLAY_DEFAULT);
072 }
073
074 /**
075 * Constructs a panel based on the specified element and the default display
076 * style.
077 *
078 * @param element a DOM element
079 */
080 public BasicPanel(Element element)
081 {
082 this(element, DISPLAY_DEFAULT);
083 }
084
085 /**
086 * Constructs a panel based on the specified element and the default display
087 * style.
088 *
089 * @param element a html element tag
090 */
091 public BasicPanel(String element)
092 {
093 this(element, DISPLAY_DEFAULT);
094 }
095
096 /**
097 * Constructs a panel based on the specified element and display style.
098 *
099 * @param element a html element tag
100 * @param childrenDisplay a css <code>display</code> style value to apply
101 * to added children
102 */
103 public BasicPanel(String element, String childrenDisplay)
104 {
105 this(DOM.createElement(element), childrenDisplay);
106 }
107
108 /**
109 * Constructs a panel based on the specified element and display style.
110 *
111 * @param element a DOM element
112 * @param childrenDisplay a css <code>display</code> style value to apply
113 * to added children
114 */
115 public BasicPanel(Element element, String childrenDisplay)
116 {
117 super(element);
118 m_childrenDisplay = childrenDisplay;
119 }
120
121 /**
122 * Gets the display style value which will be applied to added children.
123 *
124 * @return a css <code>display</code> style value or null if none specified
125 */
126 public String getChildrenDisplay()
127 {
128 return m_childrenDisplay;
129 }
130
131 /**
132 * Sets the display style value which will be applied to added children.
133 *
134 * @param childrenDisplay a css <code>display</code> style value to apply
135 * to added children
136 */
137 public void setChildrenDisplay(String childrenDisplay)
138 {
139 m_childrenDisplay = childrenDisplay;
140 }
141
142 /*
143 * (non-Javadoc)
144 * @see com.google.gwt.user.client.ui.IndexedPanel#getWidget(int)
145 */
146 public Widget getWidget(int index)
147 {
148 return getChildren().get(index);
149 }
150
151 /*
152 * (non-Javadoc)
153 * @see com.google.gwt.user.client.ui.IndexedPanel#getWidgetIndex(com.google.gwt.user.client.ui.Widget)
154 */
155 public int getWidgetIndex(Widget child)
156 {
157 return getChildren().indexOf(child);
158 }
159
160 /**
161 * Adds a widget to this panel. If a display style has been specified it will
162 * be applied to the child.
163 */
164 public void add(Widget w)
165 {
166 insert(w, getWidgetCount());
167 }
168
169 /**
170 * Inserts a widget at the specified index.
171 *
172 * @param w a widget
173 * @param beforeIndex the index before which <code>w</code> will be
174 * inserted
175 * @throws IndexOutOfBoundsException if <code>beforeIndex</code> is less
176 * than 0 or greater than the current widget count
177 */
178 public void insert(Widget w, int beforeIndex)
179 {
180 GwtUtil.rangeCheck(0, getChildren().size(), beforeIndex, true);
181
182 if (m_childrenDisplay != null)
183 {
184 DomUtil.setStyleAttribute(w, "display", m_childrenDisplay);
185 }
186 // pass null and insert manually so we can control DOM element order
187 insert(w, null, beforeIndex);
188 DOM.insertChild(getElement(), w.getElement(), beforeIndex);
189 }
190
191 /*
192 * (non-Javadoc)
193 * @see com.google.gwt.user.client.ui.IndexedPanel#remove(int)
194 */
195 public boolean remove(int index)
196 {
197 if (index < 0 || index >= getWidgetCount())
198 return false;
199
200 return remove(getWidget(index));
201 }
202
203 /**
204 * Get the number of widgets in this panel.
205 */
206 public int getWidgetCount()
207 {
208 return getChildren().size();
209 }
210
211 /**
212 * Sets a unique id for referencing this specific panel.
213 *
214 * @param id a unique id
215 * @see DomUtil#setId(com.google.gwt.user.client.ui.UIObject, String)
216 */
217 public void setId(String id)
218 {
219 DomUtil.setId(this, id);
220 }
221 }