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

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