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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  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. focusImpl.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. focusImpl.focus(td);
  257. }
  258. public void blur() {
  259. focusImpl.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(new Scheduler.ScheduledCommand() {
  469. @Override
  470. public void execute() {
  471. getTabsheet()
  472. .scrollIntoView(getTab(tabsheet.activeTabIndex));
  473. }
  474. });
  475. }
  476. public Tab navigateTab(int fromIndex, int toIndex) {
  477. Tab newNavigated = getTab(toIndex);
  478. if (newNavigated == null) {
  479. throw new IllegalArgumentException(
  480. "Tab at provided index toIndex was not found");
  481. }
  482. Tab oldNavigated = getTab(fromIndex);
  483. newNavigated.setStyleNames(newNavigated.equals(selected),
  484. isFirstVisibleTab(toIndex), true);
  485. if (oldNavigated != null && fromIndex != toIndex) {
  486. oldNavigated.setStyleNames(oldNavigated.equals(selected),
  487. isFirstVisibleTab(fromIndex), false);
  488. }
  489. return newNavigated;
  490. }
  491. public void removeTab(int i) {
  492. Tab tab = getTab(i);
  493. if (tab == null) {
  494. return;
  495. }
  496. remove(tab);
  497. /*
  498. * If this widget was selected we need to unmark it as the last
  499. * selected
  500. */
  501. if (tab == selected) {
  502. selected = null;
  503. }
  504. // FIXME: Shouldn't something be selected instead?
  505. int scrollerIndexCandidate = getTabIndex(
  506. getTabsheet().scrollerPositionTabId);
  507. if (scrollerIndexCandidate < 0) {
  508. // The tab with id scrollerPositionTabId has been removed
  509. scrollerIndexCandidate = getTabsheet().scrollerIndex;
  510. }
  511. scrollerIndexCandidate = selectNewShownTab(scrollerIndexCandidate);
  512. if (scrollerIndexCandidate >= 0
  513. && scrollerIndexCandidate < getTabCount()) {
  514. getTabsheet().scrollIntoView(getTab(scrollerIndexCandidate));
  515. }
  516. }
  517. private int selectNewShownTab(int oldPosition) {
  518. // After removing a tab, find a new scroll position. In most
  519. // cases the scroll position does not change, but if the tab
  520. // at the scroll position was removed, need to find a nearby
  521. // tab that is visible.
  522. for (int i = oldPosition; i < getTabCount(); i++) {
  523. Tab tab = getTab(i);
  524. if (!tab.isHiddenOnServer()) {
  525. return i;
  526. }
  527. }
  528. for (int i = oldPosition - 1; i >= 0; i--) {
  529. Tab tab = getTab(i);
  530. if (!tab.isHiddenOnServer()) {
  531. return i;
  532. }
  533. }
  534. return -1;
  535. }
  536. private boolean isFirstVisibleTab(int index) {
  537. return getFirstVisibleTab() == index;
  538. }
  539. /**
  540. * Returns the index of the first visible tab on the server
  541. */
  542. private int getFirstVisibleTab() {
  543. return getNextVisibleTab(-1);
  544. }
  545. /**
  546. * Find the next visible tab. Returns -1 if none is found.
  547. *
  548. * @param i
  549. * @return
  550. */
  551. private int getNextVisibleTab(int i) {
  552. int tabs = getTabCount();
  553. do {
  554. i++;
  555. } while (i < tabs && getTab(i).isHiddenOnServer());
  556. if (i == tabs) {
  557. return -1;
  558. } else {
  559. return i;
  560. }
  561. }
  562. /**
  563. * Returns the index of the first visible tab in browser.
  564. */
  565. private int getFirstVisibleTabClient() {
  566. int tabs = getTabCount();
  567. int i = 0;
  568. while (i < tabs && !getTab(i).isVisible()) {
  569. i++;
  570. }
  571. if (i == tabs) {
  572. return -1;
  573. } else {
  574. return i;
  575. }
  576. }
  577. /**
  578. * Find the previous visible tab. Returns -1 if none is found.
  579. *
  580. * @param i
  581. * @return
  582. */
  583. private int getPreviousVisibleTab(int i) {
  584. do {
  585. i--;
  586. } while (i >= 0 && getTab(i).isHiddenOnServer());
  587. return i;
  588. }
  589. public int scrollLeft(int currentFirstVisible) {
  590. int prevVisible = getPreviousVisibleTab(currentFirstVisible);
  591. if (prevVisible == -1) {
  592. return -1;
  593. }
  594. Tab newFirst = getTab(prevVisible);
  595. newFirst.setVisible(true);
  596. newFirst.recalculateCaptionWidth();
  597. return prevVisible;
  598. }
  599. public int scrollRight(int currentFirstVisible) {
  600. int nextVisible = getNextVisibleTab(currentFirstVisible);
  601. if (nextVisible == -1) {
  602. return -1;
  603. }
  604. Tab currentFirst = getTab(currentFirstVisible);
  605. currentFirst.setVisible(false);
  606. currentFirst.recalculateCaptionWidth();
  607. return nextVisible;
  608. }
  609. private void recalculateCaptionWidths() {
  610. for (int i = 0; i < getTabCount(); ++i) {
  611. getTab(i).recalculateCaptionWidth();
  612. }
  613. }
  614. }
  615. // TODO using the CLASSNAME directly makes primaryStyleName for TabSheet of
  616. // very limited use - all use of style names should be refactored in the
  617. // future
  618. public static final String CLASSNAME = TabsheetState.PRIMARY_STYLE_NAME;
  619. public static final String TABS_CLASSNAME = CLASSNAME + "-tabcontainer";
  620. public static final String SCROLLER_CLASSNAME = CLASSNAME + "-scroller";
  621. /** For internal use only. May be removed or replaced in the future. */
  622. // tabbar and 'scroller' container
  623. public final Element tabs;
  624. /**
  625. * The tabindex property (position in the browser's focus cycle.) Named like
  626. * this to avoid confusion with activeTabIndex.
  627. */
  628. int tabulatorIndex = 0;
  629. private static final FocusImpl focusImpl = FocusImpl.getFocusImplForPanel();
  630. // tab-scroller element
  631. private final Element scroller;
  632. // tab-scroller next button element
  633. private final Element scrollerNext;
  634. // tab-scroller prev button element
  635. private final Element scrollerPrev;
  636. /**
  637. * The index of the first visible tab (when scrolled)
  638. */
  639. private int scrollerIndex = 0;
  640. /**
  641. * The id of the tab at position scrollerIndex. This is used for keeping the
  642. * scroll position unchanged when a tab is removed from the server side and
  643. * the removed tab lies to the left of the current scroll position. For
  644. * other cases scrollerIndex alone would be sufficient. Since the tab at the
  645. * current scroll position can be removed, scrollerIndex is required in
  646. * addition to this variable.
  647. */
  648. private String scrollerPositionTabId;
  649. final TabBar tb = new TabBar(this);
  650. /** For internal use only. May be removed or replaced in the future. */
  651. protected final VTabsheetPanel tabPanel = new VTabsheetPanel();
  652. /** For internal use only. May be removed or replaced in the future. */
  653. public final Element contentNode;
  654. private final Element deco;
  655. /** For internal use only. May be removed or replaced in the future. */
  656. public boolean waitingForResponse;
  657. private String currentStyle;
  658. /**
  659. * @return Whether the tab could be selected or not.
  660. */
  661. private boolean canSelectTab(final int tabIndex) {
  662. Tab tab = tb.getTab(tabIndex);
  663. if (getApplicationConnection() == null || disabled
  664. || waitingForResponse) {
  665. return false;
  666. }
  667. if (!tab.isEnabledOnServer() || tab.isHiddenOnServer()) {
  668. return false;
  669. }
  670. // Note that we return true when tabIndex == activeTabIndex; the active
  671. // tab could be selected, it's just a no-op.
  672. return true;
  673. }
  674. /**
  675. * Load the content of a tab of the provided index.
  676. *
  677. * @param tabIndex
  678. * The index of the tab to load
  679. *
  680. * @return true if the specified sheet gets loaded, otherwise false.
  681. */
  682. public boolean loadTabSheet(int tabIndex) {
  683. if (activeTabIndex != tabIndex && canSelectTab(tabIndex)) {
  684. tb.selectTab(tabIndex);
  685. activeTabIndex = tabIndex;
  686. addStyleDependentName("loading");
  687. // Hide the current contents so a loading indicator can be shown
  688. // instead
  689. getCurrentlyDisplayedWidget().getElement().getParentElement()
  690. .getStyle().setVisibility(Visibility.HIDDEN);
  691. getRpcProxy().setSelected(tabKeys.get(tabIndex).toString());
  692. waitingForResponse = true;
  693. tb.getTab(tabIndex).focus(); // move keyboard focus to active tab
  694. return true;
  695. }
  696. return false;
  697. }
  698. /**
  699. * Returns the currently displayed widget in the tab panel.
  700. *
  701. * @since 7.2
  702. * @return currently displayed content widget
  703. */
  704. public Widget getCurrentlyDisplayedWidget() {
  705. return tabPanel.getWidget(tabPanel.getVisibleWidget());
  706. }
  707. /**
  708. * Returns the client to server RPC proxy for the tabsheet.
  709. *
  710. * @since 7.2
  711. * @return RPC proxy
  712. */
  713. protected TabsheetServerRpc getRpcProxy() {
  714. return connector.getRpcProxy(TabsheetServerRpc.class);
  715. }
  716. /**
  717. * For internal use only.
  718. *
  719. * Avoid using this method directly and use appropriate superclass methods
  720. * where applicable.
  721. *
  722. * @deprecated since 7.2 - use more specific methods instead (getRpcProxy(),
  723. * getConnectorForWidget(Widget) etc.)
  724. * @return ApplicationConnection
  725. */
  726. @Deprecated
  727. public ApplicationConnection getApplicationConnection() {
  728. return client;
  729. }
  730. private VTooltip getVTooltip() {
  731. return getApplicationConnection().getVTooltip();
  732. }
  733. public void tabSizeMightHaveChanged(Tab tab) {
  734. // icon onloads may change total width of tabsheet
  735. if (isDynamicWidth()) {
  736. updateDynamicWidth();
  737. }
  738. updateTabScroller();
  739. }
  740. void sendTabClosedEvent(int tabIndex) {
  741. getRpcProxy().closeTab(tabKeys.get(tabIndex));
  742. }
  743. public VTabsheet() {
  744. super(CLASSNAME);
  745. // Tab scrolling
  746. getElement().getStyle().setOverflow(Overflow.HIDDEN);
  747. tabs = DOM.createDiv();
  748. DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
  749. Roles.getTablistRole().set(tabs);
  750. Roles.getTablistRole().setAriaLiveProperty(tabs, LiveValue.OFF);
  751. scroller = DOM.createDiv();
  752. Roles.getTablistRole().setAriaHiddenState(scroller, true);
  753. DOM.setElementProperty(scroller, "className", SCROLLER_CLASSNAME);
  754. scrollerPrev = DOM.createButton();
  755. scrollerPrev.setTabIndex(-1);
  756. DOM.setElementProperty(scrollerPrev, "className",
  757. SCROLLER_CLASSNAME + "Prev");
  758. Roles.getTablistRole().setAriaHiddenState(scrollerPrev, true);
  759. DOM.sinkEvents(scrollerPrev, Event.ONCLICK | Event.ONMOUSEDOWN);
  760. scrollerNext = DOM.createButton();
  761. scrollerNext.setTabIndex(-1);
  762. DOM.setElementProperty(scrollerNext, "className",
  763. SCROLLER_CLASSNAME + "Next");
  764. Roles.getTablistRole().setAriaHiddenState(scrollerNext, true);
  765. DOM.sinkEvents(scrollerNext, Event.ONCLICK | Event.ONMOUSEDOWN);
  766. DOM.appendChild(getElement(), tabs);
  767. // Tabs
  768. tabPanel.setStyleName(CLASSNAME + "-tabsheetpanel");
  769. contentNode = DOM.createDiv();
  770. Roles.getTabpanelRole().set(contentNode);
  771. deco = DOM.createDiv();
  772. tb.setStyleName(CLASSNAME + "-tabs");
  773. DOM.setElementProperty(contentNode, "className",
  774. CLASSNAME + "-content");
  775. DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
  776. add(tb, tabs);
  777. DOM.appendChild(scroller, scrollerPrev);
  778. DOM.appendChild(scroller, scrollerNext);
  779. DOM.appendChild(getElement(), contentNode);
  780. add(tabPanel, contentNode);
  781. DOM.appendChild(getElement(), deco);
  782. DOM.appendChild(tabs, scroller);
  783. // TODO Use for Safari only. Fix annoying 1px first cell in TabBar.
  784. // DOM.setStyleAttribute(DOM.getFirstChild(DOM.getFirstChild(DOM
  785. // .getFirstChild(tb.getElement()))), "display", "none");
  786. }
  787. @Override
  788. public void onBrowserEvent(Event event) {
  789. com.google.gwt.dom.client.Element eventTarget = DOM
  790. .eventGetTarget(event);
  791. if (event.getTypeInt() == Event.ONCLICK) {
  792. // Tab scrolling
  793. if (eventTarget == scrollerPrev || eventTarget == scrollerNext) {
  794. scrollAccordingToScrollTarget(eventTarget);
  795. event.stopPropagation();
  796. }
  797. } else if (event.getTypeInt() == Event.ONMOUSEDOWN) {
  798. if (eventTarget == scrollerPrev || eventTarget == scrollerNext) {
  799. // In case the focus was previously on a Tab, we need to cancel
  800. // the upcoming blur on the Tab which will follow this mouse
  801. // down event.
  802. focusBlurManager.cancelNextBlurSchedule();
  803. return;
  804. }
  805. }
  806. super.onBrowserEvent(event);
  807. }
  808. /*
  809. * Scroll the tab bar according to the last scrollTarget (the scroll button
  810. * pressed).
  811. */
  812. private void scrollAccordingToScrollTarget(
  813. com.google.gwt.dom.client.Element scrollTarget) {
  814. if (scrollTarget == null) {
  815. return;
  816. }
  817. int newFirstIndex = -1;
  818. // Scroll left.
  819. if (isScrolledTabs() && scrollTarget == scrollerPrev) {
  820. newFirstIndex = tb.scrollLeft(scrollerIndex);
  821. // Scroll right.
  822. } else if (isClippedTabs() && scrollTarget == scrollerNext) {
  823. newFirstIndex = tb.scrollRight(scrollerIndex);
  824. }
  825. if (newFirstIndex != -1) {
  826. scrollerIndex = newFirstIndex;
  827. scrollerPositionTabId = tb.getTab(scrollerIndex).id;
  828. updateTabScroller();
  829. }
  830. // For this to work well, make sure the method gets called only from
  831. // user events.
  832. selectionHandler.focusTabAtIndex(scrollerIndex);
  833. }
  834. /**
  835. * Checks if the tab with the selected index has been scrolled out of the
  836. * view (on the left side).
  837. *
  838. * @param index
  839. * @return
  840. */
  841. private boolean scrolledOutOfView(int index) {
  842. return scrollerIndex > index;
  843. }
  844. /** For internal use only. May be removed or replaced in the future. */
  845. public void handleStyleNames(AbstractComponentState state) {
  846. // Add proper stylenames for all elements (easier to prevent unwanted
  847. // style inheritance)
  848. if (ComponentStateUtil.hasStyles(state)) {
  849. final List<String> styles = state.styles;
  850. if (currentStyle == null
  851. || !currentStyle.equals(styles.toString())) {
  852. currentStyle = styles.toString();
  853. final String tabsBaseClass = TABS_CLASSNAME;
  854. String tabsClass = tabsBaseClass;
  855. final String contentBaseClass = CLASSNAME + "-content";
  856. String contentClass = contentBaseClass;
  857. final String decoBaseClass = CLASSNAME + "-deco";
  858. String decoClass = decoBaseClass;
  859. for (String style : styles) {
  860. tb.addStyleDependentName(style);
  861. tabsClass += " " + tabsBaseClass + "-" + style;
  862. contentClass += " " + contentBaseClass + "-" + style;
  863. decoClass += " " + decoBaseClass + "-" + style;
  864. }
  865. DOM.setElementProperty(tabs, "className", tabsClass);
  866. DOM.setElementProperty(contentNode, "className", contentClass);
  867. DOM.setElementProperty(deco, "className", decoClass);
  868. }
  869. } else {
  870. tb.setStyleName(CLASSNAME + "-tabs");
  871. DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
  872. DOM.setElementProperty(contentNode, "className",
  873. CLASSNAME + "-content");
  874. DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
  875. }
  876. }
  877. /** For internal use only. May be removed or replaced in the future. */
  878. public void updateDynamicWidth() {
  879. // Find width consumed by tabs
  880. TableCellElement spacerCell = ((TableElement) tb.getElement().cast())
  881. .getRows().getItem(0).getCells().getItem(tb.getTabCount());
  882. int spacerWidth = spacerCell.getOffsetWidth();
  883. DivElement div = (DivElement) spacerCell.getFirstChildElement();
  884. int spacerMinWidth = spacerCell.getOffsetWidth() - div.getOffsetWidth();
  885. int tabsWidth = tb.getOffsetWidth() - spacerWidth + spacerMinWidth;
  886. // Find content width
  887. Style style = tabPanel.getElement().getStyle();
  888. String overflow = style.getProperty("overflow");
  889. style.setProperty("overflow", "hidden");
  890. style.setPropertyPx("width", tabsWidth);
  891. boolean hasTabs = tabPanel.getWidgetCount() > 0;
  892. Style wrapperstyle = null;
  893. if (hasTabs) {
  894. wrapperstyle = getCurrentlyDisplayedWidget().getElement()
  895. .getParentElement().getStyle();
  896. wrapperstyle.setPropertyPx("width", tabsWidth);
  897. }
  898. // Get content width from actual widget
  899. int contentWidth = 0;
  900. if (hasTabs) {
  901. contentWidth = getCurrentlyDisplayedWidget().getOffsetWidth();
  902. }
  903. style.setProperty("overflow", overflow);
  904. // Set widths to max(tabs,content)
  905. if (tabsWidth < contentWidth) {
  906. tabsWidth = contentWidth;
  907. }
  908. int outerWidth = tabsWidth + getContentAreaBorderWidth();
  909. tabs.getStyle().setPropertyPx("width", outerWidth);
  910. style.setPropertyPx("width", tabsWidth);
  911. if (hasTabs) {
  912. wrapperstyle.setPropertyPx("width", tabsWidth);
  913. }
  914. contentNode.getStyle().setPropertyPx("width", tabsWidth);
  915. super.setWidth(outerWidth + "px");
  916. updateOpenTabSize();
  917. }
  918. private boolean isAllTabsBeforeIndexInvisible() {
  919. boolean invisible = true;
  920. for (int i = 0; i < scrollerIndex; i++) {
  921. invisible = invisible & !tb.getTab(i).isVisible();
  922. }
  923. return invisible;
  924. }
  925. private boolean isScrollerPrevDisabled() {
  926. return scrollerPrev.getClassName()
  927. .contains(PREV_SCROLLER_DISABLED_CLASSNAME);
  928. }
  929. private boolean isScrollerHidden() {
  930. return scroller.getStyle().getDisplay()
  931. .equals(Display.NONE.getCssName());
  932. }
  933. private boolean isIndexSkippingHiddenTabs() {
  934. return isAllTabsBeforeIndexInvisible()
  935. && (isScrollerPrevDisabled() || isScrollerHidden());
  936. }
  937. @Override
  938. public void renderTab(final TabState tabState, int index) {
  939. Tab tab = tb.getTab(index);
  940. if (tab == null) {
  941. tab = tb.addTab();
  942. }
  943. tab.updateFromState(tabState);
  944. tab.setEnabledOnServer((!disabledTabKeys.contains(tabKeys.get(index))));
  945. tab.setHiddenOnServer(!tabState.visible);
  946. if (scrolledOutOfView(index) && !isIndexSkippingHiddenTabs()) {
  947. // Should not set tabs visible if they are scrolled out of view
  948. tab.setVisible(false);
  949. } else {
  950. // When the tab was hidden and then turned visible again
  951. // and there is space for it, it should be in view (#17096) (#17333)
  952. if (isTabSetVisibleBeforeScroller(tabState, index, tab)) {
  953. scrollerIndex = index;
  954. tab.setVisible(true);
  955. tab.setStyleNames(false, true);
  956. // scroll to the currently selected tab if it got clipped
  957. // after making another tab visible
  958. if (isClippedTabs()) {
  959. scrollIntoView(getActiveTab());
  960. }
  961. } else {
  962. tab.setVisible(tabState.visible);
  963. }
  964. }
  965. /*
  966. * Force the width of the caption container so the content will not wrap
  967. * and tabs won't be too narrow in certain browsers
  968. */
  969. tab.recalculateCaptionWidth();
  970. }
  971. /**
  972. * Checks whether the tab has been set to visible and the scroller is at the
  973. * first visible tab. That means that the scroller has to be adjusted so
  974. * that the tab is visible again.
  975. */
  976. private boolean isTabSetVisibleBeforeScroller(TabState tabState, int index,
  977. Tab tab) {
  978. return isIndexSkippingHiddenTabs() && isScrollerAtFirstVisibleTab()
  979. && hasTabChangedVisibility(tabState, tab)
  980. && scrolledOutOfView(index);
  981. }
  982. /**
  983. * Checks whether the tab is visible on server but is not visible on client
  984. * yet.
  985. */
  986. private boolean hasTabChangedVisibility(TabState tabState, Tab tab) {
  987. return !tab.isVisible() && tabState.visible;
  988. }
  989. private boolean isScrollerAtFirstVisibleTab() {
  990. return tb.getFirstVisibleTabClient() == scrollerIndex;
  991. }
  992. /**
  993. * @deprecated as of 7.1, VTabsheet only keeps the active tab in the DOM
  994. * without any place holders.
  995. */
  996. @Deprecated
  997. public class PlaceHolder extends VLabel {
  998. public PlaceHolder() {
  999. super("");
  1000. }
  1001. }
  1002. /**
  1003. * Renders the widget content for a tab sheet.
  1004. *
  1005. * @param newWidget
  1006. */
  1007. public void renderContent(Widget newWidget) {
  1008. assert tabPanel.getWidgetCount() <= 1;
  1009. if (null == newWidget) {
  1010. newWidget = new SimplePanel();
  1011. }
  1012. if (tabPanel.getWidgetCount() == 0) {
  1013. tabPanel.add(newWidget);
  1014. } else if (tabPanel.getWidget(0) != newWidget) {
  1015. tabPanel.remove(0);
  1016. tabPanel.add(newWidget);
  1017. }
  1018. assert tabPanel.getWidgetCount() <= 1;
  1019. // There's never any other index than 0, but maintaining API for now
  1020. tabPanel.showWidget(0);
  1021. VTabsheet.this.iLayout();
  1022. updateOpenTabSize();
  1023. VTabsheet.this.removeStyleDependentName("loading");
  1024. }
  1025. /**
  1026. * Recalculates the sizes of tab captions, causing the tabs to be rendered
  1027. * the correct size.
  1028. */
  1029. private void updateTabCaptionSizes() {
  1030. for (int tabIx = 0; tabIx < tb.getTabCount(); tabIx++) {
  1031. tb.getTab(tabIx).recalculateCaptionWidth();
  1032. }
  1033. }
  1034. /** For internal use only. May be removed or replaced in the future. */
  1035. public void updateContentNodeHeight() {
  1036. if (!isDynamicHeight()) {
  1037. int contentHeight = getOffsetHeight();
  1038. contentHeight -= deco.getOffsetHeight();
  1039. contentHeight -= tb.getOffsetHeight();
  1040. ComputedStyle cs = new ComputedStyle(contentNode);
  1041. contentHeight -= Math.ceil(cs.getPaddingHeight());
  1042. contentHeight -= Math.ceil(cs.getBorderHeight());
  1043. if (contentHeight < 0) {
  1044. contentHeight = 0;
  1045. }
  1046. // Set proper values for content element
  1047. contentNode.getStyle().setHeight(contentHeight, Unit.PX);
  1048. } else {
  1049. contentNode.getStyle().clearHeight();
  1050. }
  1051. }
  1052. /**
  1053. * Run internal layouting.
  1054. */
  1055. public void iLayout() {
  1056. updateTabScroller();
  1057. updateTabCaptionSizes();
  1058. }
  1059. /**
  1060. * Sets the size of the visible tab (component). As the tab is set to
  1061. * position: absolute (to work around a firefox flickering bug) we must keep
  1062. * this up-to-date by hand.
  1063. * <p>
  1064. * For internal use only. May be removed or replaced in the future.
  1065. */
  1066. public void updateOpenTabSize() {
  1067. /*
  1068. * The overflow=auto element must have a height specified, otherwise it
  1069. * will be just as high as the contents and no scrollbars will appear
  1070. */
  1071. int height = -1;
  1072. int width = -1;
  1073. int minWidth = 0;
  1074. if (!isDynamicHeight()) {
  1075. height = contentNode.getOffsetHeight();
  1076. }
  1077. if (!isDynamicWidth()) {
  1078. width = contentNode.getOffsetWidth() - getContentAreaBorderWidth();
  1079. } else {
  1080. /*
  1081. * If the tabbar is wider than the content we need to use the tabbar
  1082. * width as minimum width so scrollbars get placed correctly (at the
  1083. * right edge).
  1084. */
  1085. minWidth = tb.getOffsetWidth() - getContentAreaBorderWidth();
  1086. }
  1087. tabPanel.fixVisibleTabSize(width, height, minWidth);
  1088. }
  1089. /**
  1090. * Layouts the tab-scroller elements, and applies styles.
  1091. */
  1092. private void updateTabScroller() {
  1093. if (!isDynamicWidth()) {
  1094. tabs.getStyle().setWidth(100, Unit.PCT);
  1095. }
  1096. // Make sure scrollerIndex is valid
  1097. if (scrollerIndex < 0 || scrollerIndex > tb.getTabCount()) {
  1098. scrollerIndex = tb.getFirstVisibleTab();
  1099. } else if (tb.getTabCount() > 0
  1100. && tb.getTab(scrollerIndex).isHiddenOnServer()) {
  1101. scrollerIndex = tb.getNextVisibleTab(scrollerIndex);
  1102. }
  1103. boolean scrolled = isScrolledTabs();
  1104. boolean clipped = isClippedTabs();
  1105. if (tb.getTabCount() > 0 && tb.isVisible() && (scrolled || clipped)) {
  1106. scroller.getStyle().clearDisplay();
  1107. DOM.setElementProperty(scrollerPrev, "className", SCROLLER_CLASSNAME
  1108. + (scrolled ? "Prev" : PREV_SCROLLER_DISABLED_CLASSNAME));
  1109. DOM.setElementProperty(scrollerNext, "className",
  1110. SCROLLER_CLASSNAME + (clipped ? "Next" : "Next-disabled"));
  1111. // the active tab should be focusable if and only if it is visible
  1112. boolean isActiveTabVisible = scrollerIndex <= activeTabIndex
  1113. && !isClipped(tb.selected);
  1114. tb.selected.setTabulatorIndex(
  1115. isActiveTabVisible ? tabulatorIndex : -1);
  1116. } else {
  1117. scroller.getStyle().setDisplay(Display.NONE);
  1118. }
  1119. if (BrowserInfo.get().isSafariOrIOS()) {
  1120. /*
  1121. * another hack for webkits. tabscroller sometimes drops without
  1122. * "shaking it" reproducable in
  1123. * com.vaadin.tests.components.tabsheet.TabSheetIcons
  1124. */
  1125. final Style style = scroller.getStyle();
  1126. style.setProperty("whiteSpace", "normal");
  1127. Scheduler.get().scheduleDeferred(new Command() {
  1128. @Override
  1129. public void execute() {
  1130. style.setProperty("whiteSpace", "");
  1131. }
  1132. });
  1133. }
  1134. }
  1135. /** For internal use only. May be removed or replaced in the future. */
  1136. public void showAllTabs() {
  1137. scrollerIndex = tb.getFirstVisibleTab();
  1138. scrollerPositionTabId = scrollerIndex < 0 ? null
  1139. : tb.getTab(scrollerIndex).id;
  1140. for (int i = 0; i < tb.getTabCount(); i++) {
  1141. Tab t = tb.getTab(i);
  1142. if (!t.isHiddenOnServer()) {
  1143. t.setVisible(true);
  1144. }
  1145. }
  1146. }
  1147. private boolean isScrolledTabs() {
  1148. return scrollerIndex > tb.getFirstVisibleTab();
  1149. }
  1150. private boolean isClippedTabs() {
  1151. return (tb.getOffsetWidth() - DOM.getElementPropertyInt(
  1152. (Element) tb.getContainerElement().getLastChild().cast(),
  1153. "offsetWidth")) > getOffsetWidth()
  1154. - (isScrolledTabs() ? scroller.getOffsetWidth() : 0);
  1155. }
  1156. private boolean isClipped(Tab tab) {
  1157. return tab.getAbsoluteLeft() + tab.getOffsetWidth() > getAbsoluteLeft()
  1158. + getOffsetWidth() - scroller.getOffsetWidth();
  1159. }
  1160. @Override
  1161. protected void clearPaintables() {
  1162. int i = tb.getTabCount();
  1163. while (i > 0) {
  1164. tb.removeTab(--i);
  1165. }
  1166. tabPanel.clear();
  1167. }
  1168. @Override
  1169. public Iterator<Widget> getWidgetIterator() {
  1170. return tabPanel.iterator();
  1171. }
  1172. /** For internal use only. May be removed or replaced in the future. */
  1173. public int getContentAreaBorderWidth() {
  1174. return WidgetUtil.measureHorizontalBorder(contentNode);
  1175. }
  1176. @Override
  1177. public int getTabCount() {
  1178. return tb.getTabCount();
  1179. }
  1180. @Override
  1181. public ComponentConnector getTab(int index) {
  1182. if (tabPanel.getWidgetCount() > index) {
  1183. Widget widget = tabPanel.getWidget(index);
  1184. return getConnectorForWidget(widget);
  1185. }
  1186. return null;
  1187. }
  1188. @Override
  1189. public void removeTab(int index) {
  1190. tb.removeTab(index);
  1191. // Removing content from tp is handled by the connector
  1192. }
  1193. @Override
  1194. public void selectTab(int index) {
  1195. tb.selectTab(index);
  1196. }
  1197. @Override
  1198. public void focus() {
  1199. getActiveTab().focus();
  1200. }
  1201. public void blur() {
  1202. getActiveTab().blur();
  1203. }
  1204. /*
  1205. * Gets the active tab.
  1206. */
  1207. private Tab getActiveTab() {
  1208. return tb.getTab(activeTabIndex);
  1209. }
  1210. @Override
  1211. public void setConnector(AbstractComponentConnector connector) {
  1212. super.setConnector(connector);
  1213. focusBlurManager.connector = connector;
  1214. }
  1215. /*
  1216. * The focus and blur manager instance.
  1217. */
  1218. private FocusBlurManager focusBlurManager = new FocusBlurManager();
  1219. /*
  1220. * Generate the correct focus/blur events for the main TabSheet component
  1221. * (#14304).
  1222. *
  1223. * The TabSheet must fire one focus event when the user clicks on the tab
  1224. * bar (i.e. inner TabBar class) containing the Tabs or when the focus is
  1225. * provided to the TabSheet by any means. Also one blur event should be
  1226. * fired only when the user leaves the tab bar. After the user focus on the
  1227. * tab bar and before leaving it, no matter how many times he's pressing the
  1228. * Tabs or the scroll buttons, the TabSheet component should not fire any of
  1229. * those blur/focus events.
  1230. *
  1231. * The only focusable elements contained in the tab bar are the Tabs (see
  1232. * inner class Tab). The reason is the accessibility support.
  1233. *
  1234. * Having this in mind, the chosen solution path for our problem is to match
  1235. * a sequence of focus/blur events on the tabs, choose only the first focus
  1236. * and last blur events and pass only those further to the main component.
  1237. * Any consecutive blur/focus events on 2 Tabs must be ignored.
  1238. *
  1239. * Because in a blur event we don't know whether or not a focus will follow,
  1240. * we just defer a command initiated on the blur event to wait and see if
  1241. * any focus will appear. The command will be executed after the next focus,
  1242. * so if no focus was triggered in the mean while it'll submit the blur
  1243. * event to the main component, otherwise it'll do nothing, so the main
  1244. * component will not generate the blur..
  1245. */
  1246. private static class FocusBlurManager {
  1247. // The real tab with focus on it. If the focus goes to another element
  1248. // in the page this will be null.
  1249. private Tab focusedTab;
  1250. /*
  1251. * Gets the focused tab.
  1252. */
  1253. private Tab getFocusedTab() {
  1254. return focusedTab;
  1255. }
  1256. /*
  1257. * Sets the local field tracking the focused tab.
  1258. */
  1259. private void setFocusedTab(Tab focusedTab) {
  1260. this.focusedTab = focusedTab;
  1261. }
  1262. /*
  1263. * The ultimate focus/blur event dispatcher.
  1264. */
  1265. private AbstractComponentConnector connector;
  1266. /**
  1267. * Delegate method for the onFocus event occurring on Tab.
  1268. *
  1269. * @since 7.2.6
  1270. * @param newFocusTab
  1271. * the new focused tab.
  1272. * @see #onBlur(Tab)
  1273. */
  1274. public void onFocus(Tab newFocusTab) {
  1275. if (connector.hasEventListener(EventId.FOCUS)) {
  1276. // Send the focus event only first time when we focus on any
  1277. // tab. The focused tab will be reseted on the last blur.
  1278. if (focusedTab == null) {
  1279. connector.getRpcProxy(FocusAndBlurServerRpc.class).focus();
  1280. }
  1281. }
  1282. cancelLastBlurSchedule();
  1283. setFocusedTab(newFocusTab);
  1284. }
  1285. /**
  1286. * Delegate method for the onBlur event occurring on Tab.
  1287. *
  1288. * @param blurSource
  1289. * the source of the blur.
  1290. *
  1291. * @see #onFocus(Tab)
  1292. */
  1293. public void onBlur(Tab blurSource) {
  1294. if (focusedTab != null && focusedTab == blurSource) {
  1295. if (connector.hasEventListener(EventId.BLUR)) {
  1296. scheduleBlur(focusedTab);
  1297. }
  1298. }
  1299. }
  1300. /*
  1301. * The last blur command to be executed.
  1302. */
  1303. private BlurCommand blurCommand;
  1304. /*
  1305. * Execute the final blur command.
  1306. */
  1307. private class BlurCommand implements Command {
  1308. /*
  1309. * The blur source.
  1310. */
  1311. private Tab blurSource;
  1312. /**
  1313. * Create the blur command using the blur source.
  1314. *
  1315. * @param blurSource
  1316. * the source.
  1317. * @param focusedTabProvider
  1318. * provides the current focused tab.
  1319. */
  1320. public BlurCommand(Tab blurSource) {
  1321. this.blurSource = blurSource;
  1322. }
  1323. /**
  1324. * Stop the command from being executed.
  1325. *
  1326. * @since 7.4
  1327. */
  1328. public void stopSchedule() {
  1329. blurSource = null;
  1330. }
  1331. /**
  1332. * Schedule the command for a deferred execution.
  1333. *
  1334. * @since 7.4
  1335. */
  1336. public void scheduleDeferred() {
  1337. Scheduler.get().scheduleDeferred(this);
  1338. }
  1339. @Override
  1340. public void execute() {
  1341. Tab focusedTab = getFocusedTab();
  1342. if (blurSource == null) {
  1343. return;
  1344. }
  1345. // The focus didn't change since this blur triggered, so
  1346. // the new focused element is not a tab.
  1347. if (focusedTab == blurSource) {
  1348. // We're certain there's no focus anymore.
  1349. focusedTab.removeAssistiveDescription();
  1350. setFocusedTab(null);
  1351. connector.getRpcProxy(FocusAndBlurServerRpc.class).blur();
  1352. }
  1353. // Call this to set it to null and be consistent.
  1354. cancelLastBlurSchedule();
  1355. }
  1356. }
  1357. /*
  1358. * Schedule a new blur event for a deferred execution.
  1359. */
  1360. private void scheduleBlur(Tab blurSource) {
  1361. if (nextBlurScheduleCancelled) {
  1362. // This will set the stopNextBlurCommand back to false as well.
  1363. cancelLastBlurSchedule();
  1364. // Reset the status.
  1365. nextBlurScheduleCancelled = false;
  1366. return;
  1367. }
  1368. cancelLastBlurSchedule();
  1369. blurCommand = new BlurCommand(blurSource);
  1370. blurCommand.scheduleDeferred();
  1371. }
  1372. /**
  1373. * Remove the last blur deferred command from execution.
  1374. */
  1375. public void cancelLastBlurSchedule() {
  1376. if (blurCommand != null) {
  1377. blurCommand.stopSchedule();
  1378. blurCommand = null;
  1379. }
  1380. // We really want to make sure this flag gets reseted at any time
  1381. // when something interact with the blur manager and ther's no blur
  1382. // command scheduled (as we just canceled it).
  1383. nextBlurScheduleCancelled = false;
  1384. }
  1385. /**
  1386. * Cancel the next scheduled execution. This method must be called only
  1387. * from an event occurring before the onBlur event. It's the case of IE
  1388. * which doesn't trigger the focus event, so we're using this approach
  1389. * to cancel the next blur event prior it's execution, calling the
  1390. * method from mouse down event.
  1391. */
  1392. public void cancelNextBlurSchedule() {
  1393. // Make sure there's still no other command to be executed.
  1394. cancelLastBlurSchedule();
  1395. nextBlurScheduleCancelled = true;
  1396. }
  1397. /*
  1398. * Flag that the next deferred command won't get executed. This is
  1399. * useful in case of IE where the user focus event don't fire and we're
  1400. * using the mouse down event to track the focus. But the mouse down
  1401. * event triggers before the blur, so we need to cancel the deferred
  1402. * execution in advance.
  1403. */
  1404. private boolean nextBlurScheduleCancelled = false;
  1405. }
  1406. /*
  1407. * The tabs selection handler instance.
  1408. */
  1409. private final TabSelectionHandler selectionHandler = new TabSelectionHandler();
  1410. /*
  1411. * Handle the events for selecting the tabs.
  1412. */
  1413. private class TabSelectionHandler implements FocusHandler, BlurHandler,
  1414. KeyDownHandler, ClickHandler, MouseDownHandler {
  1415. /** For internal use only. May be removed or replaced in the future. */
  1416. // The current visible focused index.
  1417. private int focusedTabIndex = 0;
  1418. /**
  1419. * Register the tab to the selection handler.
  1420. *
  1421. * @param tab
  1422. * the tab to register.
  1423. */
  1424. public void registerTab(Tab tab) {
  1425. tab.addBlurHandler(this);
  1426. tab.addFocusHandler(this);
  1427. tab.addKeyDownHandler(this);
  1428. tab.addClickHandler(this);
  1429. tab.addMouseDownHandler(this);
  1430. }
  1431. @Override
  1432. public void onBlur(final BlurEvent event) {
  1433. getVTooltip().hideTooltip();
  1434. Object blurSource = event.getSource();
  1435. if (blurSource instanceof Tab) {
  1436. focusBlurManager.onBlur((Tab) blurSource);
  1437. }
  1438. }
  1439. @Override
  1440. public void onFocus(FocusEvent event) {
  1441. if (event.getSource() instanceof Tab) {
  1442. Tab focusSource = (Tab) event.getSource();
  1443. focusBlurManager.onFocus(focusSource);
  1444. if (focusSource.hasTooltip()) {
  1445. focusSource.setAssistiveDescription(
  1446. getVTooltip().getUniqueId());
  1447. getVTooltip().showAssistive(focusSource.getTooltipInfo());
  1448. }
  1449. }
  1450. }
  1451. @Override
  1452. public void onClick(ClickEvent event) {
  1453. // IE doesn't trigger focus when click, so we need to make sure
  1454. // the previous blur deferred command will get killed.
  1455. focusBlurManager.cancelLastBlurSchedule();
  1456. TabCaption caption = (TabCaption) event.getSource();
  1457. Element targetElement = event.getNativeEvent().getEventTarget()
  1458. .cast();
  1459. // the tab should not be focused if the close button was clicked
  1460. if (targetElement == caption.getCloseButton()) {
  1461. return;
  1462. }
  1463. int index = tb.getWidgetIndex(caption.getParent());
  1464. tb.navigateTab(focusedTabIndex, index);
  1465. focusedTabIndex = index;
  1466. if (!loadTabSheet(index)) {
  1467. // This needs to be called at the end, as the activeTabIndex
  1468. // is set in the loadTabSheet.
  1469. focus();
  1470. }
  1471. }
  1472. @Override
  1473. public void onMouseDown(MouseDownEvent event) {
  1474. if (event.getSource() instanceof Tab) {
  1475. // IE doesn't trigger focus when click, so we need to make sure
  1476. // the
  1477. // next blur deferred command will get killed.
  1478. focusBlurManager.cancelNextBlurSchedule();
  1479. }
  1480. }
  1481. @Override
  1482. public void onKeyDown(KeyDownEvent event) {
  1483. if (event.getSource() instanceof Tab) {
  1484. int keycode = event.getNativeEvent().getKeyCode();
  1485. if (!event.isAnyModifierKeyDown()) {
  1486. if (keycode == getPreviousTabKey()) {
  1487. selectPreviousTab();
  1488. event.stopPropagation();
  1489. } else if (keycode == getNextTabKey()) {
  1490. selectNextTab();
  1491. event.stopPropagation();
  1492. } else if (keycode == getCloseTabKey()) {
  1493. Tab tab = tb.getTab(activeTabIndex);
  1494. if (tab.isClosable()) {
  1495. tab.onClose();
  1496. }
  1497. } else if (keycode == getSelectTabKey()) {
  1498. loadTabSheet(focusedTabIndex);
  1499. // Prevent the page from scrolling when hitting space
  1500. // (select key) to select the current tab.
  1501. event.preventDefault();
  1502. }
  1503. }
  1504. }
  1505. }
  1506. /*
  1507. * Left arrow key selection.
  1508. */
  1509. private void selectPreviousTab() {
  1510. int newTabIndex = focusedTabIndex;
  1511. // Find the previous visible and enabled tab if any.
  1512. do {
  1513. newTabIndex--;
  1514. } while (newTabIndex >= 0 && !canSelectTab(newTabIndex));
  1515. if (newTabIndex >= 0) {
  1516. keySelectTab(newTabIndex);
  1517. }
  1518. }
  1519. /*
  1520. * Right arrow key selection.
  1521. */
  1522. private void selectNextTab() {
  1523. int newTabIndex = focusedTabIndex;
  1524. // Find the next visible and enabled tab if any.
  1525. do {
  1526. newTabIndex++;
  1527. } while (newTabIndex < getTabCount() && !canSelectTab(newTabIndex));
  1528. if (newTabIndex < getTabCount()) {
  1529. keySelectTab(newTabIndex);
  1530. }
  1531. }
  1532. /*
  1533. * Select the specified tab using left/right key.
  1534. */
  1535. private void keySelectTab(int newTabIndex) {
  1536. Tab tab = tb.getTab(newTabIndex);
  1537. if (tab == null) {
  1538. return;
  1539. }
  1540. // Focus the tab, otherwise the selected one will loose focus and
  1541. // TabSheet will get blurred.
  1542. focusTabAtIndex(newTabIndex);
  1543. tb.navigateTab(focusedTabIndex, newTabIndex);
  1544. focusedTabIndex = newTabIndex;
  1545. }
  1546. /**
  1547. * Focus the specified tab. Make sure to call this only from user
  1548. * events, otherwise will break things.
  1549. *
  1550. * @param tabIndex
  1551. * the index of the tab to set.
  1552. */
  1553. void focusTabAtIndex(int tabIndex) {
  1554. Tab tabToFocus = tb.getTab(tabIndex);
  1555. if (tabToFocus != null) {
  1556. tabToFocus.focus();
  1557. }
  1558. }
  1559. }
  1560. /**
  1561. * @return The key code of the keyboard shortcut that selects the previous
  1562. * tab in a focused tabsheet.
  1563. */
  1564. protected int getPreviousTabKey() {
  1565. return KeyCodes.KEY_LEFT;
  1566. }
  1567. /**
  1568. * Gets the key to activate the selected tab when navigating using
  1569. * previous/next (left/right) keys.
  1570. *
  1571. * @return the key to activate the selected tab.
  1572. *
  1573. * @see #getNextTabKey()
  1574. * @see #getPreviousTabKey()
  1575. */
  1576. protected int getSelectTabKey() {
  1577. return KeyCodes.KEY_SPACE;
  1578. }
  1579. /**
  1580. * @return The key code of the keyboard shortcut that selects the next tab
  1581. * in a focused tabsheet.
  1582. */
  1583. protected int getNextTabKey() {
  1584. return KeyCodes.KEY_RIGHT;
  1585. }
  1586. /**
  1587. * @return The key code of the keyboard shortcut that closes the currently
  1588. * selected tab in a focused tabsheet.
  1589. */
  1590. protected int getCloseTabKey() {
  1591. return KeyCodes.KEY_DELETE;
  1592. }
  1593. private void scrollIntoView(Tab tab) {
  1594. if (!tab.isHiddenOnServer()) {
  1595. // Check for visibility first as clipped tabs to the right are
  1596. // always visible.
  1597. // On IE8 a tab with false visibility would have the bounds of the
  1598. // full TabBar.
  1599. if (!tab.isVisible()) {
  1600. while (!tab.isVisible()) {
  1601. scrollerIndex = tb.scrollLeft(scrollerIndex);
  1602. }
  1603. updateTabScroller();
  1604. } else if (isClipped(tab)) {
  1605. while (isClipped(tab) && scrollerIndex != -1) {
  1606. scrollerIndex = tb.scrollRight(scrollerIndex);
  1607. }
  1608. updateTabScroller();
  1609. }
  1610. if (scrollerIndex >= 0 && scrollerIndex < tb.getTabCount()) {
  1611. scrollerPositionTabId = tb.getTab(scrollerIndex).id;
  1612. } else {
  1613. scrollerPositionTabId = null;
  1614. }
  1615. }
  1616. }
  1617. /**
  1618. * Makes tab bar visible.
  1619. *
  1620. * @since 7.2
  1621. */
  1622. public void showTabs() {
  1623. tb.setVisible(true);
  1624. removeStyleName(CLASSNAME + "-hidetabs");
  1625. tb.recalculateCaptionWidths();
  1626. }
  1627. /**
  1628. * Makes tab bar invisible.
  1629. *
  1630. * @since 7.2
  1631. */
  1632. public void hideTabs() {
  1633. tb.setVisible(false);
  1634. addStyleName(CLASSNAME + "-hidetabs");
  1635. }
  1636. /** Matches tab[ix] - used for extracting the index of the targeted tab */
  1637. private static final RegExp SUBPART_TAB_REGEXP = RegExp
  1638. .compile("tab\\[(\\d+)](.*)");
  1639. @Override
  1640. public com.google.gwt.user.client.Element getSubPartElement(
  1641. String subPart) {
  1642. if ("tabpanel".equals(subPart)) {
  1643. return DOM.asOld(tabPanel.getElement().getFirstChildElement());
  1644. } else if (SUBPART_TAB_REGEXP.test(subPart)) {
  1645. MatchResult result = SUBPART_TAB_REGEXP.exec(subPart);
  1646. int tabIx = Integer.valueOf(result.getGroup(1));
  1647. Tab tab = tb.getTab(tabIx);
  1648. if (tab != null) {
  1649. if ("/close".equals(result.getGroup(2))) {
  1650. if (tab.isClosable()) {
  1651. return tab.tabCaption.getCloseButton();
  1652. }
  1653. } else {
  1654. return tab.tabCaption.getElement();
  1655. }
  1656. }
  1657. }
  1658. return null;
  1659. }
  1660. @Override
  1661. public String getSubPartName(
  1662. com.google.gwt.user.client.Element subElement) {
  1663. if (tabPanel.getElement().equals(subElement.getParentElement())
  1664. || tabPanel.getElement().equals(subElement)) {
  1665. return "tabpanel";
  1666. } else {
  1667. for (int i = 0; i < tb.getTabCount(); ++i) {
  1668. Tab tab = tb.getTab(i);
  1669. if (tab.isClosable() && tab.tabCaption.getCloseButton()
  1670. .isOrHasChild(subElement)) {
  1671. return "tab[" + i + "]/close";
  1672. } else if (tab.getElement().isOrHasChild(subElement)) {
  1673. return "tab[" + i + "]";
  1674. }
  1675. }
  1676. }
  1677. return null;
  1678. }
  1679. }