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

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