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

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