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

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