KickJava   Java API By Example, From Geeks To Geeks.

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


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.Event;
20
21 /**
22  * A horizontal bar of folder-style tabs, most commonly used as part of a
23  * {@link com.google.gwt.user.client.ui.TabPanel}.
24  * <p>
25  * <img class='gallery' SRC='TabBar.png'/>
26  * </p>
27  * <h3>CSS Style Rules</h3>
28  * <ul class='css'>
29  * <li>.gwt-TabBar { the tab bar itself }</li>
30  * <li>.gwt-TabBar .gwt-TabBarFirst { the left edge of the bar }</li>
31  * <li>.gwt-TabBar .gwt-TabBarRest { the right edge of the bar }</li>
32  * <li>.gwt-TabBar .gwt-TabBarItem { unselected tabs }</li>
33  * <li>.gwt-TabBar .gwt-TabBarItem-selected { additional style for selected
34  * tabs } </li>
35  * </ul>
36  * <p>
37  * <h3>Example</h3>
38  * {@example com.google.gwt.examples.TabBarExample}
39  * </p>
40  */

41 public class TabBar extends Composite implements SourcesTabEvents,
42     ClickListener {
43
44   /**
45    * <code>ClickDecoratorPanel</code> decorates any widget with the minimal
46    * amount of machinery to receive clicks for delegation to the parent.
47    * {@link SourcesClickEvents} is not implemented due to the fact that only a
48    * single observer is needed.
49    */

50   private static final class ClickDecoratorPanel extends SimplePanel {
51     ClickListener delegate;
52
53     ClickDecoratorPanel(Widget child, ClickListener delegate) {
54       this.delegate = delegate;
55       setWidget(child);
56       sinkEvents(Event.ONCLICK);
57     }
58
59     public void onBrowserEvent(Event event) {
60       // No need for call to super.
61
switch (DOM.eventGetType(event)) {
62         case Event.ONCLICK:
63           delegate.onClick(this);
64       }
65     }
66   }
67
68   private static final String JavaDoc STYLENAME_DEFAULT = "gwt-TabBarItem";
69   private HorizontalPanel panel = new HorizontalPanel();
70   private Widget selectedTab;
71   private TabListenerCollection tabListeners;
72
73   /**
74    * Creates an empty tab bar.
75    */

76   public TabBar() {
77     initWidget(panel);
78     sinkEvents(Event.ONCLICK);
79     setStyleName("gwt-TabBar");
80
81     panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
82
83     HTML first = new HTML("&nbsp;", true), rest = new HTML("&nbsp;", true);
84     first.setStyleName("gwt-TabBarFirst");
85     rest.setStyleName("gwt-TabBarRest");
86     first.setHeight("100%");
87     rest.setHeight("100%");
88
89     panel.add(first);
90     panel.add(rest);
91     first.setHeight("100%");
92     panel.setCellHeight(first, "100%");
93     panel.setCellWidth(rest, "100%");
94   }
95
96   /**
97    * Adds a new tab with the specified text.
98    *
99    * @param text the new tab's text
100    */

101   public void addTab(String JavaDoc text) {
102     insertTab(text, getTabCount());
103   }
104
105   /**
106    * Adds a new tab with the specified text.
107    *
108    * @param text the new tab's text
109    * @param asHTML <code>true</code> to treat the specified text as html
110    */

111   public void addTab(String JavaDoc text, boolean asHTML) {
112     insertTab(text, asHTML, getTabCount());
113   }
114
115   /**
116    * Adds a new tab with the specified widget.
117    *
118    * @param widget the new tab's widget.
119    */

120   public void addTab(Widget widget) {
121     insertTab(widget, getTabCount());
122   }
123
124   public void addTabListener(TabListener listener) {
125     if (tabListeners == null) {
126       tabListeners = new TabListenerCollection();
127     }
128     tabListeners.add(listener);
129   }
130
131   /**
132    * Gets the tab that is currently selected.
133    *
134    * @return the selected tab
135    */

136   public int getSelectedTab() {
137     if (selectedTab == null) {
138       return -1;
139     }
140     return panel.getWidgetIndex(selectedTab) - 1;
141   }
142
143   /**
144    * Gets the number of tabs present.
145    *
146    * @return the tab count
147    */

148   public int getTabCount() {
149     return panel.getWidgetCount() - 2;
150   }
151
152   /**
153    * Gets the specified tab's HTML.
154    *
155    * @param index the index of the tab whose HTML is to be retrieved
156    * @return the tab's HTML
157    */

158   public String JavaDoc getTabHTML(int index) {
159     if (index >= getTabCount()) {
160       return null;
161     }
162     Widget widget = panel.getWidget(index + 1);
163     if (widget instanceof HTML) {
164       return ((HTML) widget).getHTML();
165     } else if (widget instanceof Label) {
166       return ((Label) widget).getText();
167     } else {
168       // This will be a ClickDecorator holding a user-supplied widget.
169
return DOM.getInnerHTML(widget.getElement());
170     }
171   }
172
173   /**
174    * Inserts a new tab at the specified index.
175    *
176    * @param text the new tab's text
177    * @param asHTML <code>true</code> to treat the specified text as HTML
178    * @param beforeIndex the index before which this tab will be inserted
179    */

180   public void insertTab(String JavaDoc text, boolean asHTML, int beforeIndex) {
181     checkInsertBeforeTabIndex(beforeIndex);
182
183     Label item;
184     if (asHTML) {
185       item = new HTML(text);
186     } else {
187       item = new Label(text);
188     }
189
190     item.setWordWrap(false);
191     item.addClickListener(this);
192     item.setStyleName(STYLENAME_DEFAULT);
193     panel.insert(item, beforeIndex + 1);
194   }
195
196   /**
197    * Inserts a new tab at the specified index.
198    *
199    * @param text the new tab's text
200    * @param beforeIndex the index before which this tab will be inserted
201    */

202   public void insertTab(String JavaDoc text, int beforeIndex) {
203     insertTab(text, false, beforeIndex);
204   }
205
206   /**
207    * Inserts a new tab at the specified index.
208    *
209    * @param widget widget to be used in the new tab.
210    * @param beforeIndex the index before which this tab will be inserted.
211    */

212   public void insertTab(Widget widget, int beforeIndex) {
213     checkInsertBeforeTabIndex(beforeIndex);
214
215     ClickDecoratorPanel decWidget = new ClickDecoratorPanel(widget, this);
216     decWidget.addStyleName(STYLENAME_DEFAULT);
217     panel.insert(decWidget, beforeIndex + 1);
218   }
219
220   public void onClick(Widget sender) {
221     for (int i = 1; i < panel.getWidgetCount() - 1; ++i) {
222       if (panel.getWidget(i) == sender) {
223         selectTab(i - 1);
224         return;
225       }
226     }
227   }
228
229   /**
230    * Removes the tab at the specified index.
231    *
232    * @param index the index of the tab to be removed
233    */

234   public void removeTab(int index) {
235     checkTabIndex(index);
236
237     // (index + 1) to account for 'first' placeholder widget.
238
Widget toRemove = panel.getWidget(index + 1);
239     if (toRemove == selectedTab) {
240       selectedTab = null;
241     }
242     panel.remove(toRemove);
243   }
244
245   public void removeTabListener(TabListener listener) {
246     if (tabListeners != null) {
247       tabListeners.remove(listener);
248     }
249   }
250
251   /**
252    * Programmatically selects the specified tab. Use index -1 to specify that no
253    * tab should be selected.
254    *
255    * @param index the index of the tab to be selected.
256    * @return <code>true</code> if successful, <code>false</code> if the
257    * change is denied by the {@link TabListener}.
258    */

259   public boolean selectTab(int index) {
260     checkTabIndex(index);
261
262     if (tabListeners != null) {
263       if (!tabListeners.fireBeforeTabSelected(this, index)) {
264         return false;
265       }
266     }
267
268     // Check for -1.
269
setSelectionStyle(selectedTab, false);
270     if (index == -1) {
271       selectedTab = null;
272       return true;
273     }
274
275     selectedTab = panel.getWidget(index + 1);
276     setSelectionStyle(selectedTab, true);
277
278     if (tabListeners != null) {
279       tabListeners.fireTabSelected(this, index);
280     }
281     return true;
282   }
283
284   private void checkInsertBeforeTabIndex(int beforeIndex) {
285     if ((beforeIndex < 0) || (beforeIndex > getTabCount())) {
286       throw new IndexOutOfBoundsException JavaDoc();
287     }
288   }
289
290   private void checkTabIndex(int index) {
291     if ((index < -1) || (index >= getTabCount())) {
292       throw new IndexOutOfBoundsException JavaDoc();
293     }
294   }
295
296   private void setSelectionStyle(Widget item, boolean selected) {
297     if (item != null) {
298       if (selected) {
299         item.addStyleName("gwt-TabBarItem-selected");
300       } else {
301         item.removeStyleName("gwt-TabBarItem-selected");
302       }
303     }
304   }
305 }
306
Popular Tags