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

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