You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VTabsheet.java 48KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  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.vaadin.client.ui;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import com.google.gwt.aria.client.Id;
  20. import com.google.gwt.aria.client.LiveValue;
  21. import com.google.gwt.aria.client.Roles;
  22. import com.google.gwt.aria.client.SelectedValue;
  23. import com.google.gwt.core.client.Scheduler;
  24. import com.google.gwt.dom.client.DivElement;
  25. import com.google.gwt.dom.client.Element;
  26. import com.google.gwt.dom.client.Style;
  27. import com.google.gwt.dom.client.Style.Display;
  28. import com.google.gwt.dom.client.Style.Overflow;
  29. import com.google.gwt.dom.client.Style.Unit;
  30. import com.google.gwt.dom.client.Style.Visibility;
  31. import com.google.gwt.dom.client.TableCellElement;
  32. import com.google.gwt.dom.client.TableElement;
  33. import com.google.gwt.event.dom.client.BlurEvent;
  34. import com.google.gwt.event.dom.client.BlurHandler;
  35. import com.google.gwt.event.dom.client.ClickEvent;
  36. import com.google.gwt.event.dom.client.ClickHandler;
  37. import com.google.gwt.event.dom.client.FocusEvent;
  38. import com.google.gwt.event.dom.client.FocusHandler;
  39. import com.google.gwt.event.dom.client.HasBlurHandlers;
  40. import com.google.gwt.event.dom.client.HasFocusHandlers;
  41. import com.google.gwt.event.dom.client.HasKeyDownHandlers;
  42. import com.google.gwt.event.dom.client.KeyCodes;
  43. import com.google.gwt.event.dom.client.KeyDownEvent;
  44. import com.google.gwt.event.dom.client.KeyDownHandler;
  45. import com.google.gwt.event.shared.HandlerRegistration;
  46. import com.google.gwt.regexp.shared.MatchResult;
  47. import com.google.gwt.regexp.shared.RegExp;
  48. import com.google.gwt.user.client.Command;
  49. import com.google.gwt.user.client.DOM;
  50. import com.google.gwt.user.client.Event;
  51. import com.google.gwt.user.client.ui.ComplexPanel;
  52. import com.google.gwt.user.client.ui.SimplePanel;
  53. import com.google.gwt.user.client.ui.Widget;
  54. import com.google.gwt.user.client.ui.impl.FocusImpl;
  55. import com.vaadin.client.ApplicationConnection;
  56. import com.vaadin.client.BrowserInfo;
  57. import com.vaadin.client.ComponentConnector;
  58. import com.vaadin.client.Focusable;
  59. import com.vaadin.client.TooltipInfo;
  60. import com.vaadin.client.Util;
  61. import com.vaadin.client.VCaption;
  62. import com.vaadin.client.VTooltip;
  63. import com.vaadin.client.ui.aria.AriaHelper;
  64. import com.vaadin.shared.AbstractComponentState;
  65. import com.vaadin.shared.ComponentConstants;
  66. import com.vaadin.shared.EventId;
  67. import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc;
  68. import com.vaadin.shared.ui.ComponentStateUtil;
  69. import com.vaadin.shared.ui.tabsheet.TabState;
  70. import com.vaadin.shared.ui.tabsheet.TabsheetServerRpc;
  71. import com.vaadin.shared.ui.tabsheet.TabsheetState;
  72. public class VTabsheet extends VTabsheetBase implements Focusable,
  73. FocusHandler, BlurHandler, KeyDownHandler, SubPartAware {
  74. private static class VCloseEvent {
  75. private Tab tab;
  76. VCloseEvent(Tab tab) {
  77. this.tab = tab;
  78. }
  79. public Tab getTab() {
  80. return tab;
  81. }
  82. }
  83. private interface VCloseHandler {
  84. public void onClose(VCloseEvent event);
  85. }
  86. /**
  87. * Representation of a single "tab" shown in the TabBar
  88. *
  89. */
  90. public static class Tab extends SimplePanel implements HasFocusHandlers,
  91. HasBlurHandlers, HasKeyDownHandlers {
  92. private static final String TD_CLASSNAME = CLASSNAME + "-tabitemcell";
  93. private static final String TD_FIRST_CLASSNAME = TD_CLASSNAME
  94. + "-first";
  95. private static final String TD_SELECTED_CLASSNAME = TD_CLASSNAME
  96. + "-selected";
  97. private static final String TD_SELECTED_FIRST_CLASSNAME = TD_SELECTED_CLASSNAME
  98. + "-first";
  99. private static final String TD_FOCUS_CLASSNAME = TD_CLASSNAME
  100. + "-focus";
  101. private static final String TD_FOCUS_FIRST_CLASSNAME = TD_FOCUS_CLASSNAME
  102. + "-first";
  103. private static final String TD_DISABLED_CLASSNAME = TD_CLASSNAME
  104. + "-disabled";
  105. private static final String DIV_CLASSNAME = CLASSNAME + "-tabitem";
  106. private static final String DIV_SELECTED_CLASSNAME = DIV_CLASSNAME
  107. + "-selected";
  108. private static final String DIV_FOCUS_CLASSNAME = DIV_CLASSNAME
  109. + "-focus";
  110. private TabCaption tabCaption;
  111. Element td = getElement();
  112. private VCloseHandler closeHandler;
  113. private boolean enabledOnServer = true;
  114. private Element div;
  115. private TabBar tabBar;
  116. private boolean hiddenOnServer = false;
  117. private String styleName;
  118. private String id;
  119. private Tab(TabBar tabBar) {
  120. super(DOM.createTD());
  121. this.tabBar = tabBar;
  122. setStyleName(td, TD_CLASSNAME);
  123. Roles.getTabRole().set(getElement());
  124. Roles.getTabRole().setAriaSelectedState(getElement(),
  125. SelectedValue.FALSE);
  126. div = DOM.createDiv();
  127. setTabulatorIndex(-1);
  128. setStyleName(div, DIV_CLASSNAME);
  129. DOM.appendChild(td, div);
  130. tabCaption = new TabCaption(this);
  131. add(tabCaption);
  132. Roles.getTabRole().setAriaLabelledbyProperty(getElement(),
  133. Id.of(tabCaption.getElement()));
  134. addFocusHandler(getTabsheet());
  135. addBlurHandler(getTabsheet());
  136. addKeyDownHandler(getTabsheet());
  137. }
  138. public boolean isHiddenOnServer() {
  139. return hiddenOnServer;
  140. }
  141. public void setHiddenOnServer(boolean hiddenOnServer) {
  142. this.hiddenOnServer = hiddenOnServer;
  143. Roles.getTabRole().setAriaHiddenState(getElement(), hiddenOnServer);
  144. }
  145. @Override
  146. protected com.google.gwt.user.client.Element getContainerElement() {
  147. // Attach caption element to div, not td
  148. return DOM.asOld(div);
  149. }
  150. public boolean isEnabledOnServer() {
  151. return enabledOnServer;
  152. }
  153. public void setEnabledOnServer(boolean enabled) {
  154. enabledOnServer = enabled;
  155. Roles.getTabRole().setAriaDisabledState(getElement(), !enabled);
  156. setStyleName(td, TD_DISABLED_CLASSNAME, !enabled);
  157. if (!enabled) {
  158. focusImpl.setTabIndex(td, -1);
  159. }
  160. }
  161. public void addClickHandler(ClickHandler handler) {
  162. tabCaption.addClickHandler(handler);
  163. }
  164. public void setCloseHandler(VCloseHandler closeHandler) {
  165. this.closeHandler = closeHandler;
  166. }
  167. /**
  168. * Toggles the style names for the Tab
  169. *
  170. * @param selected
  171. * true if the Tab is selected
  172. * @param first
  173. * true if the Tab is the first visible Tab
  174. */
  175. public void setStyleNames(boolean selected, boolean first) {
  176. setStyleNames(selected, first, false);
  177. }
  178. public void setStyleNames(boolean selected, boolean first,
  179. boolean keyboardFocus) {
  180. setStyleName(td, TD_FIRST_CLASSNAME, first);
  181. setStyleName(td, TD_SELECTED_CLASSNAME, selected);
  182. setStyleName(td, TD_SELECTED_FIRST_CLASSNAME, selected && first);
  183. setStyleName(div, DIV_SELECTED_CLASSNAME, selected);
  184. setStyleName(td, TD_FOCUS_CLASSNAME, keyboardFocus);
  185. setStyleName(td, TD_FOCUS_FIRST_CLASSNAME, keyboardFocus && first);
  186. setStyleName(div, DIV_FOCUS_CLASSNAME, keyboardFocus);
  187. }
  188. public void setTabulatorIndex(int tabIndex) {
  189. getElement().setTabIndex(tabIndex);
  190. }
  191. public boolean isClosable() {
  192. return tabCaption.isClosable();
  193. }
  194. public void onClose() {
  195. closeHandler.onClose(new VCloseEvent(this));
  196. }
  197. public VTabsheet getTabsheet() {
  198. return tabBar.getTabsheet();
  199. }
  200. private void updateFromState(TabState tabState) {
  201. tabCaption.update(tabState);
  202. // Apply the styleName set for the tab
  203. String newStyleName = tabState.styleName;
  204. // Find the nth td element
  205. if (newStyleName != null && !newStyleName.isEmpty()) {
  206. if (!newStyleName.equals(styleName)) {
  207. // If we have a new style name
  208. if (styleName != null && !styleName.isEmpty()) {
  209. // Remove old style name if present
  210. td.removeClassName(TD_CLASSNAME + "-" + styleName);
  211. }
  212. // Set new style name
  213. td.addClassName(TD_CLASSNAME + "-" + newStyleName);
  214. styleName = newStyleName;
  215. }
  216. } else if (styleName != null) {
  217. // Remove the set stylename if no stylename is present in the
  218. // uidl
  219. td.removeClassName(TD_CLASSNAME + "-" + styleName);
  220. styleName = null;
  221. }
  222. String newId = tabState.id;
  223. if (newId != null && !newId.isEmpty()) {
  224. td.setId(newId);
  225. id = newId;
  226. } else if (id != null) {
  227. td.removeAttribute("id");
  228. id = null;
  229. }
  230. }
  231. public void recalculateCaptionWidth() {
  232. tabCaption.setWidth(tabCaption.getRequiredWidth() + "px");
  233. }
  234. @Override
  235. public HandlerRegistration addFocusHandler(FocusHandler handler) {
  236. return addDomHandler(handler, FocusEvent.getType());
  237. }
  238. @Override
  239. public HandlerRegistration addBlurHandler(BlurHandler handler) {
  240. return addDomHandler(handler, BlurEvent.getType());
  241. }
  242. @Override
  243. public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
  244. return addDomHandler(handler, KeyDownEvent.getType());
  245. }
  246. public void focus() {
  247. getTabsheet().scrollIntoView(this);
  248. focusImpl.focus(td);
  249. }
  250. public void blur() {
  251. focusImpl.blur(td);
  252. }
  253. public boolean hasTooltip() {
  254. return tabCaption.getTooltipInfo() != null;
  255. }
  256. public TooltipInfo getTooltipInfo() {
  257. return tabCaption.getTooltipInfo();
  258. }
  259. public void setAssistiveDescription(String descriptionId) {
  260. Roles.getTablistRole().setAriaDescribedbyProperty(getElement(),
  261. Id.of(descriptionId));
  262. }
  263. public void removeAssistiveDescription() {
  264. Roles.getTablistRole().removeAriaDescribedbyProperty(getElement());
  265. }
  266. }
  267. public static class TabCaption extends VCaption {
  268. private boolean closable = false;
  269. private Element closeButton;
  270. private Tab tab;
  271. TabCaption(Tab tab) {
  272. super(tab.getTabsheet().connector.getConnection());
  273. this.tab = tab;
  274. AriaHelper.ensureHasId(getElement());
  275. }
  276. private boolean update(TabState tabState) {
  277. if (tabState.description != null || tabState.componentError != null) {
  278. setTooltipInfo(new TooltipInfo(tabState.description,
  279. tabState.componentError, this));
  280. } else {
  281. setTooltipInfo(null);
  282. }
  283. // TODO need to call this instead of super because the caption does
  284. // not have an owner
  285. String captionString = tabState.caption.isEmpty() ? null
  286. : tabState.caption;
  287. boolean ret = updateCaptionWithoutOwner(captionString,
  288. !tabState.enabled, hasAttribute(tabState.description),
  289. hasAttribute(tabState.componentError),
  290. tab.getTabsheet().connector
  291. .getResourceUrl(ComponentConstants.ICON_RESOURCE
  292. + tabState.key), tabState.iconAltText);
  293. setClosable(tabState.closable);
  294. return ret;
  295. }
  296. private boolean hasAttribute(String string) {
  297. return string != null && !string.trim().isEmpty();
  298. }
  299. private VTabsheet getTabsheet() {
  300. return tab.getTabsheet();
  301. }
  302. @Override
  303. public void onBrowserEvent(Event event) {
  304. if (closable && event.getTypeInt() == Event.ONCLICK
  305. && event.getEventTarget().cast() == closeButton) {
  306. tab.onClose();
  307. event.stopPropagation();
  308. event.preventDefault();
  309. }
  310. super.onBrowserEvent(event);
  311. if (event.getTypeInt() == Event.ONLOAD) {
  312. getTabsheet().tabSizeMightHaveChanged(getTab());
  313. }
  314. }
  315. public Tab getTab() {
  316. return tab;
  317. }
  318. public void setClosable(boolean closable) {
  319. this.closable = closable;
  320. if (closable && closeButton == null) {
  321. closeButton = DOM.createSpan();
  322. closeButton.setInnerHTML("×");
  323. closeButton
  324. .setClassName(VTabsheet.CLASSNAME + "-caption-close");
  325. Roles.getTabRole().setAriaHiddenState(closeButton, true);
  326. Roles.getTabRole().setAriaDisabledState(closeButton, true);
  327. getElement().appendChild(closeButton);
  328. } else if (!closable && closeButton != null) {
  329. getElement().removeChild(closeButton);
  330. closeButton = null;
  331. }
  332. if (closable) {
  333. addStyleDependentName("closable");
  334. } else {
  335. removeStyleDependentName("closable");
  336. }
  337. }
  338. public boolean isClosable() {
  339. return closable;
  340. }
  341. @Override
  342. public int getRequiredWidth() {
  343. int width = super.getRequiredWidth();
  344. if (closeButton != null) {
  345. width += Util.getRequiredWidth(closeButton);
  346. }
  347. return width;
  348. }
  349. public com.google.gwt.user.client.Element getCloseButton() {
  350. return DOM.asOld(closeButton);
  351. }
  352. }
  353. static class TabBar extends ComplexPanel implements ClickHandler,
  354. VCloseHandler {
  355. private final Element tr = DOM.createTR();
  356. private final Element spacerTd = DOM.createTD();
  357. private Tab selected;
  358. private VTabsheet tabsheet;
  359. TabBar(VTabsheet tabsheet) {
  360. this.tabsheet = tabsheet;
  361. Element el = DOM.createTable();
  362. Roles.getPresentationRole().set(el);
  363. Element tbody = DOM.createTBody();
  364. DOM.appendChild(el, tbody);
  365. DOM.appendChild(tbody, tr);
  366. setStyleName(spacerTd, CLASSNAME + "-spacertd");
  367. DOM.appendChild(tr, spacerTd);
  368. DOM.appendChild(spacerTd, DOM.createDiv());
  369. setElement(el);
  370. }
  371. @Override
  372. public void onClose(VCloseEvent event) {
  373. Tab tab = event.getTab();
  374. if (!tab.isEnabledOnServer()) {
  375. return;
  376. }
  377. int tabIndex = getWidgetIndex(tab);
  378. getTabsheet().sendTabClosedEvent(tabIndex);
  379. }
  380. protected com.google.gwt.user.client.Element getContainerElement() {
  381. return DOM.asOld(tr);
  382. }
  383. public int getTabCount() {
  384. return getWidgetCount();
  385. }
  386. public Tab addTab() {
  387. Tab t = new Tab(this);
  388. int tabIndex = getTabCount();
  389. // Logical attach
  390. insert(t, tr, tabIndex, true);
  391. if (tabIndex == 0) {
  392. // Set the "first" style
  393. t.setStyleNames(false, true);
  394. }
  395. t.addClickHandler(this);
  396. t.setCloseHandler(this);
  397. return t;
  398. }
  399. @Override
  400. public void onClick(ClickEvent event) {
  401. TabCaption caption = (TabCaption) event.getSource();
  402. Element targetElement = event.getNativeEvent().getEventTarget()
  403. .cast();
  404. // the tab should not be focused if the close button was clicked
  405. if (targetElement == caption.getCloseButton()) {
  406. return;
  407. }
  408. int index = getWidgetIndex(caption.getParent());
  409. navigateTab(getTabsheet().focusedTabIndex, index);
  410. getTabsheet().focusedTabIndex = index;
  411. getTabsheet().focusedTab = getTab(index);
  412. getTabsheet().focus();
  413. getTabsheet().loadTabSheet(index);
  414. }
  415. public VTabsheet getTabsheet() {
  416. return tabsheet;
  417. }
  418. public Tab getTab(int index) {
  419. if (index < 0 || index >= getTabCount()) {
  420. return null;
  421. }
  422. return (Tab) super.getWidget(index);
  423. }
  424. public void selectTab(int index) {
  425. final Tab newSelected = getTab(index);
  426. final Tab oldSelected = selected;
  427. newSelected.setStyleNames(true, isFirstVisibleTab(index), true);
  428. newSelected.setTabulatorIndex(getTabsheet().tabulatorIndex);
  429. Roles.getTabRole().setAriaSelectedState(newSelected.getElement(),
  430. SelectedValue.TRUE);
  431. if (oldSelected != null && oldSelected != newSelected) {
  432. oldSelected.setStyleNames(false,
  433. isFirstVisibleTab(getWidgetIndex(oldSelected)));
  434. oldSelected.setTabulatorIndex(-1);
  435. Roles.getTabRole().setAriaSelectedState(
  436. oldSelected.getElement(), SelectedValue.FALSE);
  437. }
  438. // Update the field holding the currently selected tab
  439. selected = newSelected;
  440. // The selected tab might need more (or less) space
  441. newSelected.recalculateCaptionWidth();
  442. getTab(tabsheet.activeTabIndex).recalculateCaptionWidth();
  443. }
  444. public void navigateTab(int fromIndex, int toIndex) {
  445. Tab newNavigated = getTab(toIndex);
  446. if (newNavigated == null) {
  447. throw new IllegalArgumentException(
  448. "Tab at provided index toIndex was not found");
  449. }
  450. Tab oldNavigated = getTab(fromIndex);
  451. newNavigated.setStyleNames(newNavigated.equals(selected),
  452. isFirstVisibleTab(toIndex), true);
  453. if (oldNavigated != null && fromIndex != toIndex) {
  454. oldNavigated.setStyleNames(oldNavigated.equals(selected),
  455. isFirstVisibleTab(fromIndex), false);
  456. }
  457. }
  458. public void removeTab(int i) {
  459. Tab tab = getTab(i);
  460. if (tab == null) {
  461. return;
  462. }
  463. remove(tab);
  464. /*
  465. * If this widget was selected we need to unmark it as the last
  466. * selected
  467. */
  468. if (tab == selected) {
  469. selected = null;
  470. }
  471. // FIXME: Shouldn't something be selected instead?
  472. }
  473. private boolean isFirstVisibleTab(int index) {
  474. return getFirstVisibleTab() == index;
  475. }
  476. /**
  477. * Returns the index of the first visible tab
  478. *
  479. * @return
  480. */
  481. private int getFirstVisibleTab() {
  482. return getNextVisibleTab(-1);
  483. }
  484. /**
  485. * Find the next visible tab. Returns -1 if none is found.
  486. *
  487. * @param i
  488. * @return
  489. */
  490. private int getNextVisibleTab(int i) {
  491. int tabs = getTabCount();
  492. do {
  493. i++;
  494. } while (i < tabs && getTab(i).isHiddenOnServer());
  495. if (i == tabs) {
  496. return -1;
  497. } else {
  498. return i;
  499. }
  500. }
  501. /**
  502. * Find the previous visible tab. Returns -1 if none is found.
  503. *
  504. * @param i
  505. * @return
  506. */
  507. private int getPreviousVisibleTab(int i) {
  508. do {
  509. i--;
  510. } while (i >= 0 && getTab(i).isHiddenOnServer());
  511. return i;
  512. }
  513. public int scrollLeft(int currentFirstVisible) {
  514. int prevVisible = getPreviousVisibleTab(currentFirstVisible);
  515. if (prevVisible == -1) {
  516. return -1;
  517. }
  518. Tab newFirst = getTab(prevVisible);
  519. newFirst.setVisible(true);
  520. newFirst.recalculateCaptionWidth();
  521. return prevVisible;
  522. }
  523. public int scrollRight(int currentFirstVisible) {
  524. int nextVisible = getNextVisibleTab(currentFirstVisible);
  525. if (nextVisible == -1) {
  526. return -1;
  527. }
  528. Tab currentFirst = getTab(currentFirstVisible);
  529. currentFirst.setVisible(false);
  530. currentFirst.recalculateCaptionWidth();
  531. return nextVisible;
  532. }
  533. private void recalculateCaptionWidths() {
  534. for (int i = 0; i < getTabCount(); ++i) {
  535. getTab(i).recalculateCaptionWidth();
  536. }
  537. }
  538. }
  539. // TODO using the CLASSNAME directly makes primaryStyleName for TabSheet of
  540. // very limited use - all use of style names should be refactored in the
  541. // future
  542. public static final String CLASSNAME = TabsheetState.PRIMARY_STYLE_NAME;
  543. public static final String TABS_CLASSNAME = CLASSNAME + "-tabcontainer";
  544. public static final String SCROLLER_CLASSNAME = CLASSNAME + "-scroller";
  545. /** For internal use only. May be removed or replaced in the future. */
  546. // tabbar and 'scroller' container
  547. public final Element tabs;
  548. Tab focusedTab;
  549. /**
  550. * The tabindex property (position in the browser's focus cycle.) Named like
  551. * this to avoid confusion with activeTabIndex.
  552. */
  553. int tabulatorIndex = 0;
  554. private static final FocusImpl focusImpl = FocusImpl.getFocusImplForPanel();
  555. // tab-scroller element
  556. private final Element scroller;
  557. // tab-scroller next button element
  558. private final Element scrollerNext;
  559. // tab-scroller prev button element
  560. private final Element scrollerPrev;
  561. /**
  562. * The index of the first visible tab (when scrolled)
  563. */
  564. private int scrollerIndex = 0;
  565. final TabBar tb = new TabBar(this);
  566. /** For internal use only. May be removed or replaced in the future. */
  567. protected final VTabsheetPanel tabPanel = new VTabsheetPanel();
  568. /** For internal use only. May be removed or replaced in the future. */
  569. public final Element contentNode;
  570. private final Element deco;
  571. /** For internal use only. May be removed or replaced in the future. */
  572. public boolean waitingForResponse;
  573. private String currentStyle;
  574. /** For internal use only. May be removed or replaced in the future. */
  575. private int focusedTabIndex = 0;
  576. /**
  577. * @return Whether the tab could be selected or not.
  578. */
  579. private boolean canSelectTab(final int tabIndex) {
  580. Tab tab = tb.getTab(tabIndex);
  581. if (getApplicationConnection() == null || disabled
  582. || waitingForResponse) {
  583. return false;
  584. }
  585. if (!tab.isEnabledOnServer() || tab.isHiddenOnServer()) {
  586. return false;
  587. }
  588. // Note that we return true when tabIndex == activeTabIndex; the active
  589. // tab could be selected, it's just a no-op.
  590. return true;
  591. }
  592. /**
  593. * Load the content of a tab of the provided index.
  594. *
  595. * @param index
  596. * of the tab to load
  597. */
  598. public void loadTabSheet(int tabIndex) {
  599. if (activeTabIndex != tabIndex && canSelectTab(tabIndex)) {
  600. tb.selectTab(tabIndex);
  601. activeTabIndex = tabIndex;
  602. addStyleDependentName("loading");
  603. // Hide the current contents so a loading indicator can be shown
  604. // instead
  605. getCurrentlyDisplayedWidget().getElement().getParentElement()
  606. .getStyle().setVisibility(Visibility.HIDDEN);
  607. getRpcProxy().setSelected(tabKeys.get(tabIndex).toString());
  608. waitingForResponse = true;
  609. tb.getTab(tabIndex).focus(); // move keyboard focus to active tab
  610. }
  611. }
  612. /**
  613. * Returns the currently displayed widget in the tab panel.
  614. *
  615. * @since 7.2
  616. * @return currently displayed content widget
  617. */
  618. public Widget getCurrentlyDisplayedWidget() {
  619. return tabPanel.getWidget(tabPanel.getVisibleWidget());
  620. }
  621. /**
  622. * Returns the client to server RPC proxy for the tabsheet.
  623. *
  624. * @since 7.2
  625. * @return RPC proxy
  626. */
  627. protected TabsheetServerRpc getRpcProxy() {
  628. return connector.getRpcProxy(TabsheetServerRpc.class);
  629. }
  630. /**
  631. * For internal use only.
  632. *
  633. * Avoid using this method directly and use appropriate superclass methods
  634. * where applicable.
  635. *
  636. * @deprecated since 7.2 - use more specific methods instead (getRpcProxy(),
  637. * getConnectorForWidget(Widget) etc.)
  638. * @return ApplicationConnection
  639. */
  640. @Deprecated
  641. public ApplicationConnection getApplicationConnection() {
  642. return client;
  643. }
  644. private VTooltip getVTooltip() {
  645. return getApplicationConnection().getVTooltip();
  646. }
  647. public void tabSizeMightHaveChanged(Tab tab) {
  648. // icon onloads may change total width of tabsheet
  649. if (isDynamicWidth()) {
  650. updateDynamicWidth();
  651. }
  652. updateTabScroller();
  653. }
  654. void sendTabClosedEvent(int tabIndex) {
  655. getRpcProxy().closeTab(tabKeys.get(tabIndex));
  656. }
  657. public VTabsheet() {
  658. super(CLASSNAME);
  659. addHandler(this, FocusEvent.getType());
  660. addHandler(this, BlurEvent.getType());
  661. // Tab scrolling
  662. getElement().getStyle().setOverflow(Overflow.HIDDEN);
  663. tabs = DOM.createDiv();
  664. DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
  665. Roles.getTablistRole().set(tabs);
  666. Roles.getTablistRole().setAriaLiveProperty(tabs, LiveValue.OFF);
  667. scroller = DOM.createDiv();
  668. Roles.getTablistRole().setAriaHiddenState(scroller, true);
  669. DOM.setElementProperty(scroller, "className", SCROLLER_CLASSNAME);
  670. scrollerPrev = DOM.createButton();
  671. scrollerPrev.setTabIndex(-1);
  672. DOM.setElementProperty(scrollerPrev, "className", SCROLLER_CLASSNAME
  673. + "Prev");
  674. Roles.getTablistRole().setAriaHiddenState(scrollerPrev, true);
  675. DOM.sinkEvents(scrollerPrev, Event.ONCLICK);
  676. scrollerNext = DOM.createButton();
  677. scrollerNext.setTabIndex(-1);
  678. DOM.setElementProperty(scrollerNext, "className", SCROLLER_CLASSNAME
  679. + "Next");
  680. Roles.getTablistRole().setAriaHiddenState(scrollerNext, true);
  681. DOM.sinkEvents(scrollerNext, Event.ONCLICK);
  682. DOM.appendChild(getElement(), tabs);
  683. // Tabs
  684. tabPanel.setStyleName(CLASSNAME + "-tabsheetpanel");
  685. contentNode = DOM.createDiv();
  686. Roles.getTabpanelRole().set(contentNode);
  687. deco = DOM.createDiv();
  688. addStyleDependentName("loading"); // Indicate initial progress
  689. tb.setStyleName(CLASSNAME + "-tabs");
  690. DOM.setElementProperty(contentNode, "className", CLASSNAME + "-content");
  691. DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
  692. add(tb, tabs);
  693. DOM.appendChild(scroller, scrollerPrev);
  694. DOM.appendChild(scroller, scrollerNext);
  695. DOM.appendChild(getElement(), contentNode);
  696. add(tabPanel, contentNode);
  697. DOM.appendChild(getElement(), deco);
  698. DOM.appendChild(tabs, scroller);
  699. // TODO Use for Safari only. Fix annoying 1px first cell in TabBar.
  700. // DOM.setStyleAttribute(DOM.getFirstChild(DOM.getFirstChild(DOM
  701. // .getFirstChild(tb.getElement()))), "display", "none");
  702. }
  703. @Override
  704. public void onBrowserEvent(Event event) {
  705. if (event.getTypeInt() == Event.ONCLICK) {
  706. // Tab scrolling
  707. if (isScrolledTabs() && DOM.eventGetTarget(event) == scrollerPrev) {
  708. int newFirstIndex = tb.scrollLeft(scrollerIndex);
  709. if (newFirstIndex != -1) {
  710. scrollerIndex = newFirstIndex;
  711. updateTabScroller();
  712. }
  713. event.stopPropagation();
  714. return;
  715. } else if (isClippedTabs()
  716. && DOM.eventGetTarget(event) == scrollerNext) {
  717. int newFirstIndex = tb.scrollRight(scrollerIndex);
  718. if (newFirstIndex != -1) {
  719. scrollerIndex = newFirstIndex;
  720. updateTabScroller();
  721. }
  722. event.stopPropagation();
  723. return;
  724. }
  725. }
  726. super.onBrowserEvent(event);
  727. }
  728. /**
  729. * Checks if the tab with the selected index has been scrolled out of the
  730. * view (on the left side).
  731. *
  732. * @param index
  733. * @return
  734. */
  735. private boolean scrolledOutOfView(int index) {
  736. return scrollerIndex > index;
  737. }
  738. /** For internal use only. May be removed or replaced in the future. */
  739. public void handleStyleNames(AbstractComponentState state) {
  740. // Add proper stylenames for all elements (easier to prevent unwanted
  741. // style inheritance)
  742. if (ComponentStateUtil.hasStyles(state)) {
  743. final List<String> styles = state.styles;
  744. if (currentStyle == null || !currentStyle.equals(styles.toString())) {
  745. currentStyle = styles.toString();
  746. final String tabsBaseClass = TABS_CLASSNAME;
  747. String tabsClass = tabsBaseClass;
  748. final String contentBaseClass = CLASSNAME + "-content";
  749. String contentClass = contentBaseClass;
  750. final String decoBaseClass = CLASSNAME + "-deco";
  751. String decoClass = decoBaseClass;
  752. for (String style : styles) {
  753. tb.addStyleDependentName(style);
  754. tabsClass += " " + tabsBaseClass + "-" + style;
  755. contentClass += " " + contentBaseClass + "-" + style;
  756. decoClass += " " + decoBaseClass + "-" + style;
  757. }
  758. DOM.setElementProperty(tabs, "className", tabsClass);
  759. DOM.setElementProperty(contentNode, "className", contentClass);
  760. DOM.setElementProperty(deco, "className", decoClass);
  761. }
  762. } else {
  763. tb.setStyleName(CLASSNAME + "-tabs");
  764. DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
  765. DOM.setElementProperty(contentNode, "className", CLASSNAME
  766. + "-content");
  767. DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
  768. }
  769. }
  770. /** For internal use only. May be removed or replaced in the future. */
  771. public void updateDynamicWidth() {
  772. // Find width consumed by tabs
  773. TableCellElement spacerCell = ((TableElement) tb.getElement().cast())
  774. .getRows().getItem(0).getCells().getItem(tb.getTabCount());
  775. int spacerWidth = spacerCell.getOffsetWidth();
  776. DivElement div = (DivElement) spacerCell.getFirstChildElement();
  777. int spacerMinWidth = spacerCell.getOffsetWidth() - div.getOffsetWidth();
  778. int tabsWidth = tb.getOffsetWidth() - spacerWidth + spacerMinWidth;
  779. // Find content width
  780. Style style = tabPanel.getElement().getStyle();
  781. String overflow = style.getProperty("overflow");
  782. style.setProperty("overflow", "hidden");
  783. style.setPropertyPx("width", tabsWidth);
  784. boolean hasTabs = tabPanel.getWidgetCount() > 0;
  785. Style wrapperstyle = null;
  786. if (hasTabs) {
  787. wrapperstyle = getCurrentlyDisplayedWidget().getElement()
  788. .getParentElement().getStyle();
  789. wrapperstyle.setPropertyPx("width", tabsWidth);
  790. }
  791. // Get content width from actual widget
  792. int contentWidth = 0;
  793. if (hasTabs) {
  794. contentWidth = getCurrentlyDisplayedWidget().getOffsetWidth();
  795. }
  796. style.setProperty("overflow", overflow);
  797. // Set widths to max(tabs,content)
  798. if (tabsWidth < contentWidth) {
  799. tabsWidth = contentWidth;
  800. }
  801. int outerWidth = tabsWidth + getContentAreaBorderWidth();
  802. tabs.getStyle().setPropertyPx("width", outerWidth);
  803. style.setPropertyPx("width", tabsWidth);
  804. if (hasTabs) {
  805. wrapperstyle.setPropertyPx("width", tabsWidth);
  806. }
  807. contentNode.getStyle().setPropertyPx("width", tabsWidth);
  808. super.setWidth(outerWidth + "px");
  809. updateOpenTabSize();
  810. }
  811. @Override
  812. public void renderTab(final TabState tabState, int index) {
  813. Tab tab = tb.getTab(index);
  814. if (tab == null) {
  815. tab = tb.addTab();
  816. }
  817. tab.updateFromState(tabState);
  818. tab.setEnabledOnServer((!disabledTabKeys.contains(tabKeys.get(index))));
  819. tab.setHiddenOnServer(!tabState.visible);
  820. if (scrolledOutOfView(index)) {
  821. // Should not set tabs visible if they are scrolled out of view
  822. tab.setVisible(false);
  823. } else {
  824. tab.setVisible(tabState.visible);
  825. }
  826. /*
  827. * Force the width of the caption container so the content will not wrap
  828. * and tabs won't be too narrow in certain browsers
  829. */
  830. tab.recalculateCaptionWidth();
  831. }
  832. /**
  833. * @deprecated as of 7.1, VTabsheet only keeps the active tab in the DOM
  834. * without any place holders.
  835. */
  836. @Deprecated
  837. public class PlaceHolder extends VLabel {
  838. public PlaceHolder() {
  839. super("");
  840. }
  841. }
  842. /**
  843. * Renders the widget content for a tab sheet.
  844. *
  845. * @param newWidget
  846. */
  847. public void renderContent(Widget newWidget) {
  848. assert tabPanel.getWidgetCount() <= 1;
  849. if (null == newWidget) {
  850. newWidget = new SimplePanel();
  851. }
  852. if (tabPanel.getWidgetCount() == 0) {
  853. tabPanel.add(newWidget);
  854. } else if (tabPanel.getWidget(0) != newWidget) {
  855. tabPanel.remove(0);
  856. tabPanel.add(newWidget);
  857. }
  858. assert tabPanel.getWidgetCount() <= 1;
  859. // There's never any other index than 0, but maintaining API for now
  860. tabPanel.showWidget(0);
  861. VTabsheet.this.iLayout();
  862. updateOpenTabSize();
  863. VTabsheet.this.removeStyleDependentName("loading");
  864. }
  865. /**
  866. * Recalculates the sizes of tab captions, causing the tabs to be rendered
  867. * the correct size.
  868. */
  869. private void updateTabCaptionSizes() {
  870. for (int tabIx = 0; tabIx < tb.getTabCount(); tabIx++) {
  871. tb.getTab(tabIx).recalculateCaptionWidth();
  872. }
  873. }
  874. /** For internal use only. May be removed or replaced in the future. */
  875. public void updateContentNodeHeight() {
  876. if (!isDynamicHeight()) {
  877. int contentHeight = getOffsetHeight();
  878. contentHeight -= DOM.getElementPropertyInt(deco, "offsetHeight");
  879. contentHeight -= tb.getOffsetHeight();
  880. if (contentHeight < 0) {
  881. contentHeight = 0;
  882. }
  883. // Set proper values for content element
  884. contentNode.getStyle().setHeight(contentHeight, Unit.PX);
  885. } else {
  886. contentNode.getStyle().clearHeight();
  887. }
  888. }
  889. /**
  890. * Run internal layouting.
  891. */
  892. public void iLayout() {
  893. updateTabScroller();
  894. updateTabCaptionSizes();
  895. }
  896. /**
  897. * Sets the size of the visible tab (component). As the tab is set to
  898. * position: absolute (to work around a firefox flickering bug) we must keep
  899. * this up-to-date by hand.
  900. * <p>
  901. * For internal use only. May be removed or replaced in the future.
  902. */
  903. public void updateOpenTabSize() {
  904. /*
  905. * The overflow=auto element must have a height specified, otherwise it
  906. * will be just as high as the contents and no scrollbars will appear
  907. */
  908. int height = -1;
  909. int width = -1;
  910. int minWidth = 0;
  911. if (!isDynamicHeight()) {
  912. height = contentNode.getOffsetHeight();
  913. }
  914. if (!isDynamicWidth()) {
  915. width = contentNode.getOffsetWidth() - getContentAreaBorderWidth();
  916. } else {
  917. /*
  918. * If the tabbar is wider than the content we need to use the tabbar
  919. * width as minimum width so scrollbars get placed correctly (at the
  920. * right edge).
  921. */
  922. minWidth = tb.getOffsetWidth() - getContentAreaBorderWidth();
  923. }
  924. tabPanel.fixVisibleTabSize(width, height, minWidth);
  925. }
  926. /**
  927. * Layouts the tab-scroller elements, and applies styles.
  928. */
  929. private void updateTabScroller() {
  930. if (!isDynamicWidth()) {
  931. tabs.getStyle().setWidth(100, Unit.PCT);
  932. }
  933. // Make sure scrollerIndex is valid
  934. if (scrollerIndex < 0 || scrollerIndex > tb.getTabCount()) {
  935. scrollerIndex = tb.getFirstVisibleTab();
  936. } else if (tb.getTabCount() > 0
  937. && tb.getTab(scrollerIndex).isHiddenOnServer()) {
  938. scrollerIndex = tb.getNextVisibleTab(scrollerIndex);
  939. }
  940. boolean scrolled = isScrolledTabs();
  941. boolean clipped = isClippedTabs();
  942. if (tb.getTabCount() > 0 && tb.isVisible() && (scrolled || clipped)) {
  943. scroller.getStyle().clearDisplay();
  944. DOM.setElementProperty(scrollerPrev, "className",
  945. SCROLLER_CLASSNAME + (scrolled ? "Prev" : "Prev-disabled"));
  946. DOM.setElementProperty(scrollerNext, "className",
  947. SCROLLER_CLASSNAME + (clipped ? "Next" : "Next-disabled"));
  948. // the active tab should be focusable if and only if it is visible
  949. boolean isActiveTabVisible = scrollerIndex <= activeTabIndex
  950. && !isClipped(tb.selected);
  951. tb.selected.setTabulatorIndex(isActiveTabVisible ? tabulatorIndex
  952. : -1);
  953. } else {
  954. scroller.getStyle().setDisplay(Display.NONE);
  955. }
  956. if (BrowserInfo.get().isSafari()) {
  957. /*
  958. * another hack for webkits. tabscroller sometimes drops without
  959. * "shaking it" reproducable in
  960. * com.vaadin.tests.components.tabsheet.TabSheetIcons
  961. */
  962. final Style style = scroller.getStyle();
  963. style.setProperty("whiteSpace", "normal");
  964. Scheduler.get().scheduleDeferred(new Command() {
  965. @Override
  966. public void execute() {
  967. style.setProperty("whiteSpace", "");
  968. }
  969. });
  970. }
  971. }
  972. /** For internal use only. May be removed or replaced in the future. */
  973. public void showAllTabs() {
  974. scrollerIndex = tb.getFirstVisibleTab();
  975. for (int i = 0; i < tb.getTabCount(); i++) {
  976. Tab t = tb.getTab(i);
  977. if (!t.isHiddenOnServer()) {
  978. t.setVisible(true);
  979. }
  980. }
  981. }
  982. private boolean isScrolledTabs() {
  983. return scrollerIndex > tb.getFirstVisibleTab();
  984. }
  985. private boolean isClippedTabs() {
  986. return (tb.getOffsetWidth() - DOM.getElementPropertyInt((Element) tb
  987. .getContainerElement().getLastChild().cast(), "offsetWidth")) > getOffsetWidth()
  988. - (isScrolledTabs() ? scroller.getOffsetWidth() : 0);
  989. }
  990. private boolean isClipped(Tab tab) {
  991. return tab.getAbsoluteLeft() + tab.getOffsetWidth() > getAbsoluteLeft()
  992. + getOffsetWidth() - scroller.getOffsetWidth();
  993. }
  994. @Override
  995. protected void clearPaintables() {
  996. int i = tb.getTabCount();
  997. while (i > 0) {
  998. tb.removeTab(--i);
  999. }
  1000. tabPanel.clear();
  1001. }
  1002. @Override
  1003. public Iterator<Widget> getWidgetIterator() {
  1004. return tabPanel.iterator();
  1005. }
  1006. /** For internal use only. May be removed or replaced in the future. */
  1007. public int getContentAreaBorderWidth() {
  1008. return Util.measureHorizontalBorder(contentNode);
  1009. }
  1010. @Override
  1011. public int getTabCount() {
  1012. return tb.getTabCount();
  1013. }
  1014. @Override
  1015. public ComponentConnector getTab(int index) {
  1016. if (tabPanel.getWidgetCount() > index) {
  1017. Widget widget = tabPanel.getWidget(index);
  1018. return getConnectorForWidget(widget);
  1019. }
  1020. return null;
  1021. }
  1022. @Override
  1023. public void removeTab(int index) {
  1024. tb.removeTab(index);
  1025. // Removing content from tp is handled by the connector
  1026. }
  1027. @Override
  1028. public void selectTab(int index) {
  1029. tb.selectTab(index);
  1030. }
  1031. @Override
  1032. public void onBlur(BlurEvent event) {
  1033. getVTooltip().hideTooltip();
  1034. if (focusedTab != null && focusedTab == event.getSource()) {
  1035. focusedTab.removeAssistiveDescription();
  1036. focusedTab = null;
  1037. if (connector.hasEventListener(EventId.BLUR)) {
  1038. connector.getRpcProxy(FocusAndBlurServerRpc.class).blur();
  1039. }
  1040. }
  1041. }
  1042. @Override
  1043. public void onFocus(FocusEvent event) {
  1044. if (focusedTab == null && event.getSource() instanceof Tab) {
  1045. focusedTab = (Tab) event.getSource();
  1046. if (connector.hasEventListener(EventId.FOCUS)) {
  1047. connector.getRpcProxy(FocusAndBlurServerRpc.class).focus();
  1048. }
  1049. if (focusedTab.hasTooltip()) {
  1050. focusedTab.setAssistiveDescription(getVTooltip().getUniqueId());
  1051. getVTooltip().showAssistive(focusedTab.getTooltipInfo());
  1052. }
  1053. }
  1054. }
  1055. @Override
  1056. public void focus() {
  1057. tb.getTab(activeTabIndex).focus();
  1058. }
  1059. public void blur() {
  1060. tb.getTab(activeTabIndex).blur();
  1061. }
  1062. @Override
  1063. public void onKeyDown(KeyDownEvent event) {
  1064. if (event.getSource() instanceof Tab) {
  1065. int keycode = event.getNativeEvent().getKeyCode();
  1066. // Scroll throw the tabs.
  1067. if (!event.isAnyModifierKeyDown()) {
  1068. if (keycode == getPreviousTabKey()) {
  1069. selectPreviousTab();
  1070. event.stopPropagation();
  1071. } else if (keycode == getNextTabKey()) {
  1072. selectNextTab();
  1073. event.stopPropagation();
  1074. } else if (keycode == getCloseTabKey()) {
  1075. Tab tab = tb.getTab(activeTabIndex);
  1076. if (tab.isClosable()) {
  1077. tab.onClose();
  1078. }
  1079. } else if (keycode == getSelectTabKey()) {
  1080. loadTabSheet(focusedTabIndex);
  1081. // Prevent the page from scrolling when hitting space
  1082. // (select key) to select the current tab.
  1083. event.preventDefault();
  1084. }
  1085. }
  1086. }
  1087. }
  1088. /**
  1089. * @return The key code of the keyboard shortcut that selects the previous
  1090. * tab in a focused tabsheet.
  1091. */
  1092. protected int getPreviousTabKey() {
  1093. return KeyCodes.KEY_LEFT;
  1094. }
  1095. /**
  1096. * Gets the key to activate the selected tab when navigating using
  1097. * previous/next (left/right) keys.
  1098. *
  1099. * @return the key to activate the selected tab.
  1100. *
  1101. * @see #getNextTabKey()
  1102. * @see #getPreviousTabKey()
  1103. */
  1104. protected int getSelectTabKey() {
  1105. return KeyCodes.KEY_SPACE;
  1106. }
  1107. /**
  1108. * @return The key code of the keyboard shortcut that selects the next tab
  1109. * in a focused tabsheet.
  1110. */
  1111. protected int getNextTabKey() {
  1112. return KeyCodes.KEY_RIGHT;
  1113. }
  1114. /**
  1115. * @return The key code of the keyboard shortcut that closes the currently
  1116. * selected tab in a focused tabsheet.
  1117. */
  1118. protected int getCloseTabKey() {
  1119. return KeyCodes.KEY_DELETE;
  1120. }
  1121. private void selectPreviousTab() {
  1122. int newTabIndex = focusedTabIndex;
  1123. // Find the previous visible and enabled tab if any.
  1124. do {
  1125. newTabIndex--;
  1126. } while (newTabIndex >= 0 && !canSelectTab(newTabIndex));
  1127. if (newTabIndex >= 0) {
  1128. tb.navigateTab(focusedTabIndex, newTabIndex);
  1129. focusedTabIndex = newTabIndex;
  1130. // If this TabSheet already has focus, set the new selected tab
  1131. // as focused.
  1132. if (focusedTab != null) {
  1133. focusedTab = tb.getTab(focusedTabIndex);
  1134. focusedTab.focus();
  1135. }
  1136. }
  1137. }
  1138. private void selectNextTab() {
  1139. int newTabIndex = focusedTabIndex;
  1140. // Find the next visible and enabled tab if any.
  1141. do {
  1142. newTabIndex++;
  1143. } while (newTabIndex < getTabCount() && !canSelectTab(newTabIndex));
  1144. if (newTabIndex < getTabCount()) {
  1145. tb.navigateTab(focusedTabIndex, newTabIndex);
  1146. focusedTabIndex = newTabIndex;
  1147. // If this TabSheet already has focus, set the new selected tab
  1148. // as focused.
  1149. if (focusedTab != null) {
  1150. focusedTab = tb.getTab(focusedTabIndex);
  1151. focusedTab.focus();
  1152. }
  1153. }
  1154. }
  1155. private void scrollIntoView(Tab tab) {
  1156. if (!tab.isHiddenOnServer()) {
  1157. if (isClipped(tab)) {
  1158. while (isClipped(tab) && scrollerIndex != -1) {
  1159. scrollerIndex = tb.scrollRight(scrollerIndex);
  1160. }
  1161. updateTabScroller();
  1162. } else if (!tab.isVisible()) {
  1163. while (!tab.isVisible()) {
  1164. scrollerIndex = tb.scrollLeft(scrollerIndex);
  1165. }
  1166. updateTabScroller();
  1167. }
  1168. }
  1169. }
  1170. /**
  1171. * Makes tab bar visible.
  1172. *
  1173. * @since 7.2
  1174. */
  1175. public void showTabs() {
  1176. tb.setVisible(true);
  1177. removeStyleName(CLASSNAME + "-hidetabs");
  1178. tb.recalculateCaptionWidths();
  1179. }
  1180. /**
  1181. * Makes tab bar invisible.
  1182. *
  1183. * @since 7.2
  1184. */
  1185. public void hideTabs() {
  1186. tb.setVisible(false);
  1187. addStyleName(CLASSNAME + "-hidetabs");
  1188. }
  1189. /** Matches tab[ix] - used for extracting the index of the targeted tab */
  1190. private static final RegExp SUBPART_TAB_REGEXP = RegExp
  1191. .compile("tab\\[(\\d+)](.*)");
  1192. @Override
  1193. public com.google.gwt.user.client.Element getSubPartElement(String subPart) {
  1194. if ("tabpanel".equals(subPart)) {
  1195. return DOM.asOld(tabPanel.getElement().getFirstChildElement());
  1196. } else if (SUBPART_TAB_REGEXP.test(subPart)) {
  1197. MatchResult result = SUBPART_TAB_REGEXP.exec(subPart);
  1198. int tabIx = Integer.valueOf(result.getGroup(1));
  1199. Tab tab = tb.getTab(tabIx);
  1200. if (tab != null) {
  1201. if ("/close".equals(result.getGroup(2))) {
  1202. if (tab.isClosable()) {
  1203. return tab.tabCaption.getCloseButton();
  1204. }
  1205. } else {
  1206. return tab.tabCaption.getElement();
  1207. }
  1208. }
  1209. }
  1210. return null;
  1211. }
  1212. @Override
  1213. public String getSubPartName(com.google.gwt.user.client.Element subElement) {
  1214. if (tabPanel.getElement().equals(subElement.getParentElement())
  1215. || tabPanel.getElement().equals(subElement)) {
  1216. return "tabpanel";
  1217. } else {
  1218. for (int i = 0; i < tb.getTabCount(); ++i) {
  1219. Tab tab = tb.getTab(i);
  1220. if (tab.isClosable()
  1221. && tab.tabCaption.getCloseButton().isOrHasChild(
  1222. subElement)) {
  1223. return "tab[" + i + "]/close";
  1224. } else if (tab.getElement().isOrHasChild(subElement)) {
  1225. return "tab[" + i + "]";
  1226. }
  1227. }
  1228. }
  1229. return null;
  1230. }
  1231. }