1 16 package com.google.gwt.user.client.ui; 17 18 import java.util.Iterator ; 19 20 48 public class TabPanel extends Composite implements TabListener, 49 SourcesTabEvents, HasWidgets, IndexedPanel { 50 51 63 private static class TabbedDeckPanel extends DeckPanel { 64 private UnmodifiableTabBar tabBar; 65 66 public TabbedDeckPanel(UnmodifiableTabBar tabBar) { 67 this.tabBar = tabBar; 68 } 69 70 public void clear() { 71 throw new UnsupportedOperationException ( 72 "Use TabPanel.clear() to alter the DeckPanel"); 73 } 74 75 public void insert(Widget w, int beforeIndex) { 76 throw new UnsupportedOperationException ( 77 "Use TabPanel.insert() to alter the DeckPanel"); 78 } 79 80 public boolean remove(Widget w) { 81 int idx = getWidgetIndex(w); 84 if (idx != -1) { 85 tabBar.removeTabProtected(idx); 86 return super.remove(w); 87 } 88 89 return false; 90 } 91 92 protected void insertProtected(Widget w, String tabText, boolean asHTML, 93 int beforeIndex) { 94 95 int idx = getWidgetIndex(w); 98 if (idx != -1) { 99 remove(w); 100 if (idx < beforeIndex) { 101 beforeIndex--; 102 } 103 } 104 105 tabBar.insertTabProtected(tabText, asHTML, beforeIndex); 106 super.insert(w, beforeIndex); 107 } 108 109 protected void insertProtected(Widget w, Widget tabWidget, 110 int beforeIndex) { 111 112 int idx = getWidgetIndex(w); 115 if (idx != -1) { 116 remove(w); 117 if (idx < beforeIndex) { 118 beforeIndex--; 119 } 120 } 121 122 tabBar.insertTabProtected(tabWidget, beforeIndex); 123 super.insert(w, beforeIndex); 124 } 125 }; 126 127 131 private static class UnmodifiableTabBar extends TabBar { 132 public void insertTab(String text, boolean asHTML, int beforeIndex) { 133 throw new UnsupportedOperationException ( 134 "Use TabPanel.insert() to alter the TabBar"); 135 } 136 137 public void insertTab(Widget widget, int beforeIndex) { 138 throw new UnsupportedOperationException ( 139 "Use TabPanel.insert() to alter the TabBar"); 140 } 141 142 public void removeTab(int index) { 143 throw new UnsupportedOperationException ( 147 "Use TabPanel.remove() to alter the TabBar"); 148 } 149 150 protected void insertTabProtected(String text, boolean asHTML, 151 int beforeIndex) { 152 super.insertTab(text, asHTML, beforeIndex); 153 } 154 155 protected void insertTabProtected(Widget widget, int beforeIndex) { 156 super.insertTab(widget, beforeIndex); 157 } 158 159 protected void removeTabProtected(int index) { 160 super.removeTab(index); 161 } 162 } 163 164 private UnmodifiableTabBar tabBar = new UnmodifiableTabBar(); 165 private TabbedDeckPanel deck = new TabbedDeckPanel(tabBar); 166 private TabListenerCollection tabListeners; 167 168 171 public TabPanel() { 172 VerticalPanel panel = new VerticalPanel(); 173 panel.add(tabBar); 174 panel.add(deck); 175 176 panel.setCellHeight(deck, "100%"); 177 tabBar.setWidth("100%"); 178 179 tabBar.addTabListener(this); 180 initWidget(panel); 181 setStyleName("gwt-TabPanel"); 182 deck.setStyleName("gwt-TabPanelBottom"); 183 } 184 185 public void add(Widget w) { 186 throw new UnsupportedOperationException ( 187 "A tabText parameter must be specified with add()."); 188 } 189 190 197 public void add(Widget w, String tabText) { 198 insert(w, tabText, getWidgetCount()); 199 } 200 201 209 public void add(Widget w, String tabText, boolean asHTML) { 210 insert(w, tabText, asHTML, getWidgetCount()); 211 } 212 213 220 public void add(Widget w, Widget tabWidget) { 221 insert(w, tabWidget, getWidgetCount()); 222 } 223 224 public void addTabListener(TabListener listener) { 225 if (tabListeners == null) { 226 tabListeners = new TabListenerCollection(); 227 } 228 tabListeners.add(listener); 229 } 230 231 public void clear() { 232 while (getWidgetCount() > 0) { 233 remove(getWidget(0)); 234 } 235 } 236 237 244 public DeckPanel getDeckPanel() { 245 return deck; 246 } 247 248 255 public TabBar getTabBar() { 256 return tabBar; 257 } 258 259 public Widget getWidget(int index) { 260 return deck.getWidget(index); 261 } 262 263 public int getWidgetCount() { 264 return deck.getWidgetCount(); 265 } 266 267 public int getWidgetIndex(Widget widget) { 268 return deck.getWidgetIndex(widget); 269 } 270 271 280 public void insert(Widget widget, String tabText, boolean asHTML, 281 int beforeIndex) { 282 deck.insertProtected(widget, tabText, asHTML, beforeIndex); 284 } 285 286 294 public void insert(Widget widget, Widget tabWidget, int beforeIndex) { 295 deck.insertProtected(widget, tabWidget, beforeIndex); 297 } 298 299 307 public void insert(Widget widget, String tabText, int beforeIndex) { 308 insert(widget, tabText, false, beforeIndex); 309 } 310 311 public Iterator iterator() { 312 return deck.iterator(); 315 } 316 317 public boolean onBeforeTabSelected(SourcesTabEvents sender, int tabIndex) { 318 if (tabListeners != null) { 319 return tabListeners.fireBeforeTabSelected(this, tabIndex); 320 } 321 return true; 322 } 323 324 public void onTabSelected(SourcesTabEvents sender, int tabIndex) { 325 deck.showWidget(tabIndex); 326 if (tabListeners != null) { 327 tabListeners.fireTabSelected(this, tabIndex); 328 } 329 } 330 331 public boolean remove(int index) { 332 return deck.remove(index); 334 } 335 336 341 public boolean remove(Widget widget) { 342 return deck.remove(widget); 344 } 345 346 public void removeTabListener(TabListener listener) { 347 if (tabListeners != null) { 348 tabListeners.remove(listener); 349 } 350 } 351 352 357 public void selectTab(int index) { 358 tabBar.selectTab(index); 359 } 360 } 361 | Popular Tags |