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 65KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  1. /*
  2. * Copyright 2000-2016 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.HasMouseDownHandlers;
  43. import com.google.gwt.event.dom.client.KeyCodes;
  44. import com.google.gwt.event.dom.client.KeyDownEvent;
  45. import com.google.gwt.event.dom.client.KeyDownHandler;
  46. import com.google.gwt.event.dom.client.MouseDownEvent;
  47. import com.google.gwt.event.dom.client.MouseDownHandler;
  48. import com.google.gwt.event.shared.HandlerRegistration;
  49. import com.google.gwt.regexp.shared.MatchResult;
  50. import com.google.gwt.regexp.shared.RegExp;
  51. import com.google.gwt.user.client.Command;
  52. import com.google.gwt.user.client.DOM;
  53. import com.google.gwt.user.client.Event;
  54. import com.google.gwt.user.client.ui.ComplexPanel;
  55. import com.google.gwt.user.client.ui.SimplePanel;
  56. import com.google.gwt.user.client.ui.Widget;
  57. import com.google.gwt.user.client.ui.impl.FocusImpl;
  58. import com.vaadin.client.ApplicationConnection;
  59. import com.vaadin.client.BrowserInfo;
  60. import com.vaadin.client.ComponentConnector;
  61. import com.vaadin.client.ComputedStyle;
  62. import com.vaadin.client.Focusable;
  63. import com.vaadin.client.TooltipInfo;
  64. import com.vaadin.client.VCaption;
  65. import com.vaadin.client.VTooltip;
  66. import com.vaadin.client.WidgetUtil;
  67. import com.vaadin.client.ui.aria.AriaHelper;
  68. import com.vaadin.shared.AbstractComponentState;
  69. import com.vaadin.shared.ComponentConstants;
  70. import com.vaadin.shared.EventId;
  71. import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc;
  72. import com.vaadin.shared.ui.ComponentStateUtil;
  73. import com.vaadin.shared.ui.tabsheet.TabState;
  74. import com.vaadin.shared.ui.tabsheet.TabsheetServerRpc;
  75. import com.vaadin.shared.ui.tabsheet.TabsheetState;
  76. public class VTabsheet extends VTabsheetBase
  77. implements Focusable, SubPartAware {
  78. private static final String PREV_SCROLLER_DISABLED_CLASSNAME = "Prev-disabled";
  79. private static class VCloseEvent {
  80. private Tab tab;
  81. VCloseEvent(Tab tab) {
  82. this.tab = tab;
  83. }
  84. public Tab getTab() {
  85. return tab;
  86. }
  87. }
  88. private interface VCloseHandler {
  89. public void onClose(VCloseEvent event);
  90. }
  91. /**
  92. * Representation of a single "tab" shown in the TabBar.
  93. *
  94. */
  95. public static class Tab extends SimplePanel implements HasFocusHandlers,
  96. HasBlurHandlers, HasMouseDownHandlers, HasKeyDownHandlers {
  97. private static final String TD_CLASSNAME = CLASSNAME + "-tabitemcell";
  98. private static final String TD_FIRST_CLASSNAME = TD_CLASSNAME
  99. + "-first";
  100. private static final String TD_SELECTED_CLASSNAME = TD_CLASSNAME
  101. + "-selected";
  102. private static final String TD_SELECTED_FIRST_CLASSNAME = TD_SELECTED_CLASSNAME
  103. + "-first";
  104. private static final String TD_FOCUS_CLASSNAME = TD_CLASSNAME
  105. + "-focus";
  106. private static final String TD_FOCUS_FIRST_CLASSNAME = TD_FOCUS_CLASSNAME
  107. + "-first";
  108. private static final String TD_DISABLED_CLASSNAME = TD_CLASSNAME
  109. + "-disabled";
  110. private static final String DIV_CLASSNAME = CLASSNAME + "-tabitem";
  111. private static final String DIV_SELECTED_CLASSNAME = DIV_CLASSNAME
  112. + "-selected";
  113. private static final String DIV_FOCUS_CLASSNAME = DIV_CLASSNAME
  114. + "-focus";
  115. private TabCaption tabCaption;
  116. Element td = getElement();
  117. private VCloseHandler closeHandler;
  118. private boolean enabledOnServer = true;
  119. private Element div;
  120. private TabBar tabBar;
  121. private boolean hiddenOnServer = false;
  122. private String styleName;
  123. private String id;
  124. private Tab(TabBar tabBar) {
  125. super(DOM.createTD());
  126. this.tabBar = tabBar;
  127. setStyleName(td, TD_CLASSNAME);
  128. Roles.getTabRole().set(getElement());
  129. Roles.getTabRole().setAriaSelectedState(getElement(),
  130. SelectedValue.FALSE);
  131. div = DOM.createDiv();
  132. setTabulatorIndex(-1);
  133. setStyleName(div, DIV_CLASSNAME);
  134. DOM.appendChild(td, div);
  135. tabCaption = new TabCaption(this);
  136. add(tabCaption);
  137. Roles.getTabRole().setAriaLabelledbyProperty(getElement(),
  138. Id.of(tabCaption.getElement()));
  139. }
  140. public boolean isHiddenOnServer() {
  141. return hiddenOnServer;
  142. }
  143. public void setHiddenOnServer(boolean hiddenOnServer) {
  144. this.hiddenOnServer = hiddenOnServer;
  145. Roles.getTabRole().setAriaHiddenState(getElement(), hiddenOnServer);
  146. }
  147. @Override
  148. protected com.google.gwt.user.client.Element getContainerElement() {
  149. // Attach caption element to div, not td
  150. return DOM.asOld(div);
  151. }
  152. public boolean isEnabledOnServer() {
  153. return enabledOnServer;
  154. }
  155. public void setEnabledOnServer(boolean enabled) {
  156. enabledOnServer = enabled;
  157. Roles.getTabRole().setAriaDisabledState(getElement(), !enabled);
  158. setStyleName(td, TD_DISABLED_CLASSNAME, !enabled);
  159. if (!enabled) {
  160. FOCUS_IMPL.setTabIndex(td, -1);
  161. }
  162. }
  163. public void addClickHandler(ClickHandler handler) {
  164. tabCaption.addClickHandler(handler);
  165. }
  166. public void setCloseHandler(VCloseHandler closeHandler) {
  167. this.closeHandler = closeHandler;
  168. }
  169. /**
  170. * Toggles the style names for the Tab.
  171. *
  172. * @param selected
  173. * true if the Tab is selected
  174. * @param first
  175. * true if the Tab is the first visible Tab
  176. */
  177. public void setStyleNames(boolean selected, boolean first) {
  178. setStyleNames(selected, first, false);
  179. }
  180. public void setStyleNames(boolean selected, boolean first,
  181. boolean keyboardFocus) {
  182. setStyleName(td, TD_FIRST_CLASSNAME, first);
  183. setStyleName(td, TD_SELECTED_CLASSNAME, selected);
  184. setStyleName(td, TD_SELECTED_FIRST_CLASSNAME, selected && first);
  185. setStyleName(div, DIV_SELECTED_CLASSNAME, selected);
  186. setStyleName(td, TD_FOCUS_CLASSNAME, keyboardFocus);
  187. setStyleName(td, TD_FOCUS_FIRST_CLASSNAME, keyboardFocus && first);
  188. setStyleName(div, DIV_FOCUS_CLASSNAME, keyboardFocus);
  189. }
  190. public void setTabulatorIndex(int tabIndex) {
  191. getElement().setTabIndex(tabIndex);
  192. }
  193. public boolean isClosable() {
  194. return tabCaption.isClosable();
  195. }
  196. public void onClose() {
  197. closeHandler.onClose(new VCloseEvent(this));
  198. }
  199. public VTabsheet getTabsheet() {
  200. return tabBar.getTabsheet();
  201. }
  202. private void updateFromState(TabState tabState) {
  203. tabCaption.setCaptionAsHtml(getTabsheet().isTabCaptionsAsHtml());
  204. tabCaption.update(tabState);
  205. // Apply the styleName set for the tab
  206. String newStyleName = tabState.styleName;
  207. // Find the nth td element
  208. if (newStyleName != null && !newStyleName.isEmpty()) {
  209. if (!newStyleName.equals(styleName)) {
  210. // If we have a new style name
  211. if (styleName != null && !styleName.isEmpty()) {
  212. // Remove old style name if present
  213. td.removeClassName(TD_CLASSNAME + "-" + styleName);
  214. }
  215. // Set new style name
  216. td.addClassName(TD_CLASSNAME + "-" + newStyleName);
  217. styleName = newStyleName;
  218. }
  219. } else if (styleName != null) {
  220. // Remove the set stylename if no stylename is present in the
  221. // uidl
  222. td.removeClassName(TD_CLASSNAME + "-" + styleName);
  223. styleName = null;
  224. }
  225. String newId = tabState.id;
  226. if (newId != null && !newId.isEmpty()) {
  227. td.setId(newId);
  228. id = newId;
  229. } else if (id != null) {
  230. td.removeAttribute("id");
  231. id = null;
  232. }
  233. }
  234. public void recalculateCaptionWidth() {
  235. tabCaption.setWidth(tabCaption.getRequiredWidth() + "px");
  236. }
  237. @Override
  238. public HandlerRegistration addFocusHandler(FocusHandler handler) {
  239. return addDomHandler(handler, FocusEvent.getType());
  240. }
  241. @Override
  242. public HandlerRegistration addBlurHandler(BlurHandler handler) {
  243. return addDomHandler(handler, BlurEvent.getType());
  244. }
  245. @Override
  246. public HandlerRegistration addMouseDownHandler(
  247. MouseDownHandler handler) {
  248. return addDomHandler(handler, MouseDownEvent.getType());
  249. }
  250. @Override
  251. public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
  252. return addDomHandler(handler, KeyDownEvent.getType());
  253. }
  254. public void focus() {
  255. getTabsheet().scrollIntoView(this);
  256. FOCUS_IMPL.focus(td);
  257. }
  258. public void blur() {
  259. FOCUS_IMPL.blur(td);
  260. }
  261. public boolean hasTooltip() {
  262. return tabCaption.getTooltipInfo() != null;
  263. }
  264. public TooltipInfo getTooltipInfo() {
  265. return tabCaption.getTooltipInfo();
  266. }
  267. public void setAssistiveDescription(String descriptionId) {
  268. Roles.getTablistRole().setAriaDescribedbyProperty(getElement(),
  269. Id.of(descriptionId));
  270. }
  271. public void removeAssistiveDescription() {
  272. Roles.getTablistRole().removeAriaDescribedbyProperty(getElement());
  273. }
  274. }
  275. public static class TabCaption extends VCaption {
  276. private boolean closable = false;
  277. private Element closeButton;
  278. private Tab tab;
  279. TabCaption(Tab tab) {
  280. super(tab.getTabsheet().connector.getConnection());
  281. this.tab = tab;
  282. AriaHelper.ensureHasId(getElement());
  283. }
  284. private boolean update(TabState tabState) {
  285. if (tabState.description != null
  286. || tabState.componentError != null) {
  287. setTooltipInfo(new TooltipInfo(tabState.description,
  288. tabState.descriptionContentMode,
  289. tabState.componentError, this,
  290. tabState.componentErrorLevel));
  291. } else {
  292. setTooltipInfo(null);
  293. }
  294. // TODO need to call this instead of super because the caption does
  295. // not have an owner
  296. String captionString = tabState.caption.isEmpty() ? null
  297. : tabState.caption;
  298. boolean ret = updateCaptionWithoutOwner(captionString,
  299. !tabState.enabled, hasAttribute(tabState.description),
  300. hasAttribute(tabState.componentError),
  301. tabState.componentErrorLevel,
  302. tab.getTabsheet().connector.getResourceUrl(
  303. ComponentConstants.ICON_RESOURCE + tabState.key),
  304. tabState.iconAltText);
  305. setClosable(tabState.closable);
  306. return ret;
  307. }
  308. private boolean hasAttribute(String string) {
  309. return string != null && !string.trim().isEmpty();
  310. }
  311. private VTabsheet getTabsheet() {
  312. return tab.getTabsheet();
  313. }
  314. @Override
  315. public void onBrowserEvent(Event event) {
  316. if (closable && event.getTypeInt() == Event.ONCLICK
  317. && event.getEventTarget().cast() == closeButton) {
  318. tab.onClose();
  319. event.stopPropagation();
  320. event.preventDefault();
  321. }
  322. super.onBrowserEvent(event);
  323. if (event.getTypeInt() == Event.ONLOAD) {
  324. getTabsheet().tabSizeMightHaveChanged(getTab());
  325. }
  326. }
  327. public Tab getTab() {
  328. return tab;
  329. }
  330. public void setClosable(boolean closable) {
  331. this.closable = closable;
  332. if (closable && closeButton == null) {
  333. closeButton = DOM.createSpan();
  334. closeButton.setInnerHTML("×");
  335. closeButton
  336. .setClassName(VTabsheet.CLASSNAME + "-caption-close");
  337. Roles.getTabRole().setAriaHiddenState(closeButton, true);
  338. Roles.getTabRole().setAriaDisabledState(closeButton, true);
  339. getElement().appendChild(closeButton);
  340. } else if (!closable && closeButton != null) {
  341. getElement().removeChild(closeButton);
  342. closeButton = null;
  343. }
  344. if (closable) {
  345. addStyleDependentName("closable");
  346. } else {
  347. removeStyleDependentName("closable");
  348. }
  349. }
  350. public boolean isClosable() {
  351. return closable;
  352. }
  353. @Override
  354. public int getRequiredWidth() {
  355. int width = super.getRequiredWidth();
  356. if (closeButton != null) {
  357. width += WidgetUtil.getRequiredWidth(closeButton);
  358. }
  359. return width;
  360. }
  361. public com.google.gwt.user.client.Element getCloseButton() {
  362. return DOM.asOld(closeButton);
  363. }
  364. }
  365. static class TabBar extends ComplexPanel implements VCloseHandler {
  366. private final Element tr = DOM.createTR();
  367. private final Element spacerTd = DOM.createTD();
  368. private Tab selected;
  369. private VTabsheet tabsheet;
  370. TabBar(VTabsheet tabsheet) {
  371. this.tabsheet = tabsheet;
  372. Element el = DOM.createTable();
  373. Roles.getPresentationRole().set(el);
  374. Element tbody = DOM.createTBody();
  375. DOM.appendChild(el, tbody);
  376. DOM.appendChild(tbody, tr);
  377. setStyleName(spacerTd, CLASSNAME + "-spacertd");
  378. DOM.appendChild(tr, spacerTd);
  379. DOM.appendChild(spacerTd, DOM.createDiv());
  380. setElement(el);
  381. }
  382. @Override
  383. public void onClose(VCloseEvent event) {
  384. Tab tab = event.getTab();
  385. if (!tab.isEnabledOnServer()) {
  386. return;
  387. }
  388. int tabIndex = getWidgetIndex(tab);
  389. getTabsheet().sendTabClosedEvent(tabIndex);
  390. }
  391. protected com.google.gwt.user.client.Element getContainerElement() {
  392. return DOM.asOld(tr);
  393. }
  394. /**
  395. * Gets the number of tabs from the tab bar.
  396. *
  397. * @return the number of tabs from the tab bar.
  398. */
  399. public int getTabCount() {
  400. return getWidgetCount();
  401. }
  402. /**
  403. * Adds a tab to the tab bar.
  404. *
  405. * @return the added tab.
  406. */
  407. public Tab addTab() {
  408. Tab t = new Tab(this);
  409. int tabIndex = getTabCount();
  410. // Logical attach
  411. insert(t, tr, tabIndex, true);
  412. if (tabIndex == 0) {
  413. // Set the "first" style
  414. t.setStyleNames(false, true);
  415. }
  416. getTabsheet().selectionHandler.registerTab(t);
  417. t.setCloseHandler(this);
  418. return t;
  419. }
  420. /**
  421. * Gets the tab sheet instance where the tab bar is attached to.
  422. *
  423. * @return the tab sheet instance where the tab bar is attached to.
  424. */
  425. public VTabsheet getTabsheet() {
  426. return tabsheet;
  427. }
  428. public Tab getTab(int index) {
  429. if (index < 0 || index >= getTabCount()) {
  430. return null;
  431. }
  432. return (Tab) super.getWidget(index);
  433. }
  434. private int getTabIndex(String tabId) {
  435. if (tabId == null) {
  436. return -1;
  437. }
  438. for (int i = 0; i < getTabCount(); i++) {
  439. if (tabId.equals(getTab(i).id)) {
  440. return i;
  441. }
  442. }
  443. return -1;
  444. }
  445. public void selectTab(int index) {
  446. final Tab newSelected = getTab(index);
  447. final Tab oldSelected = selected;
  448. if (oldSelected == newSelected) {
  449. return;
  450. }
  451. newSelected.setStyleNames(true, isFirstVisibleTab(index), true);
  452. newSelected.setTabulatorIndex(getTabsheet().tabulatorIndex);
  453. Roles.getTabRole().setAriaSelectedState(newSelected.getElement(),
  454. SelectedValue.TRUE);
  455. if (oldSelected != null && oldSelected != newSelected) {
  456. oldSelected.setStyleNames(false,
  457. isFirstVisibleTab(getWidgetIndex(oldSelected)));
  458. oldSelected.setTabulatorIndex(-1);
  459. Roles.getTabRole().setAriaSelectedState(
  460. oldSelected.getElement(), SelectedValue.FALSE);
  461. }
  462. // Update the field holding the currently selected tab
  463. selected = newSelected;
  464. // The selected tab might need more (or less) space
  465. newSelected.recalculateCaptionWidth();
  466. getTab(tabsheet.activeTabIndex).recalculateCaptionWidth();
  467. // Scroll the tab into view if it is not already, after layout
  468. Scheduler.get().scheduleFinally(() -> getTabsheet()
  469. .scrollIntoView(getTab(tabsheet.activeTabIndex)));
  470. }
  471. public Tab navigateTab(int fromIndex, int toIndex) {
  472. Tab newNavigated = getTab(toIndex);
  473. if (newNavigated == null) {
  474. throw new IllegalArgumentException(
  475. "Tab at provided index toIndex was not found");
  476. }
  477. Tab oldNavigated = getTab(fromIndex);
  478. newNavigated.setStyleNames(newNavigated.equals(selected),
  479. isFirstVisibleTab(toIndex), true);
  480. if (oldNavigated != null && fromIndex != toIndex) {
  481. oldNavigated.setStyleNames(oldNavigated.equals(selected),
  482. isFirstVisibleTab(fromIndex), false);
  483. }
  484. return newNavigated;
  485. }
  486. public void removeTab(int i) {
  487. Tab tab = getTab(i);
  488. if (tab == null) {
  489. return;
  490. }
  491. remove(tab);
  492. /*
  493. * If this widget was selected we need to unmark it as the last
  494. * selected
  495. */
  496. if (tab == selected) {
  497. selected = null;
  498. }
  499. // FIXME: Shouldn't something be selected instead?
  500. int scrollerIndexCandidate = getTabIndex(
  501. getTabsheet().scrollerPositionTabId);
  502. if (scrollerIndexCandidate < 0) {
  503. // The tab with id scrollerPositionTabId has been removed
  504. scrollerIndexCandidate = getTabsheet().scrollerIndex;
  505. }
  506. scrollerIndexCandidate = selectNewShownTab(scrollerIndexCandidate);
  507. if (scrollerIndexCandidate >= 0
  508. && scrollerIndexCandidate < getTabCount()) {
  509. getTabsheet().scrollIntoView(getTab(scrollerIndexCandidate));
  510. }
  511. }
  512. private int selectNewShownTab(int oldPosition) {
  513. // After removing a tab, find a new scroll position. In most
  514. // cases the scroll position does not change, but if the tab
  515. // at the scroll position was removed, need to find a nearby
  516. // tab that is visible.
  517. for (int i = oldPosition; i < getTabCount(); i++) {
  518. Tab tab = getTab(i);
  519. if (!tab.isHiddenOnServer()) {
  520. return i;
  521. }
  522. }
  523. for (int i = oldPosition - 1; i >= 0; i--) {
  524. Tab tab = getTab(i);
  525. if (!tab.isHiddenOnServer()) {
  526. return i;
  527. }
  528. }
  529. return -1;
  530. }
  531. private boolean isFirstVisibleTab(int index) {
  532. return getFirstVisibleTab() == index;
  533. }
  534. /**
  535. * Returns the index of the first visible tab on the server
  536. */
  537. private int getFirstVisibleTab() {
  538. return getNextVisibleTab(-1);
  539. }
  540. /**
  541. * Find the next visible tab. Returns -1 if none is found.
  542. *
  543. * @param i
  544. * @return
  545. */
  546. private int getNextVisibleTab(int i) {
  547. int tabs = getTabCount();
  548. do {
  549. i++;
  550. } while (i < tabs && getTab(i).isHiddenOnServer());
  551. if (i == tabs) {
  552. return -1;
  553. } else {
  554. return i;
  555. }
  556. }
  557. /**
  558. * Returns the index of the first visible tab in browser.
  559. */
  560. private int getFirstVisibleTabClient() {
  561. int tabs = getTabCount();
  562. int i = 0;
  563. while (i < tabs && !getTab(i).isVisible()) {
  564. i++;
  565. }
  566. if (i == tabs) {
  567. return -1;
  568. } else {
  569. return i;
  570. }
  571. }
  572. /**
  573. * Find the previous visible tab. Returns -1 if none is found.
  574. *
  575. * @param i
  576. * @return
  577. */
  578. private int getPreviousVisibleTab(int i) {
  579. do {
  580. i--;
  581. } while (i >= 0 && getTab(i).isHiddenOnServer());
  582. return i;
  583. }
  584. public int scrollLeft(int currentFirstVisible) {
  585. int prevVisible = getPreviousVisibleTab(currentFirstVisible);
  586. if (prevVisible == -1) {
  587. return -1;
  588. }
  589. Tab newFirst = getTab(prevVisible);
  590. newFirst.setVisible(true);
  591. newFirst.recalculateCaptionWidth();
  592. return prevVisible;
  593. }
  594. public int scrollRight(int currentFirstVisible) {
  595. int nextVisible = getNextVisibleTab(currentFirstVisible);
  596. if (nextVisible == -1) {
  597. return -1;
  598. }
  599. Tab currentFirst = getTab(currentFirstVisible);
  600. currentFirst.setVisible(false);
  601. currentFirst.recalculateCaptionWidth();
  602. return nextVisible;
  603. }
  604. private void recalculateCaptionWidths() {
  605. for (int i = 0; i < getTabCount(); ++i) {
  606. getTab(i).recalculateCaptionWidth();
  607. }
  608. }
  609. }
  610. // TODO using the CLASSNAME directly makes primaryStyleName for TabSheet of
  611. // very limited use - all use of style names should be refactored in the
  612. // future
  613. public static final String CLASSNAME = TabsheetState.PRIMARY_STYLE_NAME;
  614. public static final String TABS_CLASSNAME = CLASSNAME + "-tabcontainer";
  615. public static final String SCROLLER_CLASSNAME = CLASSNAME + "-scroller";
  616. private static final FocusImpl FOCUS_IMPL = FocusImpl
  617. .getFocusImplForPanel();
  618. /** For internal use only. May be removed or replaced in the future. */
  619. // tabbar and 'scroller' container
  620. public final Element tabs;
  621. /**
  622. * The tabindex property (position in the browser's focus cycle.) Named like
  623. * this to avoid confusion with activeTabIndex.
  624. */
  625. int tabulatorIndex = 0;
  626. // tab-scroller element
  627. private final Element scroller;
  628. // tab-scroller next button element
  629. private final Element scrollerNext;
  630. // tab-scroller prev button element
  631. private final Element scrollerPrev;
  632. /**
  633. * The index of the first visible tab (when scrolled)
  634. */
  635. private int scrollerIndex = 0;
  636. /**
  637. * The id of the tab at position scrollerIndex. This is used for keeping the
  638. * scroll position unchanged when a tab is removed from the server side and
  639. * the removed tab lies to the left of the current scroll position. For
  640. * other cases scrollerIndex alone would be sufficient. Since the tab at the
  641. * current scroll position can be removed, scrollerIndex is required in
  642. * addition to this variable.
  643. */
  644. private String scrollerPositionTabId;
  645. final TabBar tb = new TabBar(this);
  646. /** For internal use only. May be removed or replaced in the future. */
  647. protected final VTabsheetPanel tabPanel = new VTabsheetPanel();
  648. /** For internal use only. May be removed or replaced in the future. */
  649. public final Element contentNode;
  650. private final Element deco;
  651. /** For internal use only. May be removed or replaced in the future. */
  652. public boolean waitingForResponse;
  653. private String currentStyle;
  654. /**
  655. * @return Whether the tab could be selected or not.
  656. */
  657. private boolean canSelectTab(final int tabIndex) {
  658. Tab tab = tb.getTab(tabIndex);
  659. if (getApplicationConnection() == null || disabled
  660. || waitingForResponse) {
  661. return false;
  662. }
  663. if (!tab.isEnabledOnServer() || tab.isHiddenOnServer()) {
  664. return false;
  665. }
  666. // Note that we return true when tabIndex == activeTabIndex; the active
  667. // tab could be selected, it's just a no-op.
  668. return true;
  669. }
  670. /**
  671. * Load the content of a tab of the provided index.
  672. *
  673. * @param tabIndex
  674. * The index of the tab to load
  675. *
  676. * @return true if the specified sheet gets loaded, otherwise false.
  677. */
  678. public boolean loadTabSheet(int tabIndex) {
  679. if (activeTabIndex != tabIndex && canSelectTab(tabIndex)) {
  680. tb.selectTab(tabIndex);
  681. activeTabIndex = tabIndex;
  682. addStyleDependentName("loading");
  683. // Hide the current contents so a loading indicator can be shown
  684. // instead
  685. getCurrentlyDisplayedWidget().getElement().getParentElement()
  686. .getStyle().setVisibility(Visibility.HIDDEN);
  687. getRpcProxy().setSelected(tabKeys.get(tabIndex));
  688. waitingForResponse = true;
  689. tb.getTab(tabIndex).focus(); // move keyboard focus to active tab
  690. return true;
  691. }
  692. return false;
  693. }
  694. /**
  695. * Returns the currently displayed widget in the tab panel.
  696. *
  697. * @since 7.2
  698. * @return currently displayed content widget
  699. */
  700. public Widget getCurrentlyDisplayedWidget() {
  701. return tabPanel.getWidget(tabPanel.getVisibleWidget());
  702. }
  703. /**
  704. * Returns the client to server RPC proxy for the tabsheet.
  705. *
  706. * @since 7.2
  707. * @return RPC proxy
  708. */
  709. protected TabsheetServerRpc getRpcProxy() {
  710. return connector.getRpcProxy(TabsheetServerRpc.class);
  711. }
  712. /**
  713. * For internal use only.
  714. *
  715. * Avoid using this method directly and use appropriate superclass methods
  716. * where applicable.
  717. *
  718. * @deprecated since 7.2 - use more specific methods instead (getRpcProxy(),
  719. * getConnectorForWidget(Widget) etc.)
  720. * @return ApplicationConnection
  721. */
  722. @Deprecated
  723. public ApplicationConnection getApplicationConnection() {
  724. return client;
  725. }
  726. private VTooltip getVTooltip() {
  727. return getApplicationConnection().getVTooltip();
  728. }
  729. public void tabSizeMightHaveChanged(Tab tab) {
  730. // icon onloads may change total width of tabsheet
  731. if (isDynamicWidth()) {
  732. updateDynamicWidth();
  733. }
  734. updateTabScroller();
  735. }
  736. void sendTabClosedEvent(int tabIndex) {
  737. getRpcProxy().closeTab(tabKeys.get(tabIndex));
  738. }
  739. public VTabsheet() {
  740. super(CLASSNAME);
  741. // Tab scrolling
  742. getElement().getStyle().setOverflow(Overflow.HIDDEN);
  743. tabs = DOM.createDiv();
  744. DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
  745. Roles.getTablistRole().set(tabs);
  746. Roles.getTablistRole().setAriaLiveProperty(tabs, LiveValue.OFF);
  747. scroller = DOM.createDiv();
  748. Roles.getTablistRole().setAriaHiddenState(scroller, true);
  749. DOM.setElementProperty(scroller, "className", SCROLLER_CLASSNAME);
  750. scrollerPrev = DOM.createButton();
  751. scrollerPrev.setTabIndex(-1);
  752. DOM.setElementProperty(scrollerPrev, "className",
  753. SCROLLER_CLASSNAME + "Prev");
  754. Roles.getTablistRole().setAriaHiddenState(scrollerPrev, true);
  755. DOM.sinkEvents(scrollerPrev, Event.ONCLICK | Event.ONMOUSEDOWN);
  756. scrollerNext = DOM.createButton();
  757. scrollerNext.setTabIndex(-1);
  758. DOM.setElementProperty(scrollerNext, "className",
  759. SCROLLER_CLASSNAME + "Next");
  760. Roles.getTablistRole().setAriaHiddenState(scrollerNext, true);
  761. DOM.sinkEvents(scrollerNext, Event.ONCLICK | Event.ONMOUSEDOWN);
  762. DOM.appendChild(getElement(), tabs);
  763. // Tabs
  764. tabPanel.setStyleName(CLASSNAME + "-tabsheetpanel");
  765. contentNode = DOM.createDiv();
  766. Roles.getTabpanelRole().set(contentNode);
  767. deco = DOM.createDiv();
  768. tb.setStyleName(CLASSNAME + "-tabs");
  769. DOM.setElementProperty(contentNode, "className",
  770. CLASSNAME + "-content");
  771. DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
  772. add(tb, tabs);
  773. DOM.appendChild(scroller, scrollerPrev);
  774. DOM.appendChild(scroller, scrollerNext);
  775. DOM.appendChild(getElement(), contentNode);
  776. add(tabPanel, contentNode);
  777. DOM.appendChild(getElement(), deco);
  778. DOM.appendChild(tabs, scroller);
  779. // TODO Use for Safari only. Fix annoying 1px first cell in TabBar.
  780. // DOM.setStyleAttribute(DOM.getFirstChild(DOM.getFirstChild(DOM
  781. // .getFirstChild(tb.getElement()))), "display", "none");
  782. }
  783. @Override
  784. public void onBrowserEvent(Event event) {
  785. com.google.gwt.dom.client.Element eventTarget = DOM
  786. .eventGetTarget(event);
  787. if (event.getTypeInt() == Event.ONCLICK) {
  788. // Tab scrolling
  789. if (eventTarget == scrollerPrev || eventTarget == scrollerNext) {
  790. scrollAccordingToScrollTarget(eventTarget);
  791. event.stopPropagation();
  792. }
  793. } else if (event.getTypeInt() == Event.ONMOUSEDOWN) {
  794. if (eventTarget == scrollerPrev || eventTarget == scrollerNext) {
  795. // In case the focus was previously on a Tab, we need to cancel
  796. // the upcoming blur on the Tab which will follow this mouse
  797. // down event.
  798. focusBlurManager.cancelNextBlurSchedule();
  799. return;
  800. }
  801. }
  802. super.onBrowserEvent(event);
  803. }
  804. /*
  805. * Scroll the tab bar according to the last scrollTarget (the scroll button
  806. * pressed).
  807. */
  808. private void scrollAccordingToScrollTarget(
  809. com.google.gwt.dom.client.Element scrollTarget) {
  810. if (scrollTarget == null) {
  811. return;
  812. }
  813. int newFirstIndex = -1;
  814. // Scroll left.
  815. if (isScrolledTabs() && scrollTarget == scrollerPrev) {
  816. newFirstIndex = tb.scrollLeft(scrollerIndex);
  817. // Scroll right.
  818. } else if (isClippedTabs() && scrollTarget == scrollerNext) {
  819. newFirstIndex = tb.scrollRight(scrollerIndex);
  820. }
  821. if (newFirstIndex != -1) {
  822. scrollerIndex = newFirstIndex;
  823. scrollerPositionTabId = tb.getTab(scrollerIndex).id;
  824. updateTabScroller();
  825. }
  826. // For this to work well, make sure the method gets called only from
  827. // user events.
  828. selectionHandler.focusTabAtIndex(scrollerIndex);
  829. }
  830. /**
  831. * Checks if the tab with the selected index has been scrolled out of the
  832. * view (on the left side).
  833. *
  834. * @param index
  835. * @return
  836. */
  837. private boolean scrolledOutOfView(int index) {
  838. return scrollerIndex > index;
  839. }
  840. /** For internal use only. May be removed or replaced in the future. */
  841. public void handleStyleNames(AbstractComponentState state) {
  842. // Add proper stylenames for all elements (easier to prevent unwanted
  843. // style inheritance)
  844. if (ComponentStateUtil.hasStyles(state)) {
  845. final List<String> styles = state.styles;
  846. if (currentStyle == null
  847. || !currentStyle.equals(styles.toString())) {
  848. currentStyle = styles.toString();
  849. final String tabsBaseClass = TABS_CLASSNAME;
  850. String tabsClass = tabsBaseClass;
  851. final String contentBaseClass = CLASSNAME + "-content";
  852. String contentClass = contentBaseClass;
  853. final String decoBaseClass = CLASSNAME + "-deco";
  854. String decoClass = decoBaseClass;
  855. for (String style : styles) {
  856. tb.addStyleDependentName(style);
  857. tabsClass += " " + tabsBaseClass + "-" + style;
  858. contentClass += " " + contentBaseClass + "-" + style;
  859. decoClass += " " + decoBaseClass + "-" + style;
  860. }
  861. DOM.setElementProperty(tabs, "className", tabsClass);
  862. DOM.setElementProperty(contentNode, "className", contentClass);
  863. DOM.setElementProperty(deco, "className", decoClass);
  864. }
  865. } else {
  866. tb.setStyleName(CLASSNAME + "-tabs");
  867. DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
  868. DOM.setElementProperty(contentNode, "className",
  869. CLASSNAME + "-content");
  870. DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
  871. }
  872. }
  873. /** For internal use only. May be removed or replaced in the future. */
  874. public void updateDynamicWidth() {
  875. // Find width consumed by tabs
  876. TableCellElement spacerCell = ((TableElement) tb.getElement().cast())
  877. .getRows().getItem(0).getCells().getItem(tb.getTabCount());
  878. int spacerWidth = spacerCell.getOffsetWidth();
  879. DivElement div = (DivElement) spacerCell.getFirstChildElement();
  880. int spacerMinWidth = spacerCell.getOffsetWidth() - div.getOffsetWidth();
  881. int tabsWidth = tb.getOffsetWidth() - spacerWidth + spacerMinWidth;
  882. // Find content width
  883. Style style = tabPanel.getElement().getStyle();
  884. String overflow = style.getProperty("overflow");
  885. style.setProperty("overflow", "hidden");
  886. style.setPropertyPx("width", tabsWidth);
  887. boolean hasTabs = tabPanel.getWidgetCount() > 0;
  888. Style wrapperstyle = null;
  889. if (hasTabs) {
  890. wrapperstyle = getCurrentlyDisplayedWidget().getElement()
  891. .getParentElement().getStyle();
  892. wrapperstyle.setPropertyPx("width", tabsWidth);
  893. }
  894. // Get content width from actual widget
  895. int contentWidth = 0;
  896. if (hasTabs) {
  897. contentWidth = getCurrentlyDisplayedWidget().getOffsetWidth();
  898. }
  899. style.setProperty("overflow", overflow);
  900. // Set widths to max(tabs,content)
  901. if (tabsWidth < contentWidth) {
  902. tabsWidth = contentWidth;
  903. }
  904. int outerWidth = tabsWidth + getContentAreaBorderWidth();
  905. tabs.getStyle().setPropertyPx("width", outerWidth);
  906. style.setPropertyPx("width", tabsWidth);
  907. if (hasTabs) {
  908. wrapperstyle.setPropertyPx("width", tabsWidth);
  909. }
  910. contentNode.getStyle().setPropertyPx("width", tabsWidth);
  911. super.setWidth(outerWidth + "px");
  912. updateOpenTabSize();
  913. }
  914. private boolean isAllTabsBeforeIndexInvisible() {
  915. boolean invisible = true;
  916. for (int i = 0; i < scrollerIndex; i++) {
  917. invisible = invisible & !tb.getTab(i).isVisible();
  918. }
  919. return invisible;
  920. }
  921. private boolean isScrollerPrevDisabled() {
  922. return scrollerPrev.getClassName()
  923. .contains(PREV_SCROLLER_DISABLED_CLASSNAME);
  924. }
  925. private boolean isScrollerHidden() {
  926. return scroller.getStyle().getDisplay()
  927. .equals(Display.NONE.getCssName());
  928. }
  929. private boolean isIndexSkippingHiddenTabs() {
  930. return isAllTabsBeforeIndexInvisible()
  931. && (isScrollerPrevDisabled() || isScrollerHidden());
  932. }
  933. @Override
  934. public void renderTab(final TabState tabState, int index) {
  935. Tab tab = tb.getTab(index);
  936. if (tab == null) {
  937. tab = tb.addTab();
  938. }
  939. tab.updateFromState(tabState);
  940. tab.setEnabledOnServer((!disabledTabKeys.contains(tabKeys.get(index))));
  941. tab.setHiddenOnServer(!tabState.visible);
  942. if (scrolledOutOfView(index) && !isIndexSkippingHiddenTabs()) {
  943. // Should not set tabs visible if they are scrolled out of view
  944. tab.setVisible(false);
  945. } else {
  946. // When the tab was hidden and then turned visible again
  947. // and there is space for it, it should be in view (#17096) (#17333)
  948. if (isTabSetVisibleBeforeScroller(tabState, index, tab)) {
  949. scrollerIndex = index;
  950. tab.setVisible(true);
  951. tab.setStyleNames(false, true);
  952. // scroll to the currently selected tab if it got clipped
  953. // after making another tab visible
  954. if (isClippedTabs()) {
  955. scrollIntoView(getActiveTab());
  956. }
  957. } else {
  958. tab.setVisible(tabState.visible);
  959. }
  960. }
  961. /*
  962. * Force the width of the caption container so the content will not wrap
  963. * and tabs won't be too narrow in certain browsers
  964. */
  965. tab.recalculateCaptionWidth();
  966. }
  967. /**
  968. * Checks whether the tab has been set to visible and the scroller is at the
  969. * first visible tab. That means that the scroller has to be adjusted so
  970. * that the tab is visible again.
  971. */
  972. private boolean isTabSetVisibleBeforeScroller(TabState tabState, int index,
  973. Tab tab) {
  974. return isIndexSkippingHiddenTabs() && isScrollerAtFirstVisibleTab()
  975. && hasTabChangedVisibility(tabState, tab)
  976. && scrolledOutOfView(index);
  977. }
  978. /**
  979. * Checks whether the tab is visible on server but is not visible on client
  980. * yet.
  981. */
  982. private boolean hasTabChangedVisibility(TabState tabState, Tab tab) {
  983. return !tab.isVisible() && tabState.visible;
  984. }
  985. private boolean isScrollerAtFirstVisibleTab() {
  986. return tb.getFirstVisibleTabClient() == scrollerIndex;
  987. }
  988. /**
  989. * @deprecated as of 7.1, VTabsheet only keeps the active tab in the DOM
  990. * without any place holders.
  991. */
  992. @Deprecated
  993. public class PlaceHolder extends VLabel {
  994. public PlaceHolder() {
  995. super("");
  996. }
  997. }
  998. /**
  999. * Renders the widget content for a tab sheet.
  1000. *
  1001. * @param newWidget
  1002. */
  1003. public void renderContent(Widget newWidget) {
  1004. assert tabPanel.getWidgetCount() <= 1;
  1005. if (null == newWidget) {
  1006. newWidget = new SimplePanel();
  1007. }
  1008. if (tabPanel.getWidgetCount() == 0) {
  1009. tabPanel.add(newWidget);
  1010. } else if (tabPanel.getWidget(0) != newWidget) {
  1011. tabPanel.remove(0);
  1012. tabPanel.add(newWidget);
  1013. }
  1014. assert tabPanel.getWidgetCount() <= 1;
  1015. // There's never any other index than 0, but maintaining API for now
  1016. tabPanel.showWidget(0);
  1017. VTabsheet.this.iLayout();
  1018. updateOpenTabSize();
  1019. VTabsheet.this.removeStyleDependentName("loading");
  1020. }
  1021. /**
  1022. * Recalculates the sizes of tab captions, causing the tabs to be rendered
  1023. * the correct size.
  1024. */
  1025. private void updateTabCaptionSizes() {
  1026. for (int tabIx = 0; tabIx < tb.getTabCount(); tabIx++) {
  1027. tb.getTab(tabIx).recalculateCaptionWidth();
  1028. }
  1029. }
  1030. /** For internal use only. May be removed or replaced in the future. */
  1031. public void updateContentNodeHeight() {
  1032. if (!isDynamicHeight()) {
  1033. int contentHeight = getOffsetHeight();
  1034. contentHeight -= deco.getOffsetHeight();
  1035. contentHeight -= tb.getOffsetHeight();
  1036. ComputedStyle cs = new ComputedStyle(contentNode);
  1037. contentHeight -= Math.ceil(cs.getPaddingHeight());
  1038. contentHeight -= Math.ceil(cs.getBorderHeight());
  1039. if (contentHeight < 0) {
  1040. contentHeight = 0;
  1041. }
  1042. // Set proper values for content element
  1043. contentNode.getStyle().setHeight(contentHeight, Unit.PX);
  1044. } else {
  1045. contentNode.getStyle().clearHeight();
  1046. }
  1047. }
  1048. /**
  1049. * Run internal layouting.
  1050. */
  1051. public void iLayout() {
  1052. updateTabScroller();
  1053. updateTabCaptionSizes();
  1054. }
  1055. /**
  1056. * Sets the size of the visible tab (component). As the tab is set to
  1057. * position: absolute (to work around a firefox flickering bug) we must keep
  1058. * this up-to-date by hand.
  1059. * <p>
  1060. * For internal use only. May be removed or replaced in the future.
  1061. */
  1062. public void updateOpenTabSize() {
  1063. /*
  1064. * The overflow=auto element must have a height specified, otherwise it
  1065. * will be just as high as the contents and no scrollbars will appear
  1066. */
  1067. int height = -1;
  1068. int width = -1;
  1069. int minWidth = 0;
  1070. if (!isDynamicHeight()) {
  1071. height = contentNode.getOffsetHeight();
  1072. }
  1073. if (!isDynamicWidth()) {
  1074. width = contentNode.getOffsetWidth() - getContentAreaBorderWidth();
  1075. } else {
  1076. /*
  1077. * If the tabbar is wider than the content we need to use the tabbar
  1078. * width as minimum width so scrollbars get placed correctly (at the
  1079. * right edge).
  1080. */
  1081. minWidth = tb.getOffsetWidth() - getContentAreaBorderWidth();
  1082. }
  1083. tabPanel.fixVisibleTabSize(width, height, minWidth);
  1084. }
  1085. /**
  1086. * Layouts the tab-scroller elements, and applies styles.
  1087. */
  1088. private void updateTabScroller() {
  1089. if (!isDynamicWidth()) {
  1090. tabs.getStyle().setWidth(100, Unit.PCT);
  1091. }
  1092. // Make sure scrollerIndex is valid
  1093. if (scrollerIndex < 0 || scrollerIndex > tb.getTabCount()) {
  1094. scrollerIndex = tb.getFirstVisibleTab();
  1095. } else if (tb.getTabCount() > 0
  1096. && tb.getTab(scrollerIndex).isHiddenOnServer()) {
  1097. scrollerIndex = tb.getNextVisibleTab(scrollerIndex);
  1098. }
  1099. boolean scrolled = isScrolledTabs();
  1100. boolean clipped = isClippedTabs();
  1101. if (tb.getTabCount() > 0 && tb.isVisible() && (scrolled || clipped)) {
  1102. scroller.getStyle().clearDisplay();
  1103. DOM.setElementProperty(scrollerPrev, "className", SCROLLER_CLASSNAME
  1104. + (scrolled ? "Prev" : PREV_SCROLLER_DISABLED_CLASSNAME));
  1105. DOM.setElementProperty(scrollerNext, "className",
  1106. SCROLLER_CLASSNAME + (clipped ? "Next" : "Next-disabled"));
  1107. // the active tab should be focusable if and only if it is visible
  1108. boolean isActiveTabVisible = scrollerIndex <= activeTabIndex
  1109. && !isClipped(tb.selected);
  1110. tb.selected.setTabulatorIndex(
  1111. isActiveTabVisible ? tabulatorIndex : -1);
  1112. } else {
  1113. scroller.getStyle().setDisplay(Display.NONE);
  1114. }
  1115. if (BrowserInfo.get().isSafariOrIOS()) {
  1116. /*
  1117. * another hack for webkits. tabscroller sometimes drops without
  1118. * "shaking it" reproducable in
  1119. * com.vaadin.tests.components.tabsheet.TabSheetIcons
  1120. */
  1121. final Style style = scroller.getStyle();
  1122. style.setProperty("whiteSpace", "normal");
  1123. Scheduler.get().scheduleDeferred(
  1124. () -> style.setProperty("whiteSpace", ""));
  1125. }
  1126. }
  1127. /** For internal use only. May be removed or replaced in the future. */
  1128. public void showAllTabs() {
  1129. scrollerIndex = tb.getFirstVisibleTab();
  1130. scrollerPositionTabId = scrollerIndex < 0 ? null
  1131. : tb.getTab(scrollerIndex).id;
  1132. for (int i = 0; i < tb.getTabCount(); i++) {
  1133. Tab t = tb.getTab(i);
  1134. if (!t.isHiddenOnServer()) {
  1135. t.setVisible(true);
  1136. }
  1137. }
  1138. }
  1139. private boolean isScrolledTabs() {
  1140. return scrollerIndex > tb.getFirstVisibleTab();
  1141. }
  1142. private boolean isClippedTabs() {
  1143. return (tb.getOffsetWidth() - DOM.getElementPropertyInt(
  1144. (Element) tb.getContainerElement().getLastChild().cast(),
  1145. "offsetWidth")) > getOffsetWidth()
  1146. - (isScrolledTabs() ? scroller.getOffsetWidth() : 0);
  1147. }
  1148. private boolean isClipped(Tab tab) {
  1149. return tab.getAbsoluteLeft() + tab.getOffsetWidth() > getAbsoluteLeft()
  1150. + getOffsetWidth() - scroller.getOffsetWidth();
  1151. }
  1152. @Override
  1153. protected void clearPaintables() {
  1154. int i = tb.getTabCount();
  1155. while (i > 0) {
  1156. tb.removeTab(--i);
  1157. }
  1158. tabPanel.clear();
  1159. }
  1160. @Override
  1161. public Iterator<Widget> getWidgetIterator() {
  1162. return tabPanel.iterator();
  1163. }
  1164. /** For internal use only. May be removed or replaced in the future. */
  1165. public int getContentAreaBorderWidth() {
  1166. return WidgetUtil.measureHorizontalBorder(contentNode);
  1167. }
  1168. @Override
  1169. public int getTabCount() {
  1170. return tb.getTabCount();
  1171. }
  1172. @Override
  1173. public ComponentConnector getTab(int index) {
  1174. if (tabPanel.getWidgetCount() > index) {
  1175. Widget widget = tabPanel.getWidget(index);
  1176. return getConnectorForWidget(widget);
  1177. }
  1178. return null;
  1179. }
  1180. @Override
  1181. public void removeTab(int index) {
  1182. tb.removeTab(index);
  1183. // Removing content from tp is handled by the connector
  1184. }
  1185. @Override
  1186. public void selectTab(int index) {
  1187. tb.selectTab(index);
  1188. }
  1189. @Override
  1190. public void focus() {
  1191. getActiveTab().focus();
  1192. }
  1193. public void blur() {
  1194. getActiveTab().blur();
  1195. }
  1196. /*
  1197. * Gets the active tab.
  1198. */
  1199. private Tab getActiveTab() {
  1200. return tb.getTab(activeTabIndex);
  1201. }
  1202. @Override
  1203. public void setConnector(AbstractComponentConnector connector) {
  1204. super.setConnector(connector);
  1205. focusBlurManager.connector = connector;
  1206. }
  1207. /*
  1208. * The focus and blur manager instance.
  1209. */
  1210. private FocusBlurManager focusBlurManager = new FocusBlurManager();
  1211. /*
  1212. * Generate the correct focus/blur events for the main TabSheet component
  1213. * (#14304).
  1214. *
  1215. * The TabSheet must fire one focus event when the user clicks on the tab
  1216. * bar (i.e. inner TabBar class) containing the Tabs or when the focus is
  1217. * provided to the TabSheet by any means. Also one blur event should be
  1218. * fired only when the user leaves the tab bar. After the user focus on the
  1219. * tab bar and before leaving it, no matter how many times he's pressing the
  1220. * Tabs or the scroll buttons, the TabSheet component should not fire any of
  1221. * those blur/focus events.
  1222. *
  1223. * The only focusable elements contained in the tab bar are the Tabs (see
  1224. * inner class Tab). The reason is the accessibility support.
  1225. *
  1226. * Having this in mind, the chosen solution path for our problem is to match
  1227. * a sequence of focus/blur events on the tabs, choose only the first focus
  1228. * and last blur events and pass only those further to the main component.
  1229. * Any consecutive blur/focus events on 2 Tabs must be ignored.
  1230. *
  1231. * Because in a blur event we don't know whether or not a focus will follow,
  1232. * we just defer a command initiated on the blur event to wait and see if
  1233. * any focus will appear. The command will be executed after the next focus,
  1234. * so if no focus was triggered in the mean while it'll submit the blur
  1235. * event to the main component, otherwise it'll do nothing, so the main
  1236. * component will not generate the blur..
  1237. */
  1238. private static class FocusBlurManager {
  1239. // The real tab with focus on it. If the focus goes to another element
  1240. // in the page this will be null.
  1241. private Tab focusedTab;
  1242. /*
  1243. * Gets the focused tab.
  1244. */
  1245. private Tab getFocusedTab() {
  1246. return focusedTab;
  1247. }
  1248. /*
  1249. * Sets the local field tracking the focused tab.
  1250. */
  1251. private void setFocusedTab(Tab focusedTab) {
  1252. this.focusedTab = focusedTab;
  1253. }
  1254. /*
  1255. * The ultimate focus/blur event dispatcher.
  1256. */
  1257. private AbstractComponentConnector connector;
  1258. /**
  1259. * Delegate method for the onFocus event occurring on Tab.
  1260. *
  1261. * @since 7.2.6
  1262. * @param newFocusTab
  1263. * the new focused tab.
  1264. * @see #onBlur(Tab)
  1265. */
  1266. public void onFocus(Tab newFocusTab) {
  1267. if (connector.hasEventListener(EventId.FOCUS)) {
  1268. // Send the focus event only first time when we focus on any
  1269. // tab. The focused tab will be reseted on the last blur.
  1270. if (focusedTab == null) {
  1271. connector.getRpcProxy(FocusAndBlurServerRpc.class).focus();
  1272. }
  1273. }
  1274. cancelLastBlurSchedule();
  1275. setFocusedTab(newFocusTab);
  1276. }
  1277. /**
  1278. * Delegate method for the onBlur event occurring on Tab.
  1279. *
  1280. * @param blurSource
  1281. * the source of the blur.
  1282. *
  1283. * @see #onFocus(Tab)
  1284. */
  1285. public void onBlur(Tab blurSource) {
  1286. if (focusedTab != null && focusedTab == blurSource) {
  1287. if (connector.hasEventListener(EventId.BLUR)) {
  1288. scheduleBlur(focusedTab);
  1289. }
  1290. }
  1291. }
  1292. /*
  1293. * The last blur command to be executed.
  1294. */
  1295. private BlurCommand blurCommand;
  1296. /*
  1297. * Execute the final blur command.
  1298. */
  1299. private class BlurCommand implements Command {
  1300. /*
  1301. * The blur source.
  1302. */
  1303. private Tab blurSource;
  1304. /**
  1305. * Create the blur command using the blur source.
  1306. *
  1307. * @param blurSource
  1308. * the source.
  1309. * @param focusedTabProvider
  1310. * provides the current focused tab.
  1311. */
  1312. public BlurCommand(Tab blurSource) {
  1313. this.blurSource = blurSource;
  1314. }
  1315. /**
  1316. * Stop the command from being executed.
  1317. *
  1318. * @since 7.4
  1319. */
  1320. public void stopSchedule() {
  1321. blurSource = null;
  1322. }
  1323. /**
  1324. * Schedule the command for a deferred execution.
  1325. *
  1326. * @since 7.4
  1327. */
  1328. public void scheduleDeferred() {
  1329. Scheduler.get().scheduleDeferred(this);
  1330. }
  1331. @Override
  1332. public void execute() {
  1333. Tab focusedTab = getFocusedTab();
  1334. if (blurSource == null) {
  1335. return;
  1336. }
  1337. // The focus didn't change since this blur triggered, so
  1338. // the new focused element is not a tab.
  1339. if (focusedTab == blurSource) {
  1340. // We're certain there's no focus anymore.
  1341. focusedTab.removeAssistiveDescription();
  1342. setFocusedTab(null);
  1343. connector.getRpcProxy(FocusAndBlurServerRpc.class).blur();
  1344. }
  1345. // Call this to set it to null and be consistent.
  1346. cancelLastBlurSchedule();
  1347. }
  1348. }
  1349. /*
  1350. * Schedule a new blur event for a deferred execution.
  1351. */
  1352. private void scheduleBlur(Tab blurSource) {
  1353. if (nextBlurScheduleCancelled) {
  1354. // This will set the stopNextBlurCommand back to false as well.
  1355. cancelLastBlurSchedule();
  1356. // Reset the status.
  1357. nextBlurScheduleCancelled = false;
  1358. return;
  1359. }
  1360. cancelLastBlurSchedule();
  1361. blurCommand = new BlurCommand(blurSource);
  1362. blurCommand.scheduleDeferred();
  1363. }
  1364. /**
  1365. * Remove the last blur deferred command from execution.
  1366. */
  1367. public void cancelLastBlurSchedule() {
  1368. if (blurCommand != null) {
  1369. blurCommand.stopSchedule();
  1370. blurCommand = null;
  1371. }
  1372. // We really want to make sure this flag gets reseted at any time
  1373. // when something interact with the blur manager and ther's no blur
  1374. // command scheduled (as we just canceled it).
  1375. nextBlurScheduleCancelled = false;
  1376. }
  1377. /**
  1378. * Cancel the next scheduled execution. This method must be called only
  1379. * from an event occurring before the onBlur event. It's the case of IE
  1380. * which doesn't trigger the focus event, so we're using this approach
  1381. * to cancel the next blur event prior it's execution, calling the
  1382. * method from mouse down event.
  1383. */
  1384. public void cancelNextBlurSchedule() {
  1385. // Make sure there's still no other command to be executed.
  1386. cancelLastBlurSchedule();
  1387. nextBlurScheduleCancelled = true;
  1388. }
  1389. /*
  1390. * Flag that the next deferred command won't get executed. This is
  1391. * useful in case of IE where the user focus event don't fire and we're
  1392. * using the mouse down event to track the focus. But the mouse down
  1393. * event triggers before the blur, so we need to cancel the deferred
  1394. * execution in advance.
  1395. */
  1396. private boolean nextBlurScheduleCancelled = false;
  1397. }
  1398. /*
  1399. * The tabs selection handler instance.
  1400. */
  1401. private final TabSelectionHandler selectionHandler = new TabSelectionHandler();
  1402. /*
  1403. * Handle the events for selecting the tabs.
  1404. */
  1405. private class TabSelectionHandler implements FocusHandler, BlurHandler,
  1406. KeyDownHandler, ClickHandler, MouseDownHandler {
  1407. /** For internal use only. May be removed or replaced in the future. */
  1408. // The current visible focused index.
  1409. private int focusedTabIndex = 0;
  1410. /**
  1411. * Register the tab to the selection handler.
  1412. *
  1413. * @param tab
  1414. * the tab to register.
  1415. */
  1416. public void registerTab(Tab tab) {
  1417. tab.addBlurHandler(this);
  1418. tab.addFocusHandler(this);
  1419. tab.addKeyDownHandler(this);
  1420. tab.addClickHandler(this);
  1421. tab.addMouseDownHandler(this);
  1422. }
  1423. @Override
  1424. public void onBlur(final BlurEvent event) {
  1425. getVTooltip().hideTooltip();
  1426. Object blurSource = event.getSource();
  1427. if (blurSource instanceof Tab) {
  1428. focusBlurManager.onBlur((Tab) blurSource);
  1429. }
  1430. }
  1431. @Override
  1432. public void onFocus(FocusEvent event) {
  1433. if (event.getSource() instanceof Tab) {
  1434. Tab focusSource = (Tab) event.getSource();
  1435. focusBlurManager.onFocus(focusSource);
  1436. if (focusSource.hasTooltip()) {
  1437. focusSource.setAssistiveDescription(
  1438. getVTooltip().getUniqueId());
  1439. getVTooltip().showAssistive(focusSource.getTooltipInfo());
  1440. }
  1441. }
  1442. }
  1443. @Override
  1444. public void onClick(ClickEvent event) {
  1445. // IE doesn't trigger focus when click, so we need to make sure
  1446. // the previous blur deferred command will get killed.
  1447. focusBlurManager.cancelLastBlurSchedule();
  1448. TabCaption caption = (TabCaption) event.getSource();
  1449. Element targetElement = event.getNativeEvent().getEventTarget()
  1450. .cast();
  1451. // the tab should not be focused if the close button was clicked
  1452. if (targetElement == caption.getCloseButton()) {
  1453. return;
  1454. }
  1455. int index = tb.getWidgetIndex(caption.getParent());
  1456. tb.navigateTab(focusedTabIndex, index);
  1457. focusedTabIndex = index;
  1458. if (!loadTabSheet(index)) {
  1459. // This needs to be called at the end, as the activeTabIndex
  1460. // is set in the loadTabSheet.
  1461. focus();
  1462. }
  1463. }
  1464. @Override
  1465. public void onMouseDown(MouseDownEvent event) {
  1466. if (event.getSource() instanceof Tab) {
  1467. // IE doesn't trigger focus when click, so we need to make sure
  1468. // the
  1469. // next blur deferred command will get killed.
  1470. focusBlurManager.cancelNextBlurSchedule();
  1471. }
  1472. }
  1473. @Override
  1474. public void onKeyDown(KeyDownEvent event) {
  1475. if (event.getSource() instanceof Tab) {
  1476. int keycode = event.getNativeEvent().getKeyCode();
  1477. if (!event.isAnyModifierKeyDown()) {
  1478. if (keycode == getPreviousTabKey()) {
  1479. selectPreviousTab();
  1480. event.stopPropagation();
  1481. } else if (keycode == getNextTabKey()) {
  1482. selectNextTab();
  1483. event.stopPropagation();
  1484. } else if (keycode == getCloseTabKey()) {
  1485. Tab tab = tb.getTab(activeTabIndex);
  1486. if (tab.isClosable()) {
  1487. tab.onClose();
  1488. }
  1489. } else if (keycode == getSelectTabKey()) {
  1490. loadTabSheet(focusedTabIndex);
  1491. // Prevent the page from scrolling when hitting space
  1492. // (select key) to select the current tab.
  1493. event.preventDefault();
  1494. }
  1495. }
  1496. }
  1497. }
  1498. /*
  1499. * Left arrow key selection.
  1500. */
  1501. private void selectPreviousTab() {
  1502. int newTabIndex = focusedTabIndex;
  1503. // Find the previous visible and enabled tab if any.
  1504. do {
  1505. newTabIndex--;
  1506. } while (newTabIndex >= 0 && !canSelectTab(newTabIndex));
  1507. if (newTabIndex >= 0) {
  1508. keySelectTab(newTabIndex);
  1509. }
  1510. }
  1511. /*
  1512. * Right arrow key selection.
  1513. */
  1514. private void selectNextTab() {
  1515. int newTabIndex = focusedTabIndex;
  1516. // Find the next visible and enabled tab if any.
  1517. do {
  1518. newTabIndex++;
  1519. } while (newTabIndex < getTabCount() && !canSelectTab(newTabIndex));
  1520. if (newTabIndex < getTabCount()) {
  1521. keySelectTab(newTabIndex);
  1522. }
  1523. }
  1524. /*
  1525. * Select the specified tab using left/right key.
  1526. */
  1527. private void keySelectTab(int newTabIndex) {
  1528. Tab tab = tb.getTab(newTabIndex);
  1529. if (tab == null) {
  1530. return;
  1531. }
  1532. // Focus the tab, otherwise the selected one will loose focus and
  1533. // TabSheet will get blurred.
  1534. focusTabAtIndex(newTabIndex);
  1535. tb.navigateTab(focusedTabIndex, newTabIndex);
  1536. focusedTabIndex = newTabIndex;
  1537. }
  1538. /**
  1539. * Focus the specified tab. Make sure to call this only from user
  1540. * events, otherwise will break things.
  1541. *
  1542. * @param tabIndex
  1543. * the index of the tab to set.
  1544. */
  1545. void focusTabAtIndex(int tabIndex) {
  1546. Tab tabToFocus = tb.getTab(tabIndex);
  1547. if (tabToFocus != null) {
  1548. tabToFocus.focus();
  1549. }
  1550. }
  1551. }
  1552. /**
  1553. * @return The key code of the keyboard shortcut that selects the previous
  1554. * tab in a focused tabsheet.
  1555. */
  1556. protected int getPreviousTabKey() {
  1557. return KeyCodes.KEY_LEFT;
  1558. }
  1559. /**
  1560. * Gets the key to activate the selected tab when navigating using
  1561. * previous/next (left/right) keys.
  1562. *
  1563. * @return the key to activate the selected tab.
  1564. *
  1565. * @see #getNextTabKey()
  1566. * @see #getPreviousTabKey()
  1567. */
  1568. protected int getSelectTabKey() {
  1569. return KeyCodes.KEY_SPACE;
  1570. }
  1571. /**
  1572. * @return The key code of the keyboard shortcut that selects the next tab
  1573. * in a focused tabsheet.
  1574. */
  1575. protected int getNextTabKey() {
  1576. return KeyCodes.KEY_RIGHT;
  1577. }
  1578. /**
  1579. * @return The key code of the keyboard shortcut that closes the currently
  1580. * selected tab in a focused tabsheet.
  1581. */
  1582. protected int getCloseTabKey() {
  1583. return KeyCodes.KEY_DELETE;
  1584. }
  1585. private void scrollIntoView(Tab tab) {
  1586. if (!tab.isHiddenOnServer()) {
  1587. // Check for visibility first as clipped tabs to the right are
  1588. // always visible.
  1589. // On IE8 a tab with false visibility would have the bounds of the
  1590. // full TabBar.
  1591. if (!tab.isVisible()) {
  1592. while (!tab.isVisible()) {
  1593. scrollerIndex = tb.scrollLeft(scrollerIndex);
  1594. }
  1595. updateTabScroller();
  1596. } else if (isClipped(tab)) {
  1597. while (isClipped(tab) && scrollerIndex != -1) {
  1598. scrollerIndex = tb.scrollRight(scrollerIndex);
  1599. }
  1600. updateTabScroller();
  1601. }
  1602. if (scrollerIndex >= 0 && scrollerIndex < tb.getTabCount()) {
  1603. scrollerPositionTabId = tb.getTab(scrollerIndex).id;
  1604. } else {
  1605. scrollerPositionTabId = null;
  1606. }
  1607. }
  1608. }
  1609. /**
  1610. * Makes tab bar visible.
  1611. *
  1612. * @since 7.2
  1613. */
  1614. public void showTabs() {
  1615. tb.setVisible(true);
  1616. removeStyleName(CLASSNAME + "-hidetabs");
  1617. tb.recalculateCaptionWidths();
  1618. }
  1619. /**
  1620. * Makes tab bar invisible.
  1621. *
  1622. * @since 7.2
  1623. */
  1624. public void hideTabs() {
  1625. tb.setVisible(false);
  1626. addStyleName(CLASSNAME + "-hidetabs");
  1627. }
  1628. /** Matches tab[ix] - used for extracting the index of the targeted tab */
  1629. private static final RegExp SUBPART_TAB_REGEXP = RegExp
  1630. .compile("tab\\[(\\d+)](.*)");
  1631. @Override
  1632. public com.google.gwt.user.client.Element getSubPartElement(
  1633. String subPart) {
  1634. if ("tabpanel".equals(subPart)) {
  1635. return DOM.asOld(tabPanel.getElement().getFirstChildElement());
  1636. } else if (SUBPART_TAB_REGEXP.test(subPart)) {
  1637. MatchResult result = SUBPART_TAB_REGEXP.exec(subPart);
  1638. int tabIx = Integer.valueOf(result.getGroup(1));
  1639. Tab tab = tb.getTab(tabIx);
  1640. if (tab != null) {
  1641. if ("/close".equals(result.getGroup(2))) {
  1642. if (tab.isClosable()) {
  1643. return tab.tabCaption.getCloseButton();
  1644. }
  1645. } else {
  1646. return tab.tabCaption.getElement();
  1647. }
  1648. }
  1649. }
  1650. return null;
  1651. }
  1652. @Override
  1653. public String getSubPartName(
  1654. com.google.gwt.user.client.Element subElement) {
  1655. if (tabPanel.getElement().equals(subElement.getParentElement())
  1656. || tabPanel.getElement().equals(subElement)) {
  1657. return "tabpanel";
  1658. } else {
  1659. for (int i = 0; i < tb.getTabCount(); ++i) {
  1660. Tab tab = tb.getTab(i);
  1661. if (tab.isClosable() && tab.tabCaption.getCloseButton()
  1662. .isOrHasChild(subElement)) {
  1663. return "tab[" + i + "]/close";
  1664. } else if (tab.getElement().isOrHasChild(subElement)) {
  1665. return "tab[" + i + "]";
  1666. }
  1667. }
  1668. }
  1669. return null;
  1670. }
  1671. }