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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import com.google.gwt.core.client.Scheduler;
  8. import com.google.gwt.dom.client.DivElement;
  9. import com.google.gwt.dom.client.Style;
  10. import com.google.gwt.dom.client.Style.Visibility;
  11. import com.google.gwt.dom.client.TableCellElement;
  12. import com.google.gwt.dom.client.TableElement;
  13. import com.google.gwt.event.dom.client.BlurEvent;
  14. import com.google.gwt.event.dom.client.BlurHandler;
  15. import com.google.gwt.event.dom.client.ClickEvent;
  16. import com.google.gwt.event.dom.client.ClickHandler;
  17. import com.google.gwt.event.dom.client.FocusEvent;
  18. import com.google.gwt.event.dom.client.FocusHandler;
  19. import com.google.gwt.event.dom.client.HasBlurHandlers;
  20. import com.google.gwt.event.dom.client.HasFocusHandlers;
  21. import com.google.gwt.event.dom.client.HasKeyDownHandlers;
  22. import com.google.gwt.event.dom.client.KeyCodes;
  23. import com.google.gwt.event.dom.client.KeyDownEvent;
  24. import com.google.gwt.event.dom.client.KeyDownHandler;
  25. import com.google.gwt.event.shared.HandlerRegistration;
  26. import com.google.gwt.user.client.Command;
  27. import com.google.gwt.user.client.DOM;
  28. import com.google.gwt.user.client.Element;
  29. import com.google.gwt.user.client.Event;
  30. import com.google.gwt.user.client.ui.ComplexPanel;
  31. import com.google.gwt.user.client.ui.SimplePanel;
  32. import com.google.gwt.user.client.ui.Widget;
  33. import com.google.gwt.user.client.ui.impl.FocusImpl;
  34. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  35. import com.vaadin.terminal.gwt.client.BrowserInfo;
  36. import com.vaadin.terminal.gwt.client.ComponentConnector;
  37. import com.vaadin.terminal.gwt.client.ComponentState;
  38. import com.vaadin.terminal.gwt.client.ConnectorMap;
  39. import com.vaadin.terminal.gwt.client.EventId;
  40. import com.vaadin.terminal.gwt.client.Focusable;
  41. import com.vaadin.terminal.gwt.client.TooltipInfo;
  42. import com.vaadin.terminal.gwt.client.UIDL;
  43. import com.vaadin.terminal.gwt.client.Util;
  44. import com.vaadin.terminal.gwt.client.VCaption;
  45. import com.vaadin.terminal.gwt.client.ui.label.VLabel;
  46. public class VTabsheet extends VTabsheetBase implements Focusable,
  47. FocusHandler, BlurHandler, KeyDownHandler {
  48. private static class VCloseEvent {
  49. private Tab tab;
  50. VCloseEvent(Tab tab) {
  51. this.tab = tab;
  52. }
  53. public Tab getTab() {
  54. return tab;
  55. }
  56. }
  57. private interface VCloseHandler {
  58. public void onClose(VCloseEvent event);
  59. }
  60. /**
  61. * Representation of a single "tab" shown in the TabBar
  62. *
  63. */
  64. private static class Tab extends SimplePanel implements HasFocusHandlers,
  65. HasBlurHandlers, HasKeyDownHandlers {
  66. private static final String TD_CLASSNAME = CLASSNAME + "-tabitemcell";
  67. private static final String TD_FIRST_CLASSNAME = TD_CLASSNAME
  68. + "-first";
  69. private static final String TD_SELECTED_CLASSNAME = TD_CLASSNAME
  70. + "-selected";
  71. private static final String TD_SELECTED_FIRST_CLASSNAME = TD_SELECTED_CLASSNAME
  72. + "-first";
  73. private static final String TD_DISABLED_CLASSNAME = TD_CLASSNAME
  74. + "-disabled";
  75. private static final String DIV_CLASSNAME = CLASSNAME + "-tabitem";
  76. private static final String DIV_SELECTED_CLASSNAME = DIV_CLASSNAME
  77. + "-selected";
  78. private TabCaption tabCaption;
  79. Element td = getElement();
  80. private VCloseHandler closeHandler;
  81. private boolean enabledOnServer = true;
  82. private Element div;
  83. private TabBar tabBar;
  84. private boolean hiddenOnServer = false;
  85. private String styleName;
  86. private Tab(TabBar tabBar) {
  87. super(DOM.createTD());
  88. this.tabBar = tabBar;
  89. setStyleName(td, TD_CLASSNAME);
  90. div = DOM.createDiv();
  91. focusImpl.setTabIndex(td, -1);
  92. setStyleName(div, DIV_CLASSNAME);
  93. DOM.appendChild(td, div);
  94. tabCaption = new TabCaption(this, getTabsheet()
  95. .getApplicationConnection());
  96. add(tabCaption);
  97. addFocusHandler(getTabsheet());
  98. addBlurHandler(getTabsheet());
  99. addKeyDownHandler(getTabsheet());
  100. }
  101. public boolean isHiddenOnServer() {
  102. return hiddenOnServer;
  103. }
  104. public void setHiddenOnServer(boolean hiddenOnServer) {
  105. this.hiddenOnServer = hiddenOnServer;
  106. }
  107. @Override
  108. protected Element getContainerElement() {
  109. // Attach caption element to div, not td
  110. return div;
  111. }
  112. public boolean isEnabledOnServer() {
  113. return enabledOnServer;
  114. }
  115. public void setEnabledOnServer(boolean enabled) {
  116. enabledOnServer = enabled;
  117. setStyleName(td, TD_DISABLED_CLASSNAME, !enabled);
  118. if (!enabled) {
  119. focusImpl.setTabIndex(td, -1);
  120. }
  121. }
  122. public void addClickHandler(ClickHandler handler) {
  123. tabCaption.addClickHandler(handler);
  124. }
  125. public void setCloseHandler(VCloseHandler closeHandler) {
  126. this.closeHandler = closeHandler;
  127. }
  128. /**
  129. * Toggles the style names for the Tab
  130. *
  131. * @param selected
  132. * true if the Tab is selected
  133. * @param first
  134. * true if the Tab is the first visible Tab
  135. */
  136. public void setStyleNames(boolean selected, boolean first) {
  137. // TODO #5100 doesn't belong here
  138. focusImpl.setTabIndex(td, selected ? getTabsheet().tabulatorIndex
  139. : -1);
  140. setStyleName(td, TD_FIRST_CLASSNAME, first);
  141. setStyleName(td, TD_SELECTED_CLASSNAME, selected);
  142. setStyleName(td, TD_SELECTED_FIRST_CLASSNAME, selected && first);
  143. setStyleName(div, DIV_SELECTED_CLASSNAME, selected);
  144. }
  145. public boolean isClosable() {
  146. return tabCaption.isClosable();
  147. }
  148. public void onClose() {
  149. closeHandler.onClose(new VCloseEvent(this));
  150. }
  151. public VTabsheet getTabsheet() {
  152. return tabBar.getTabsheet();
  153. }
  154. public void updateFromUIDL(UIDL tabUidl) {
  155. tabCaption.updateCaption(tabUidl);
  156. // Apply the styleName set for the tab
  157. String newStyleName = tabUidl.getStringAttribute(TAB_STYLE_NAME);
  158. // Find the nth td element
  159. if (newStyleName != null && newStyleName.length() != 0) {
  160. if (!newStyleName.equals(styleName)) {
  161. // If we have a new style name
  162. if (styleName != null && styleName.length() != 0) {
  163. // Remove old style name if present
  164. td.removeClassName(TD_CLASSNAME + "-" + styleName);
  165. }
  166. // Set new style name
  167. td.addClassName(TD_CLASSNAME + "-" + newStyleName);
  168. styleName = newStyleName;
  169. }
  170. } else if (styleName != null) {
  171. // Remove the set stylename if no stylename is present in the
  172. // uidl
  173. td.removeClassName(TD_CLASSNAME + "-" + styleName);
  174. styleName = null;
  175. }
  176. }
  177. public void recalculateCaptionWidth() {
  178. tabCaption.setWidth(tabCaption.getRequiredWidth() + "px");
  179. }
  180. public HandlerRegistration addFocusHandler(FocusHandler handler) {
  181. return addDomHandler(handler, FocusEvent.getType());
  182. }
  183. public HandlerRegistration addBlurHandler(BlurHandler handler) {
  184. return addDomHandler(handler, BlurEvent.getType());
  185. }
  186. public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
  187. return addDomHandler(handler, KeyDownEvent.getType());
  188. }
  189. public void focus() {
  190. focusImpl.focus(td);
  191. }
  192. public void blur() {
  193. focusImpl.blur(td);
  194. }
  195. }
  196. private static class TabCaption extends VCaption {
  197. private boolean closable = false;
  198. private Element closeButton;
  199. private Tab tab;
  200. private ApplicationConnection client;
  201. TabCaption(Tab tab, ApplicationConnection client) {
  202. super(client);
  203. this.client = client;
  204. this.tab = tab;
  205. }
  206. @Override
  207. public boolean updateCaption(UIDL uidl) {
  208. if (uidl.hasAttribute(TabsheetBaseConnector.ATTRIBUTE_TAB_DESCRIPTION)) {
  209. TooltipInfo tooltipInfo = new TooltipInfo();
  210. tooltipInfo
  211. .setTitle(uidl
  212. .getStringAttribute(TabsheetBaseConnector.ATTRIBUTE_TAB_DESCRIPTION));
  213. // TODO currently, there is no error indicator and message for a tab
  214. client.registerTooltip(getTabsheet(), getElement(), tooltipInfo);
  215. } else {
  216. client.registerTooltip(getTabsheet(), getElement(), null);
  217. }
  218. // TODO need to call this instead of super because the caption does
  219. // not have an owner
  220. boolean ret = updateCaptionWithoutOwner(
  221. uidl,
  222. uidl.getStringAttribute(TabsheetBaseConnector.ATTRIBUTE_TAB_CAPTION),
  223. uidl.hasAttribute(TabsheetBaseConnector.ATTRIBUTE_TAB_DISABLED),
  224. uidl.hasAttribute(TabsheetBaseConnector.ATTRIBUTE_TAB_DESCRIPTION),
  225. uidl.getStringAttribute(TabsheetBaseConnector.ATTRIBUTE_TAB_ICON));
  226. setClosable(uidl.hasAttribute("closable"));
  227. return ret;
  228. }
  229. private VTabsheet getTabsheet() {
  230. return tab.getTabsheet();
  231. }
  232. @Override
  233. public void onBrowserEvent(Event event) {
  234. if (closable && event.getTypeInt() == Event.ONCLICK
  235. && event.getEventTarget().cast() == closeButton) {
  236. tab.onClose();
  237. event.stopPropagation();
  238. event.preventDefault();
  239. }
  240. super.onBrowserEvent(event);
  241. if (event.getTypeInt() == Event.ONLOAD) {
  242. getTabsheet().tabSizeMightHaveChanged(getTab());
  243. }
  244. client.handleTooltipEvent(event, getTabsheet(), getElement());
  245. }
  246. public Tab getTab() {
  247. return tab;
  248. }
  249. public void setClosable(boolean closable) {
  250. this.closable = closable;
  251. if (closable && closeButton == null) {
  252. closeButton = DOM.createSpan();
  253. closeButton.setInnerHTML("×");
  254. closeButton
  255. .setClassName(VTabsheet.CLASSNAME + "-caption-close");
  256. getElement().insertBefore(closeButton,
  257. getElement().getLastChild());
  258. } else if (!closable && closeButton != null) {
  259. getElement().removeChild(closeButton);
  260. closeButton = null;
  261. }
  262. if (closable) {
  263. addStyleDependentName("closable");
  264. } else {
  265. removeStyleDependentName("closable");
  266. }
  267. }
  268. public boolean isClosable() {
  269. return closable;
  270. }
  271. @Override
  272. public int getRequiredWidth() {
  273. int width = super.getRequiredWidth();
  274. if (closeButton != null) {
  275. width += Util.getRequiredWidth(closeButton);
  276. }
  277. return width;
  278. }
  279. }
  280. static class TabBar extends ComplexPanel implements ClickHandler,
  281. VCloseHandler {
  282. private final Element tr = DOM.createTR();
  283. private final Element spacerTd = DOM.createTD();
  284. private Tab selected;
  285. private VTabsheet tabsheet;
  286. TabBar(VTabsheet tabsheet) {
  287. this.tabsheet = tabsheet;
  288. Element el = DOM.createTable();
  289. Element tbody = DOM.createTBody();
  290. DOM.appendChild(el, tbody);
  291. DOM.appendChild(tbody, tr);
  292. setStyleName(spacerTd, CLASSNAME + "-spacertd");
  293. DOM.appendChild(tr, spacerTd);
  294. DOM.appendChild(spacerTd, DOM.createDiv());
  295. setElement(el);
  296. }
  297. public void onClose(VCloseEvent event) {
  298. Tab tab = event.getTab();
  299. if (!tab.isEnabledOnServer()) {
  300. return;
  301. }
  302. int tabIndex = getWidgetIndex(tab);
  303. getTabsheet().sendTabClosedEvent(tabIndex);
  304. }
  305. protected Element getContainerElement() {
  306. return tr;
  307. }
  308. public int getTabCount() {
  309. return getWidgetCount();
  310. }
  311. public Tab addTab() {
  312. Tab t = new Tab(this);
  313. int tabIndex = getTabCount();
  314. // Logical attach
  315. insert(t, tr, tabIndex, true);
  316. if (tabIndex == 0) {
  317. // Set the "first" style
  318. t.setStyleNames(false, true);
  319. }
  320. t.addClickHandler(this);
  321. t.setCloseHandler(this);
  322. return t;
  323. }
  324. public void onClick(ClickEvent event) {
  325. Widget caption = (Widget) event.getSource();
  326. int index = getWidgetIndex(caption.getParent());
  327. getTabsheet().onTabSelected(index);
  328. }
  329. public VTabsheet getTabsheet() {
  330. return tabsheet;
  331. }
  332. public Tab getTab(int index) {
  333. if (index < 0 || index >= getTabCount()) {
  334. return null;
  335. }
  336. return (Tab) super.getWidget(index);
  337. }
  338. public void selectTab(int index) {
  339. final Tab newSelected = getTab(index);
  340. final Tab oldSelected = selected;
  341. newSelected.setStyleNames(true, isFirstVisibleTab(index));
  342. if (oldSelected != null && oldSelected != newSelected) {
  343. oldSelected.setStyleNames(false,
  344. isFirstVisibleTab(getWidgetIndex(oldSelected)));
  345. }
  346. // Update the field holding the currently selected tab
  347. selected = newSelected;
  348. // The selected tab might need more (or less) space
  349. newSelected.recalculateCaptionWidth();
  350. getTab(tabsheet.activeTabIndex).recalculateCaptionWidth();
  351. }
  352. public void removeTab(int i) {
  353. Tab tab = getTab(i);
  354. if (tab == null) {
  355. return;
  356. }
  357. remove(tab);
  358. /*
  359. * If this widget was selected we need to unmark it as the last
  360. * selected
  361. */
  362. if (tab == selected) {
  363. selected = null;
  364. }
  365. // FIXME: Shouldn't something be selected instead?
  366. }
  367. private boolean isFirstVisibleTab(int index) {
  368. return getFirstVisibleTab() == index;
  369. }
  370. /**
  371. * Returns the index of the first visible tab
  372. *
  373. * @return
  374. */
  375. private int getFirstVisibleTab() {
  376. return getNextVisibleTab(-1);
  377. }
  378. /**
  379. * Find the next visible tab. Returns -1 if none is found.
  380. *
  381. * @param i
  382. * @return
  383. */
  384. private int getNextVisibleTab(int i) {
  385. int tabs = getTabCount();
  386. do {
  387. i++;
  388. } while (i < tabs && getTab(i).isHiddenOnServer());
  389. if (i == tabs) {
  390. return -1;
  391. } else {
  392. return i;
  393. }
  394. }
  395. /**
  396. * Find the previous visible tab. Returns -1 if none is found.
  397. *
  398. * @param i
  399. * @return
  400. */
  401. private int getPreviousVisibleTab(int i) {
  402. do {
  403. i--;
  404. } while (i >= 0 && getTab(i).isHiddenOnServer());
  405. return i;
  406. }
  407. public int scrollLeft(int currentFirstVisible) {
  408. int prevVisible = getPreviousVisibleTab(currentFirstVisible);
  409. if (prevVisible == -1) {
  410. return -1;
  411. }
  412. Tab newFirst = getTab(prevVisible);
  413. newFirst.setVisible(true);
  414. newFirst.recalculateCaptionWidth();
  415. return prevVisible;
  416. }
  417. public int scrollRight(int currentFirstVisible) {
  418. int nextVisible = getNextVisibleTab(currentFirstVisible);
  419. if (nextVisible == -1) {
  420. return -1;
  421. }
  422. Tab currentFirst = getTab(currentFirstVisible);
  423. currentFirst.setVisible(false);
  424. currentFirst.recalculateCaptionWidth();
  425. return nextVisible;
  426. }
  427. }
  428. public static final String CLASSNAME = "v-tabsheet";
  429. public static final String TABS_CLASSNAME = "v-tabsheet-tabcontainer";
  430. public static final String SCROLLER_CLASSNAME = "v-tabsheet-scroller";
  431. // Can't use "style" as it's already in use
  432. public static final String TAB_STYLE_NAME = "tabstyle";
  433. final Element tabs; // tabbar and 'scroller' container
  434. Tab focusedTab;
  435. int tabulatorIndex = 0;
  436. private static final FocusImpl focusImpl = FocusImpl.getFocusImplForPanel();
  437. private final Element scroller; // tab-scroller element
  438. private final Element scrollerNext; // tab-scroller next button element
  439. private final Element scrollerPrev; // tab-scroller prev button element
  440. /**
  441. * The index of the first visible tab (when scrolled)
  442. */
  443. private int scrollerIndex = 0;
  444. final TabBar tb = new TabBar(this);
  445. final VTabsheetPanel tp = new VTabsheetPanel();
  446. final Element contentNode;
  447. private final Element deco;
  448. boolean waitingForResponse;
  449. private String currentStyle;
  450. /**
  451. * @return Whether the tab could be selected or not.
  452. */
  453. private boolean onTabSelected(final int tabIndex) {
  454. if (disabled || waitingForResponse) {
  455. return false;
  456. }
  457. final Object tabKey = tabKeys.get(tabIndex);
  458. if (disabledTabKeys.contains(tabKey)) {
  459. return false;
  460. }
  461. if (client != null && activeTabIndex != tabIndex) {
  462. tb.selectTab(tabIndex);
  463. if (focusedTab != null) {
  464. focusedTab = tb.getTab(tabIndex);
  465. }
  466. addStyleDependentName("loading");
  467. // Hide the current contents so a loading indicator can be shown
  468. // instead
  469. Widget currentlyDisplayedWidget = tp.getWidget(tp
  470. .getVisibleWidget());
  471. currentlyDisplayedWidget.getElement().getParentElement().getStyle()
  472. .setVisibility(Visibility.HIDDEN);
  473. client.updateVariable(id, "selected", tabKeys.get(tabIndex)
  474. .toString(), true);
  475. waitingForResponse = true;
  476. return true;
  477. }
  478. return false;
  479. }
  480. public ApplicationConnection getApplicationConnection() {
  481. return client;
  482. }
  483. public void tabSizeMightHaveChanged(Tab tab) {
  484. // icon onloads may change total width of tabsheet
  485. if (isDynamicWidth()) {
  486. updateDynamicWidth();
  487. }
  488. updateTabScroller();
  489. }
  490. void sendTabClosedEvent(int tabIndex) {
  491. client.updateVariable(id, "close", tabKeys.get(tabIndex), true);
  492. }
  493. boolean isDynamicWidth() {
  494. ComponentConnector paintable = ConnectorMap.get(client).getConnector(
  495. this);
  496. return paintable.isUndefinedWidth();
  497. }
  498. boolean isDynamicHeight() {
  499. ComponentConnector paintable = ConnectorMap.get(client).getConnector(
  500. this);
  501. return paintable.isUndefinedHeight();
  502. }
  503. public VTabsheet() {
  504. super(CLASSNAME);
  505. addHandler(this, FocusEvent.getType());
  506. addHandler(this, BlurEvent.getType());
  507. // Tab scrolling
  508. DOM.setStyleAttribute(getElement(), "overflow", "hidden");
  509. tabs = DOM.createDiv();
  510. DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
  511. scroller = DOM.createDiv();
  512. DOM.setElementProperty(scroller, "className", SCROLLER_CLASSNAME);
  513. scrollerPrev = DOM.createButton();
  514. DOM.setElementProperty(scrollerPrev, "className", SCROLLER_CLASSNAME
  515. + "Prev");
  516. DOM.sinkEvents(scrollerPrev, Event.ONCLICK);
  517. scrollerNext = DOM.createButton();
  518. DOM.setElementProperty(scrollerNext, "className", SCROLLER_CLASSNAME
  519. + "Next");
  520. DOM.sinkEvents(scrollerNext, Event.ONCLICK);
  521. DOM.appendChild(getElement(), tabs);
  522. // Tabs
  523. tp.setStyleName(CLASSNAME + "-tabsheetpanel");
  524. contentNode = DOM.createDiv();
  525. deco = DOM.createDiv();
  526. addStyleDependentName("loading"); // Indicate initial progress
  527. tb.setStyleName(CLASSNAME + "-tabs");
  528. DOM.setElementProperty(contentNode, "className", CLASSNAME + "-content");
  529. DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
  530. add(tb, tabs);
  531. DOM.appendChild(scroller, scrollerPrev);
  532. DOM.appendChild(scroller, scrollerNext);
  533. DOM.appendChild(getElement(), contentNode);
  534. add(tp, contentNode);
  535. DOM.appendChild(getElement(), deco);
  536. DOM.appendChild(tabs, scroller);
  537. // TODO Use for Safari only. Fix annoying 1px first cell in TabBar.
  538. // DOM.setStyleAttribute(DOM.getFirstChild(DOM.getFirstChild(DOM
  539. // .getFirstChild(tb.getElement()))), "display", "none");
  540. }
  541. @Override
  542. public void onBrowserEvent(Event event) {
  543. // Tab scrolling
  544. if (isScrolledTabs() && DOM.eventGetTarget(event) == scrollerPrev) {
  545. int newFirstIndex = tb.scrollLeft(scrollerIndex);
  546. if (newFirstIndex != -1) {
  547. scrollerIndex = newFirstIndex;
  548. updateTabScroller();
  549. }
  550. } else if (isClippedTabs() && DOM.eventGetTarget(event) == scrollerNext) {
  551. int newFirstIndex = tb.scrollRight(scrollerIndex);
  552. if (newFirstIndex != -1) {
  553. scrollerIndex = newFirstIndex;
  554. updateTabScroller();
  555. }
  556. } else {
  557. super.onBrowserEvent(event);
  558. }
  559. }
  560. /**
  561. * Checks if the tab with the selected index has been scrolled out of the
  562. * view (on the left side).
  563. *
  564. * @param index
  565. * @return
  566. */
  567. private boolean scrolledOutOfView(int index) {
  568. return scrollerIndex > index;
  569. }
  570. void handleStyleNames(UIDL uidl, ComponentState state) {
  571. // Add proper stylenames for all elements (easier to prevent unwanted
  572. // style inheritance)
  573. if (state.hasStyles()) {
  574. final List<String> styles = state.getStyles();
  575. if (!currentStyle.equals(styles.toString())) {
  576. currentStyle = styles.toString();
  577. final String tabsBaseClass = TABS_CLASSNAME;
  578. String tabsClass = tabsBaseClass;
  579. final String contentBaseClass = CLASSNAME + "-content";
  580. String contentClass = contentBaseClass;
  581. final String decoBaseClass = CLASSNAME + "-deco";
  582. String decoClass = decoBaseClass;
  583. for (String style : styles) {
  584. tb.addStyleDependentName(style);
  585. tabsClass += " " + tabsBaseClass + "-" + style;
  586. contentClass += " " + contentBaseClass + "-" + style;
  587. decoClass += " " + decoBaseClass + "-" + style;
  588. }
  589. DOM.setElementProperty(tabs, "className", tabsClass);
  590. DOM.setElementProperty(contentNode, "className", contentClass);
  591. DOM.setElementProperty(deco, "className", decoClass);
  592. borderW = -1;
  593. }
  594. } else {
  595. tb.setStyleName(CLASSNAME + "-tabs");
  596. DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
  597. DOM.setElementProperty(contentNode, "className", CLASSNAME
  598. + "-content");
  599. DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
  600. }
  601. if (uidl.hasAttribute("hidetabs")) {
  602. tb.setVisible(false);
  603. addStyleName(CLASSNAME + "-hidetabs");
  604. } else {
  605. tb.setVisible(true);
  606. removeStyleName(CLASSNAME + "-hidetabs");
  607. }
  608. }
  609. void updateDynamicWidth() {
  610. // Find width consumed by tabs
  611. TableCellElement spacerCell = ((TableElement) tb.getElement().cast())
  612. .getRows().getItem(0).getCells().getItem(tb.getTabCount());
  613. int spacerWidth = spacerCell.getOffsetWidth();
  614. DivElement div = (DivElement) spacerCell.getFirstChildElement();
  615. int spacerMinWidth = spacerCell.getOffsetWidth() - div.getOffsetWidth();
  616. int tabsWidth = tb.getOffsetWidth() - spacerWidth + spacerMinWidth;
  617. // Find content width
  618. Style style = tp.getElement().getStyle();
  619. String overflow = style.getProperty("overflow");
  620. style.setProperty("overflow", "hidden");
  621. style.setPropertyPx("width", tabsWidth);
  622. boolean hasTabs = tp.getWidgetCount() > 0;
  623. Style wrapperstyle = null;
  624. if (hasTabs) {
  625. wrapperstyle = tp.getWidget(tp.getVisibleWidget()).getElement()
  626. .getParentElement().getStyle();
  627. wrapperstyle.setPropertyPx("width", tabsWidth);
  628. }
  629. // Get content width from actual widget
  630. int contentWidth = 0;
  631. if (hasTabs) {
  632. contentWidth = tp.getWidget(tp.getVisibleWidget()).getOffsetWidth();
  633. }
  634. style.setProperty("overflow", overflow);
  635. // Set widths to max(tabs,content)
  636. if (tabsWidth < contentWidth) {
  637. tabsWidth = contentWidth;
  638. }
  639. int outerWidth = tabsWidth + getContentAreaBorderWidth();
  640. tabs.getStyle().setPropertyPx("width", outerWidth);
  641. style.setPropertyPx("width", tabsWidth);
  642. if (hasTabs) {
  643. wrapperstyle.setPropertyPx("width", tabsWidth);
  644. }
  645. contentNode.getStyle().setPropertyPx("width", tabsWidth);
  646. super.setWidth(outerWidth + "px");
  647. updateOpenTabSize();
  648. }
  649. @Override
  650. protected void renderTab(final UIDL tabUidl, int index, boolean selected,
  651. boolean hidden) {
  652. Tab tab = tb.getTab(index);
  653. if (tab == null) {
  654. tab = tb.addTab();
  655. }
  656. tab.updateFromUIDL(tabUidl);
  657. tab.setEnabledOnServer((!disabledTabKeys.contains(tabKeys.get(index))));
  658. tab.setHiddenOnServer(hidden);
  659. if (scrolledOutOfView(index)) {
  660. // Should not set tabs visible if they are scrolled out of view
  661. hidden = true;
  662. }
  663. // Set the current visibility of the tab (in the browser)
  664. tab.setVisible(!hidden);
  665. /*
  666. * Force the width of the caption container so the content will not wrap
  667. * and tabs won't be too narrow in certain browsers
  668. */
  669. tab.recalculateCaptionWidth();
  670. UIDL tabContentUIDL = null;
  671. ComponentConnector tabContentPaintable = null;
  672. Widget tabContentWidget = null;
  673. if (tabUidl.getChildCount() > 0) {
  674. tabContentUIDL = tabUidl.getChildUIDL(0);
  675. tabContentPaintable = client.getPaintable(tabContentUIDL);
  676. tabContentWidget = tabContentPaintable.getWidget();
  677. }
  678. if (tabContentPaintable != null) {
  679. /* This is a tab with content information */
  680. int oldIndex = tp.getWidgetIndex(tabContentWidget);
  681. if (oldIndex != -1 && oldIndex != index) {
  682. /*
  683. * The tab has previously been rendered in another position so
  684. * we must move the cached content to correct position
  685. */
  686. tp.insert(tabContentWidget, index);
  687. }
  688. } else {
  689. /* A tab whose content has not yet been loaded */
  690. /*
  691. * Make sure there is a corresponding empty tab in tp. The same
  692. * operation as the moving above but for not-loaded tabs.
  693. */
  694. if (index < tp.getWidgetCount()) {
  695. Widget oldWidget = tp.getWidget(index);
  696. if (!(oldWidget instanceof PlaceHolder)) {
  697. tp.insert(new PlaceHolder(), index);
  698. }
  699. }
  700. }
  701. if (selected) {
  702. renderContent(tabContentUIDL);
  703. tb.selectTab(index);
  704. } else {
  705. if (tabContentUIDL != null) {
  706. // updating a drawn child on hidden tab
  707. if (tp.getWidgetIndex(tabContentWidget) < 0) {
  708. tp.insert(tabContentWidget, index);
  709. }
  710. tabContentPaintable.updateFromUIDL(tabContentUIDL, client);
  711. } else if (tp.getWidgetCount() <= index) {
  712. tp.add(new PlaceHolder());
  713. }
  714. }
  715. }
  716. public class PlaceHolder extends VLabel {
  717. public PlaceHolder() {
  718. super("");
  719. }
  720. }
  721. @Override
  722. protected void selectTab(int index, final UIDL contentUidl) {
  723. if (index != activeTabIndex) {
  724. activeTabIndex = index;
  725. tb.selectTab(activeTabIndex);
  726. }
  727. renderContent(contentUidl);
  728. }
  729. private void renderContent(final UIDL contentUIDL) {
  730. final ComponentConnector content = client.getPaintable(contentUIDL);
  731. Widget newWidget = content.getWidget();
  732. if (tp.getWidgetCount() > activeTabIndex) {
  733. Widget old = tp.getWidget(activeTabIndex);
  734. if (old != newWidget) {
  735. tp.remove(activeTabIndex);
  736. ConnectorMap paintableMap = ConnectorMap.get(client);
  737. if (paintableMap.isConnector(old)) {
  738. paintableMap.unregisterConnector(paintableMap
  739. .getConnector(old));
  740. }
  741. tp.insert(content.getWidget(), activeTabIndex);
  742. }
  743. } else {
  744. tp.add(content.getWidget());
  745. }
  746. tp.showWidget(activeTabIndex);
  747. VTabsheet.this.iLayout();
  748. content.updateFromUIDL(contentUIDL, client);
  749. /*
  750. * The size of a cached, relative sized component must be updated to
  751. * report correct size to updateOpenTabSize().
  752. */
  753. if (contentUIDL.getBooleanAttribute("cached")) {
  754. client.handleComponentRelativeSize(content.getWidget());
  755. }
  756. updateOpenTabSize();
  757. VTabsheet.this.removeStyleDependentName("loading");
  758. }
  759. void updateContentNodeHeight() {
  760. if (!isDynamicHeight()) {
  761. int contentHeight = getOffsetHeight();
  762. contentHeight -= DOM.getElementPropertyInt(deco, "offsetHeight");
  763. contentHeight -= tb.getOffsetHeight();
  764. if (contentHeight < 0) {
  765. contentHeight = 0;
  766. }
  767. // Set proper values for content element
  768. DOM.setStyleAttribute(contentNode, "height", contentHeight + "px");
  769. } else {
  770. DOM.setStyleAttribute(contentNode, "height", "");
  771. }
  772. }
  773. public void iLayout() {
  774. updateTabScroller();
  775. tp.runWebkitOverflowAutoFix();
  776. }
  777. /**
  778. * Sets the size of the visible tab (component). As the tab is set to
  779. * position: absolute (to work around a firefox flickering bug) we must keep
  780. * this up-to-date by hand.
  781. */
  782. void updateOpenTabSize() {
  783. /*
  784. * The overflow=auto element must have a height specified, otherwise it
  785. * will be just as high as the contents and no scrollbars will appear
  786. */
  787. int height = -1;
  788. int width = -1;
  789. int minWidth = 0;
  790. if (!isDynamicHeight()) {
  791. height = contentNode.getOffsetHeight();
  792. }
  793. if (!isDynamicWidth()) {
  794. width = contentNode.getOffsetWidth() - getContentAreaBorderWidth();
  795. } else {
  796. /*
  797. * If the tabbar is wider than the content we need to use the tabbar
  798. * width as minimum width so scrollbars get placed correctly (at the
  799. * right edge).
  800. */
  801. minWidth = tb.getOffsetWidth() - getContentAreaBorderWidth();
  802. }
  803. tp.fixVisibleTabSize(width, height, minWidth);
  804. }
  805. /**
  806. * Layouts the tab-scroller elements, and applies styles.
  807. */
  808. private void updateTabScroller() {
  809. if (!isDynamicWidth()) {
  810. ComponentConnector paintable = ConnectorMap.get(client)
  811. .getConnector(this);
  812. DOM.setStyleAttribute(tabs, "width", paintable.getState()
  813. .getWidth());
  814. }
  815. // Make sure scrollerIndex is valid
  816. if (scrollerIndex < 0 || scrollerIndex > tb.getTabCount()) {
  817. scrollerIndex = tb.getFirstVisibleTab();
  818. } else if (tb.getTabCount() > 0
  819. && tb.getTab(scrollerIndex).isHiddenOnServer()) {
  820. scrollerIndex = tb.getNextVisibleTab(scrollerIndex);
  821. }
  822. boolean scrolled = isScrolledTabs();
  823. boolean clipped = isClippedTabs();
  824. if (tb.getTabCount() > 0 && tb.isVisible() && (scrolled || clipped)) {
  825. DOM.setStyleAttribute(scroller, "display", "");
  826. DOM.setElementProperty(scrollerPrev, "className",
  827. SCROLLER_CLASSNAME + (scrolled ? "Prev" : "Prev-disabled"));
  828. DOM.setElementProperty(scrollerNext, "className",
  829. SCROLLER_CLASSNAME + (clipped ? "Next" : "Next-disabled"));
  830. } else {
  831. DOM.setStyleAttribute(scroller, "display", "none");
  832. }
  833. if (BrowserInfo.get().isSafari()) {
  834. // fix tab height for safari, bugs sometimes if tabs contain icons
  835. String property = tabs.getStyle().getProperty("height");
  836. if (property == null || property.equals("")) {
  837. tabs.getStyle().setPropertyPx("height", tb.getOffsetHeight());
  838. }
  839. /*
  840. * another hack for webkits. tabscroller sometimes drops without
  841. * "shaking it" reproducable in
  842. * com.vaadin.tests.components.tabsheet.TabSheetIcons
  843. */
  844. final Style style = scroller.getStyle();
  845. style.setProperty("whiteSpace", "normal");
  846. Scheduler.get().scheduleDeferred(new Command() {
  847. public void execute() {
  848. style.setProperty("whiteSpace", "");
  849. }
  850. });
  851. }
  852. }
  853. void showAllTabs() {
  854. scrollerIndex = tb.getFirstVisibleTab();
  855. for (int i = 0; i < tb.getTabCount(); i++) {
  856. Tab t = tb.getTab(i);
  857. if (!t.isHiddenOnServer()) {
  858. t.setVisible(true);
  859. }
  860. }
  861. }
  862. private boolean isScrolledTabs() {
  863. return scrollerIndex > tb.getFirstVisibleTab();
  864. }
  865. private boolean isClippedTabs() {
  866. return (tb.getOffsetWidth() - DOM.getElementPropertyInt((Element) tb
  867. .getContainerElement().getLastChild().cast(), "offsetWidth")) > getOffsetWidth()
  868. - (isScrolledTabs() ? scroller.getOffsetWidth() : 0);
  869. }
  870. @Override
  871. protected void clearPaintables() {
  872. int i = tb.getTabCount();
  873. while (i > 0) {
  874. tb.removeTab(--i);
  875. }
  876. tp.clear();
  877. }
  878. @Override
  879. protected Iterator<Widget> getWidgetIterator() {
  880. return tp.iterator();
  881. }
  882. private int borderW = -1;
  883. int getContentAreaBorderWidth() {
  884. if (borderW < 0) {
  885. borderW = Util.measureHorizontalBorder(contentNode);
  886. }
  887. return borderW;
  888. }
  889. @Override
  890. protected int getTabCount() {
  891. return tb.getTabCount();
  892. }
  893. @Override
  894. protected ComponentConnector getTab(int index) {
  895. if (tp.getWidgetCount() > index) {
  896. Widget widget = tp.getWidget(index);
  897. return ConnectorMap.get(client).getConnector(widget);
  898. }
  899. return null;
  900. }
  901. @Override
  902. protected void removeTab(int index) {
  903. tb.removeTab(index);
  904. /*
  905. * This must be checked because renderTab automatically removes the
  906. * active tab content when it changes
  907. */
  908. if (tp.getWidgetCount() > index) {
  909. tp.remove(index);
  910. }
  911. }
  912. public void onBlur(BlurEvent event) {
  913. if (focusedTab != null && event.getSource() instanceof Tab) {
  914. focusedTab = null;
  915. if (client.hasEventListeners(this, EventId.BLUR)) {
  916. client.updateVariable(id, EventId.BLUR, "", true);
  917. }
  918. }
  919. }
  920. public void onFocus(FocusEvent event) {
  921. if (focusedTab == null && event.getSource() instanceof Tab) {
  922. focusedTab = (Tab) event.getSource();
  923. if (client.hasEventListeners(this, EventId.FOCUS)) {
  924. client.updateVariable(id, EventId.FOCUS, "", true);
  925. }
  926. }
  927. }
  928. public void focus() {
  929. tb.getTab(activeTabIndex).focus();
  930. }
  931. public void blur() {
  932. tb.getTab(activeTabIndex).blur();
  933. }
  934. public void onKeyDown(KeyDownEvent event) {
  935. if (event.getSource() instanceof Tab) {
  936. int keycode = event.getNativeEvent().getKeyCode();
  937. if (keycode == getPreviousTabKey()) {
  938. int newTabIndex = activeTabIndex;
  939. // Find the previous non-disabled tab with wraparound.
  940. do {
  941. newTabIndex = (newTabIndex != 0) ? newTabIndex - 1 : tb
  942. .getTabCount() - 1;
  943. } while (newTabIndex != activeTabIndex
  944. && !onTabSelected(newTabIndex));
  945. activeTabIndex = newTabIndex;
  946. // Tab scrolling
  947. if (isScrolledTabs()) {
  948. int newFirstIndex = tb.scrollLeft(scrollerIndex);
  949. if (newFirstIndex != -1) {
  950. scrollerIndex = newFirstIndex;
  951. updateTabScroller();
  952. }
  953. }
  954. } else if (keycode == getNextTabKey()) {
  955. int newTabIndex = activeTabIndex;
  956. // Find the next non-disabled tab with wraparound.
  957. do {
  958. newTabIndex = (newTabIndex + 1) % tb.getTabCount();
  959. } while (newTabIndex != activeTabIndex
  960. && !onTabSelected(newTabIndex));
  961. activeTabIndex = newTabIndex;
  962. if (isClippedTabs()) {
  963. int newFirstIndex = tb.scrollRight(scrollerIndex);
  964. if (newFirstIndex != -1) {
  965. scrollerIndex = newFirstIndex;
  966. updateTabScroller();
  967. }
  968. }
  969. } else if (keycode == getCloseTabKey()) {
  970. Tab tab = tb.getTab(activeTabIndex);
  971. if (tab.isClosable()) {
  972. tab.onClose();
  973. removeTab(activeTabIndex);
  974. }
  975. }
  976. }
  977. }
  978. protected int getPreviousTabKey() {
  979. return KeyCodes.KEY_LEFT;
  980. }
  981. protected int getNextTabKey() {
  982. return KeyCodes.KEY_RIGHT;
  983. }
  984. protected int getCloseTabKey() {
  985. return KeyCodes.KEY_DELETE;
  986. }
  987. }