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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.Set;
  8. import com.google.gwt.dom.client.DivElement;
  9. import com.google.gwt.dom.client.Style;
  10. import com.google.gwt.dom.client.TableCellElement;
  11. import com.google.gwt.dom.client.TableElement;
  12. import com.google.gwt.event.dom.client.ClickEvent;
  13. import com.google.gwt.event.dom.client.ClickHandler;
  14. import com.google.gwt.user.client.Command;
  15. import com.google.gwt.user.client.DOM;
  16. import com.google.gwt.user.client.DeferredCommand;
  17. import com.google.gwt.user.client.Element;
  18. import com.google.gwt.user.client.Event;
  19. import com.google.gwt.user.client.ui.ComplexPanel;
  20. import com.google.gwt.user.client.ui.Widget;
  21. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  22. import com.vaadin.terminal.gwt.client.BrowserInfo;
  23. import com.vaadin.terminal.gwt.client.Paintable;
  24. import com.vaadin.terminal.gwt.client.RenderInformation;
  25. import com.vaadin.terminal.gwt.client.RenderSpace;
  26. import com.vaadin.terminal.gwt.client.TooltipInfo;
  27. import com.vaadin.terminal.gwt.client.UIDL;
  28. import com.vaadin.terminal.gwt.client.Util;
  29. import com.vaadin.terminal.gwt.client.VCaption;
  30. public class VTabsheet extends VTabsheetBase {
  31. private class TabSheetCaption extends VCaption {
  32. private boolean hidden = false;
  33. private boolean closable = false;
  34. private Element closeButton;
  35. TabSheetCaption() {
  36. super(null, client);
  37. }
  38. @Override
  39. public boolean updateCaption(UIDL uidl) {
  40. if (uidl.hasAttribute(ATTRIBUTE_DESCRIPTION)
  41. || uidl.hasAttribute(ATTRIBUTE_ERROR)) {
  42. TooltipInfo tooltipInfo = new TooltipInfo();
  43. tooltipInfo.setTitle(uidl
  44. .getStringAttribute(ATTRIBUTE_DESCRIPTION));
  45. if (uidl.hasAttribute(ATTRIBUTE_ERROR)) {
  46. tooltipInfo.setErrorUidl(uidl.getErrors());
  47. }
  48. client.registerTooltip(VTabsheet.this, getElement(),
  49. tooltipInfo);
  50. } else {
  51. client.registerTooltip(VTabsheet.this, getElement(), null);
  52. }
  53. boolean ret = super.updateCaption(uidl);
  54. setClosable(uidl.hasAttribute("closable"));
  55. return ret;
  56. }
  57. @Override
  58. public void onBrowserEvent(Event event) {
  59. if (closable && event.getTypeInt() == Event.ONCLICK
  60. && event.getEventTarget().cast() == closeButton) {
  61. final String tabKey = tabKeys.get(tb.getTabIndex(this))
  62. .toString();
  63. if (!disabledTabKeys.contains(tabKey)) {
  64. client.updateVariable(id, "close", tabKey, true);
  65. event.stopPropagation();
  66. event.preventDefault();
  67. return;
  68. }
  69. }
  70. super.onBrowserEvent(event);
  71. if (event.getTypeInt() == Event.ONLOAD) {
  72. // icon onloads may change total width of tabsheet
  73. if (isDynamicWidth()) {
  74. updateDynamicWidth();
  75. }
  76. updateTabScroller();
  77. }
  78. client.handleTooltipEvent(event, VTabsheet.this, getElement());
  79. }
  80. @Override
  81. public void setWidth(String width) {
  82. super.setWidth(width);
  83. if (BrowserInfo.get().isIE7()) {
  84. /*
  85. * IE7 apparently has problems with calculating width for
  86. * floated elements inside a DIV with padding. Set the width
  87. * explicitly for the caption.
  88. */
  89. fixTextWidth();
  90. }
  91. }
  92. private void fixTextWidth() {
  93. Element captionText = getTextElement();
  94. if (captionText == null) {
  95. return;
  96. }
  97. int captionWidth = Util.getRequiredWidth(captionText);
  98. int scrollWidth = captionText.getScrollWidth();
  99. if (scrollWidth > captionWidth) {
  100. captionWidth = scrollWidth;
  101. }
  102. captionText.getStyle().setPropertyPx("width", captionWidth);
  103. }
  104. public boolean isHidden() {
  105. return hidden;
  106. }
  107. public void setHidden(boolean hidden) {
  108. this.hidden = hidden;
  109. }
  110. public void setClosable(boolean closable) {
  111. this.closable = closable;
  112. if (closable && closeButton == null) {
  113. closeButton = DOM.createSpan();
  114. closeButton.setInnerHTML("×");
  115. closeButton
  116. .setClassName(VTabsheet.CLASSNAME + "-caption-close");
  117. getElement().insertBefore(closeButton,
  118. getElement().getLastChild());
  119. } else if (!closable && closeButton != null) {
  120. getElement().removeChild(closeButton);
  121. closeButton = null;
  122. }
  123. if (closable) {
  124. addStyleDependentName("closable");
  125. } else {
  126. removeStyleDependentName("closable");
  127. }
  128. }
  129. @Override
  130. public int getRequiredWidth() {
  131. int width = super.getRequiredWidth();
  132. if (closeButton != null) {
  133. width += Util.getRequiredWidth(closeButton);
  134. }
  135. return width;
  136. }
  137. }
  138. class TabBar extends ComplexPanel implements ClickHandler {
  139. private final Element tr = DOM.createTR();
  140. private final Element spacerTd = DOM.createTD();
  141. TabBar() {
  142. Element el = DOM.createTable();
  143. Element tbody = DOM.createTBody();
  144. DOM.appendChild(el, tbody);
  145. DOM.appendChild(tbody, tr);
  146. setStyleName(spacerTd, CLASSNAME + "-spacertd");
  147. DOM.appendChild(tr, spacerTd);
  148. DOM.appendChild(spacerTd, DOM.createDiv());
  149. setElement(el);
  150. }
  151. protected Element getContainerElement() {
  152. return tr;
  153. }
  154. private Widget oldSelected;
  155. public int getTabCount() {
  156. return getWidgetCount();
  157. }
  158. public void addTab(VCaption c) {
  159. Element td = DOM.createTD();
  160. setStyleName(td, CLASSNAME + "-tabitemcell");
  161. if (getWidgetCount() == 0) {
  162. setStyleName(td, CLASSNAME + "-tabitemcell-first", true);
  163. }
  164. Element div = DOM.createDiv();
  165. setStyleName(div, CLASSNAME + "-tabitem");
  166. DOM.appendChild(td, div);
  167. DOM.insertBefore(tr, td, spacerTd);
  168. c.addClickHandler(this);
  169. add(c, div);
  170. }
  171. public void onClick(ClickEvent event) {
  172. int index = getWidgetIndex((Widget) event.getSource());
  173. onTabSelected(index);
  174. }
  175. public void selectTab(int index) {
  176. final String classname = CLASSNAME + "-tabitem-selected";
  177. String classname2 = CLASSNAME + "-tabitemcell-selected"
  178. + (index == 0 ? "-first" : "");
  179. final Widget newSelected = getWidget(index);
  180. final com.google.gwt.dom.client.Element div = newSelected
  181. .getElement().getParentElement();
  182. Widget.setStyleName(div, classname, true);
  183. Widget.setStyleName(div.getParentElement(), classname2, true);
  184. if (oldSelected != null && oldSelected != newSelected) {
  185. classname2 = CLASSNAME + "-tabitemcell-selected"
  186. + (getWidgetIndex(oldSelected) == 0 ? "-first" : "");
  187. final com.google.gwt.dom.client.Element divOld = oldSelected
  188. .getElement().getParentElement();
  189. Widget.setStyleName(divOld, classname, false);
  190. Widget.setStyleName(divOld.getParentElement(), classname2,
  191. false);
  192. }
  193. oldSelected = newSelected;
  194. }
  195. public void removeTab(int i) {
  196. Widget w = getWidget(i);
  197. if (w == null) {
  198. return;
  199. }
  200. Element caption = w.getElement();
  201. Element div = DOM.getParent(caption);
  202. Element td = DOM.getParent(div);
  203. Element tr = DOM.getParent(td);
  204. remove(w);
  205. /*
  206. * Widget is the Caption but we want to remove everything up to and
  207. * including the parent TD
  208. */
  209. DOM.removeChild(tr, td);
  210. /*
  211. * If this widget was selected we need to unmark it as the last
  212. * selected
  213. */
  214. if (w == oldSelected) {
  215. oldSelected = null;
  216. }
  217. }
  218. public TabSheetCaption getTab(int index) {
  219. if (index >= getWidgetCount()) {
  220. return null;
  221. }
  222. return (TabSheetCaption) getWidget(index);
  223. }
  224. public int getTabIndex(TabSheetCaption tab) {
  225. return getChildren().indexOf(tab);
  226. }
  227. public void setVisible(int index, boolean visible) {
  228. com.google.gwt.dom.client.Element e = getTab(index).getElement()
  229. .getParentElement().getParentElement();
  230. if (visible) {
  231. e.getStyle().setProperty("display", "");
  232. } else {
  233. e.getStyle().setProperty("display", "none");
  234. }
  235. }
  236. public void updateCaptionSize(int index) {
  237. VCaption c = getTab(index);
  238. c.setWidth(c.getRequiredWidth() + "px");
  239. }
  240. }
  241. public static final String CLASSNAME = "v-tabsheet";
  242. public static final String TABS_CLASSNAME = "v-tabsheet-tabcontainer";
  243. public static final String SCROLLER_CLASSNAME = "v-tabsheet-scroller";
  244. private final Element tabs; // tabbar and 'scroller' container
  245. private final Element scroller; // tab-scroller element
  246. private final Element scrollerNext; // tab-scroller next button element
  247. private final Element scrollerPrev; // tab-scroller prev button element
  248. /**
  249. * The index of the first visible tab (when scrolled)
  250. */
  251. private int scrollerIndex = 0;
  252. private final TabBar tb = new TabBar();
  253. private final VTabsheetPanel tp = new VTabsheetPanel();
  254. private final Element contentNode, deco;
  255. private final HashMap<String, VCaption> captions = new HashMap<String, VCaption>();
  256. private String height;
  257. private String width;
  258. private boolean waitingForResponse;
  259. private final RenderInformation renderInformation = new RenderInformation();
  260. /**
  261. * Previous visible widget is set invisible with CSS (not display: none, but
  262. * visibility: hidden), to avoid flickering during render process. Normal
  263. * visibility must be returned later when new widget is rendered.
  264. */
  265. private Widget previousVisibleWidget;
  266. private boolean rendering = false;
  267. private String currentStyle;
  268. private void onTabSelected(final int tabIndex) {
  269. if (disabled || waitingForResponse) {
  270. return;
  271. }
  272. final Object tabKey = tabKeys.get(tabIndex);
  273. if (disabledTabKeys.contains(tabKey)) {
  274. return;
  275. }
  276. if (client != null && activeTabIndex != tabIndex) {
  277. tb.selectTab(tabIndex);
  278. addStyleDependentName("loading");
  279. // run updating variables in deferred command to bypass some FF
  280. // optimization issues
  281. DeferredCommand.addCommand(new Command() {
  282. public void execute() {
  283. previousVisibleWidget = tp.getWidget(tp.getVisibleWidget());
  284. DOM.setStyleAttribute(
  285. DOM.getParent(previousVisibleWidget.getElement()),
  286. "visibility", "hidden");
  287. client.updateVariable(id, "selected", tabKeys.get(tabIndex)
  288. .toString(), true);
  289. }
  290. });
  291. waitingForResponse = true;
  292. }
  293. }
  294. private boolean isDynamicWidth() {
  295. return width == null || width.equals("");
  296. }
  297. private boolean isDynamicHeight() {
  298. return height == null || height.equals("");
  299. }
  300. public VTabsheet() {
  301. super(CLASSNAME);
  302. // Tab scrolling
  303. DOM.setStyleAttribute(getElement(), "overflow", "hidden");
  304. tabs = DOM.createDiv();
  305. DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
  306. scroller = DOM.createDiv();
  307. DOM.setElementProperty(scroller, "className", SCROLLER_CLASSNAME);
  308. scrollerPrev = DOM.createButton();
  309. DOM.setElementProperty(scrollerPrev, "className", SCROLLER_CLASSNAME
  310. + "Prev");
  311. DOM.sinkEvents(scrollerPrev, Event.ONCLICK);
  312. scrollerNext = DOM.createButton();
  313. DOM.setElementProperty(scrollerNext, "className", SCROLLER_CLASSNAME
  314. + "Next");
  315. DOM.sinkEvents(scrollerNext, Event.ONCLICK);
  316. DOM.appendChild(getElement(), tabs);
  317. // Tabs
  318. tp.setStyleName(CLASSNAME + "-tabsheetpanel");
  319. contentNode = DOM.createDiv();
  320. deco = DOM.createDiv();
  321. addStyleDependentName("loading"); // Indicate initial progress
  322. tb.setStyleName(CLASSNAME + "-tabs");
  323. DOM.setElementProperty(contentNode, "className", CLASSNAME + "-content");
  324. DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
  325. add(tb, tabs);
  326. DOM.appendChild(scroller, scrollerPrev);
  327. DOM.appendChild(scroller, scrollerNext);
  328. DOM.appendChild(getElement(), contentNode);
  329. add(tp, contentNode);
  330. DOM.appendChild(getElement(), deco);
  331. DOM.appendChild(tabs, scroller);
  332. // TODO Use for Safari only. Fix annoying 1px first cell in TabBar.
  333. // DOM.setStyleAttribute(DOM.getFirstChild(DOM.getFirstChild(DOM
  334. // .getFirstChild(tb.getElement()))), "display", "none");
  335. }
  336. @Override
  337. public void onBrowserEvent(Event event) {
  338. // Tab scrolling
  339. if (isScrolledTabs() && DOM.eventGetTarget(event) == scrollerPrev) {
  340. int prevVisible = getPreviousVisibleTab(scrollerIndex);
  341. if (prevVisible != -1) {
  342. tb.setVisible(prevVisible, true);
  343. tb.updateCaptionSize(prevVisible);
  344. scrollerIndex = prevVisible;
  345. updateTabScroller();
  346. }
  347. } else if (isClippedTabs() && DOM.eventGetTarget(event) == scrollerNext) {
  348. int firstVisible = scrollerIndex;
  349. int nextVisible = getNextVisibleTab(firstVisible);
  350. if (nextVisible != -1) {
  351. tb.setVisible(firstVisible, false);
  352. tb.updateCaptionSize(firstVisible);
  353. scrollerIndex = nextVisible;
  354. updateTabScroller();
  355. }
  356. } else {
  357. super.onBrowserEvent(event);
  358. }
  359. }
  360. /**
  361. * Find the next visible tab. Returns -1 if none is found.
  362. *
  363. * @param i
  364. * @return
  365. */
  366. private int getNextVisibleTab(int i) {
  367. int tabs = tb.getTabCount();
  368. do {
  369. i++;
  370. } while (i < tabs && tb.getTab(i).isHidden());
  371. if (i == tabs) {
  372. return -1;
  373. } else {
  374. return i;
  375. }
  376. }
  377. /**
  378. * Find the previous visible tab. Returns -1 if none is found.
  379. *
  380. * @param i
  381. * @return
  382. */
  383. private int getPreviousVisibleTab(int i) {
  384. do {
  385. i--;
  386. } while (i >= 0 && tb.getTab(i).isHidden());
  387. return i;
  388. }
  389. /**
  390. * Checks if the tab with the selected index has been scrolled out of the
  391. * view (on the left side).
  392. *
  393. * @param index
  394. * @return
  395. */
  396. private boolean scrolledOutOfView(int index) {
  397. return scrollerIndex > index;
  398. }
  399. @Override
  400. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  401. rendering = true;
  402. if (!uidl.getBooleanAttribute("cached")) {
  403. // Handle stylename changes before generics (might affect size
  404. // calculations)
  405. handleStyleNames(uidl);
  406. }
  407. super.updateFromUIDL(uidl, client);
  408. if (cachedUpdate) {
  409. rendering = false;
  410. return;
  411. }
  412. // tabs; push or not
  413. if (!isDynamicWidth()) {
  414. // FIXME: This makes tab sheet tabs go to 1px width on every update
  415. // and then back to original width
  416. // update width later, in updateTabScroller();
  417. DOM.setStyleAttribute(tabs, "width", "1px");
  418. DOM.setStyleAttribute(tabs, "overflow", "hidden");
  419. } else {
  420. showAllTabs();
  421. DOM.setStyleAttribute(tabs, "width", "");
  422. DOM.setStyleAttribute(tabs, "overflow", "visible");
  423. updateDynamicWidth();
  424. }
  425. if (!isDynamicHeight()) {
  426. // Must update height after the styles have been set
  427. updateContentNodeHeight();
  428. updateOpenTabSize();
  429. }
  430. iLayout();
  431. // Re run relative size update to ensure optimal scrollbars
  432. // TODO isolate to situation that visible tab has undefined height
  433. try {
  434. client.handleComponentRelativeSize(tp.getWidget(tp
  435. .getVisibleWidget()));
  436. } catch (Exception e) {
  437. // Ignore, most likely empty tabsheet
  438. }
  439. renderInformation.updateSize(getElement());
  440. waitingForResponse = false;
  441. rendering = false;
  442. }
  443. private void handleStyleNames(UIDL uidl) {
  444. // Add proper stylenames for all elements (easier to prevent unwanted
  445. // style inheritance)
  446. if (uidl.hasAttribute("style")) {
  447. final String style = uidl.getStringAttribute("style");
  448. if (currentStyle != style) {
  449. currentStyle = style;
  450. final String[] styles = style.split(" ");
  451. final String tabsBaseClass = TABS_CLASSNAME;
  452. String tabsClass = tabsBaseClass;
  453. final String contentBaseClass = CLASSNAME + "-content";
  454. String contentClass = contentBaseClass;
  455. final String decoBaseClass = CLASSNAME + "-deco";
  456. String decoClass = decoBaseClass;
  457. for (int i = 0; i < styles.length; i++) {
  458. tb.addStyleDependentName(styles[i]);
  459. tabsClass += " " + tabsBaseClass + "-" + styles[i];
  460. contentClass += " " + contentBaseClass + "-" + styles[i];
  461. decoClass += " " + decoBaseClass + "-" + styles[i];
  462. }
  463. DOM.setElementProperty(tabs, "className", tabsClass);
  464. DOM.setElementProperty(contentNode, "className", contentClass);
  465. DOM.setElementProperty(deco, "className", decoClass);
  466. borderW = -1;
  467. }
  468. } else {
  469. tb.setStyleName(CLASSNAME + "-tabs");
  470. DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
  471. DOM.setElementProperty(contentNode, "className", CLASSNAME
  472. + "-content");
  473. DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
  474. }
  475. if (uidl.hasAttribute("hidetabs")) {
  476. tb.setVisible(false);
  477. addStyleName(CLASSNAME + "-hidetabs");
  478. } else {
  479. tb.setVisible(true);
  480. removeStyleName(CLASSNAME + "-hidetabs");
  481. }
  482. }
  483. private void updateDynamicWidth() {
  484. // Find width consumed by tabs
  485. TableCellElement spacerCell = ((TableElement) tb.getElement().cast())
  486. .getRows().getItem(0).getCells().getItem(tb.getTabCount());
  487. int spacerWidth = spacerCell.getOffsetWidth();
  488. DivElement div = (DivElement) spacerCell.getFirstChildElement();
  489. int spacerMinWidth = spacerCell.getOffsetWidth() - div.getOffsetWidth();
  490. int tabsWidth = tb.getOffsetWidth() - spacerWidth + spacerMinWidth;
  491. // Find content width
  492. Style style = tp.getElement().getStyle();
  493. String overflow = style.getProperty("overflow");
  494. style.setProperty("overflow", "hidden");
  495. style.setPropertyPx("width", tabsWidth);
  496. Style wrapperstyle = tp.getWidget(tp.getVisibleWidget()).getElement()
  497. .getParentElement().getStyle();
  498. wrapperstyle.setPropertyPx("width", tabsWidth);
  499. // Get content width from actual widget
  500. int contentWidth = 0;
  501. if (tp.getWidgetCount() > 0) {
  502. contentWidth = tp.getWidget(tp.getVisibleWidget()).getOffsetWidth();
  503. }
  504. style.setProperty("overflow", overflow);
  505. // Set widths to max(tabs,content)
  506. if (tabsWidth < contentWidth) {
  507. tabsWidth = contentWidth;
  508. }
  509. int outerWidth = tabsWidth + getContentAreaBorderWidth();
  510. tabs.getStyle().setPropertyPx("width", outerWidth);
  511. style.setPropertyPx("width", tabsWidth);
  512. wrapperstyle.setPropertyPx("width", tabsWidth);
  513. contentNode.getStyle().setPropertyPx("width", tabsWidth);
  514. super.setWidth(outerWidth + "px");
  515. updateOpenTabSize();
  516. }
  517. @Override
  518. protected void renderTab(final UIDL tabUidl, int index, boolean selected,
  519. boolean hidden) {
  520. TabSheetCaption c = tb.getTab(index);
  521. if (c == null) {
  522. c = new TabSheetCaption();
  523. tb.addTab(c);
  524. }
  525. c.updateCaption(tabUidl);
  526. c.setHidden(hidden);
  527. if (scrolledOutOfView(index)) {
  528. // Should not set tabs visible if they are scrolled out of view
  529. hidden = true;
  530. }
  531. // Set the current visibility of the tab (in the browser)
  532. tb.setVisible(index, !hidden);
  533. /*
  534. * Force the width of the caption container so the content will not wrap
  535. * and tabs won't be too narrow in certain browsers
  536. */
  537. c.setWidth(c.getRequiredWidth() + "px");
  538. captions.put("" + index, c);
  539. UIDL tabContentUIDL = null;
  540. Paintable tabContent = null;
  541. if (tabUidl.getChildCount() > 0) {
  542. tabContentUIDL = tabUidl.getChildUIDL(0);
  543. tabContent = client.getPaintable(tabContentUIDL);
  544. }
  545. if (tabContent != null) {
  546. /* This is a tab with content information */
  547. int oldIndex = tp.getWidgetIndex((Widget) tabContent);
  548. if (oldIndex != -1 && oldIndex != index) {
  549. /*
  550. * The tab has previously been rendered in another position so
  551. * we must move the cached content to correct position
  552. */
  553. tp.insert((Widget) tabContent, index);
  554. }
  555. } else {
  556. /* A tab whose content has not yet been loaded */
  557. /*
  558. * Make sure there is a corresponding empty tab in tp. The same
  559. * operation as the moving above but for not-loaded tabs.
  560. */
  561. if (index < tp.getWidgetCount()) {
  562. Widget oldWidget = tp.getWidget(index);
  563. if (!(oldWidget instanceof PlaceHolder)) {
  564. tp.insert(new PlaceHolder(), index);
  565. }
  566. }
  567. }
  568. if (selected) {
  569. renderContent(tabContentUIDL);
  570. tb.selectTab(index);
  571. } else {
  572. if (tabContentUIDL != null) {
  573. // updating a drawn child on hidden tab
  574. if (tp.getWidgetIndex((Widget) tabContent) < 0) {
  575. tp.insert((Widget) tabContent, index);
  576. }
  577. tabContent.updateFromUIDL(tabContentUIDL, client);
  578. } else if (tp.getWidgetCount() <= index) {
  579. tp.add(new PlaceHolder());
  580. }
  581. }
  582. }
  583. public class PlaceHolder extends VLabel {
  584. public PlaceHolder() {
  585. super("");
  586. }
  587. }
  588. @Override
  589. protected void selectTab(int index, final UIDL contentUidl) {
  590. if (index != activeTabIndex) {
  591. activeTabIndex = index;
  592. tb.selectTab(activeTabIndex);
  593. }
  594. renderContent(contentUidl);
  595. }
  596. private void renderContent(final UIDL contentUIDL) {
  597. final Paintable content = client.getPaintable(contentUIDL);
  598. if (tp.getWidgetCount() > activeTabIndex) {
  599. Widget old = tp.getWidget(activeTabIndex);
  600. if (old != content) {
  601. tp.remove(activeTabIndex);
  602. if (old instanceof Paintable) {
  603. client.unregisterPaintable((Paintable) old);
  604. }
  605. tp.insert((Widget) content, activeTabIndex);
  606. }
  607. } else {
  608. tp.add((Widget) content);
  609. }
  610. tp.showWidget(activeTabIndex);
  611. VTabsheet.this.iLayout();
  612. (content).updateFromUIDL(contentUIDL, client);
  613. /*
  614. * The size of a cached, relative sized component must be updated to
  615. * report correct size to updateOpenTabSize().
  616. */
  617. if (contentUIDL.getBooleanAttribute("cached")) {
  618. client.handleComponentRelativeSize((Widget) content);
  619. }
  620. updateOpenTabSize();
  621. VTabsheet.this.removeStyleDependentName("loading");
  622. if (previousVisibleWidget != null) {
  623. DOM.setStyleAttribute(
  624. DOM.getParent(previousVisibleWidget.getElement()),
  625. "visibility", "");
  626. previousVisibleWidget = null;
  627. }
  628. }
  629. @Override
  630. public void setHeight(String height) {
  631. super.setHeight(height);
  632. this.height = height;
  633. updateContentNodeHeight();
  634. if (!rendering) {
  635. updateOpenTabSize();
  636. iLayout();
  637. // TODO Check if this is needed
  638. client.runDescendentsLayout(this);
  639. }
  640. }
  641. private void updateContentNodeHeight() {
  642. if (height != null && !"".equals(height)) {
  643. int contentHeight = getOffsetHeight();
  644. contentHeight -= DOM.getElementPropertyInt(deco, "offsetHeight");
  645. contentHeight -= tb.getOffsetHeight();
  646. if (contentHeight < 0) {
  647. contentHeight = 0;
  648. }
  649. // Set proper values for content element
  650. DOM.setStyleAttribute(contentNode, "height", contentHeight + "px");
  651. renderSpace.setHeight(contentHeight);
  652. } else {
  653. DOM.setStyleAttribute(contentNode, "height", "");
  654. renderSpace.setHeight(0);
  655. }
  656. }
  657. @Override
  658. public void setWidth(String width) {
  659. if ((this.width == null && width.equals(""))
  660. || (this.width != null && this.width.equals(width))) {
  661. return;
  662. }
  663. super.setWidth(width);
  664. if (width.equals("")) {
  665. width = null;
  666. }
  667. this.width = width;
  668. if (width == null) {
  669. renderSpace.setWidth(0);
  670. contentNode.getStyle().setProperty("width", "");
  671. } else {
  672. int contentWidth = getOffsetWidth() - getContentAreaBorderWidth();
  673. if (contentWidth < 0) {
  674. contentWidth = 0;
  675. }
  676. contentNode.getStyle().setProperty("width", contentWidth + "px");
  677. renderSpace.setWidth(contentWidth);
  678. }
  679. if (!rendering) {
  680. if (isDynamicHeight()) {
  681. Util.updateRelativeChildrenAndSendSizeUpdateEvent(client, tp,
  682. this);
  683. }
  684. updateOpenTabSize();
  685. iLayout();
  686. // TODO Check if this is needed
  687. client.runDescendentsLayout(this);
  688. }
  689. }
  690. public void iLayout() {
  691. updateTabScroller();
  692. tp.runWebkitOverflowAutoFix();
  693. }
  694. /**
  695. * Sets the size of the visible tab (component). As the tab is set to
  696. * position: absolute (to work around a firefox flickering bug) we must keep
  697. * this up-to-date by hand.
  698. */
  699. private void updateOpenTabSize() {
  700. /*
  701. * The overflow=auto element must have a height specified, otherwise it
  702. * will be just as high as the contents and no scrollbars will appear
  703. */
  704. int height = -1;
  705. int width = -1;
  706. int minWidth = 0;
  707. if (!isDynamicHeight()) {
  708. height = renderSpace.getHeight();
  709. }
  710. if (!isDynamicWidth()) {
  711. width = renderSpace.getWidth();
  712. } else {
  713. /*
  714. * If the tabbar is wider than the content we need to use the tabbar
  715. * width as minimum width so scrollbars get placed correctly (at the
  716. * right edge).
  717. */
  718. minWidth = tb.getOffsetWidth() - getContentAreaBorderWidth();
  719. }
  720. tp.fixVisibleTabSize(width, height, minWidth);
  721. }
  722. /**
  723. * Layouts the tab-scroller elements, and applies styles.
  724. */
  725. private void updateTabScroller() {
  726. if (width != null) {
  727. DOM.setStyleAttribute(tabs, "width", width);
  728. }
  729. // Make sure scrollerIndex is valid
  730. if (scrollerIndex > tb.getTabCount()) {
  731. scrollerIndex = getNextVisibleTab(-1);
  732. } else if (tb.getTabCount() > 0 && tb.getTab(scrollerIndex).isHidden()) {
  733. scrollerIndex = getNextVisibleTab(scrollerIndex);
  734. }
  735. boolean scrolled = isScrolledTabs();
  736. boolean clipped = isClippedTabs();
  737. if (tb.getTabCount() > 0 && tb.isVisible() && (scrolled || clipped)) {
  738. DOM.setStyleAttribute(scroller, "display", "");
  739. DOM.setElementProperty(scrollerPrev, "className",
  740. SCROLLER_CLASSNAME + (scrolled ? "Prev" : "Prev-disabled"));
  741. DOM.setElementProperty(scrollerNext, "className",
  742. SCROLLER_CLASSNAME + (clipped ? "Next" : "Next-disabled"));
  743. } else {
  744. DOM.setStyleAttribute(scroller, "display", "none");
  745. }
  746. if (BrowserInfo.get().isSafari()) {
  747. // fix tab height for safari, bugs sometimes if tabs contain icons
  748. String property = tabs.getStyle().getProperty("height");
  749. if (property == null || property.equals("")) {
  750. tabs.getStyle().setPropertyPx("height", tb.getOffsetHeight());
  751. }
  752. /*
  753. * another hack for webkits. tabscroller sometimes drops without
  754. * "shaking it" reproducable in
  755. * com.vaadin.tests.components.tabsheet.TabSheetIcons
  756. */
  757. final Style style = scroller.getStyle();
  758. style.setProperty("whiteSpace", "normal");
  759. DeferredCommand.addCommand(new Command() {
  760. public void execute() {
  761. style.setProperty("whiteSpace", "");
  762. }
  763. });
  764. }
  765. }
  766. private void showAllTabs() {
  767. scrollerIndex = getNextVisibleTab(-1);
  768. for (int i = 0; i < tb.getTabCount(); i++) {
  769. if (!tb.getTab(i).isHidden()) {
  770. tb.setVisible(i, true);
  771. }
  772. }
  773. }
  774. private boolean isScrolledTabs() {
  775. return scrollerIndex > getNextVisibleTab(-1);
  776. }
  777. private boolean isClippedTabs() {
  778. return (tb.getOffsetWidth() - DOM.getElementPropertyInt((Element) tb
  779. .getContainerElement().getLastChild().cast(), "offsetWidth")) > getOffsetWidth()
  780. - (isScrolledTabs() ? scroller.getOffsetWidth() : 0);
  781. }
  782. @Override
  783. protected void clearPaintables() {
  784. int i = tb.getTabCount();
  785. while (i > 0) {
  786. tb.removeTab(--i);
  787. }
  788. tp.clear();
  789. }
  790. @Override
  791. protected Iterator getPaintableIterator() {
  792. return tp.iterator();
  793. }
  794. public boolean hasChildComponent(Widget component) {
  795. if (tp.getWidgetIndex(component) < 0) {
  796. return false;
  797. } else {
  798. return true;
  799. }
  800. }
  801. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  802. tp.replaceComponent(oldComponent, newComponent);
  803. }
  804. public void updateCaption(Paintable component, UIDL uidl) {
  805. /* Tabsheet does not render its children's captions */
  806. }
  807. public boolean requestLayout(Set<Paintable> child) {
  808. if (!isDynamicHeight() && !isDynamicWidth()) {
  809. /*
  810. * If the height and width has been specified for this container the
  811. * child components cannot make the size of the layout change
  812. */
  813. // layout size change may affect its available space (scrollbars)
  814. for (Paintable paintable : child) {
  815. client.handleComponentRelativeSize((Widget) paintable);
  816. }
  817. return true;
  818. }
  819. updateOpenTabSize();
  820. if (renderInformation.updateSize(getElement())) {
  821. /*
  822. * Size has changed so we let the child components know about the
  823. * new size.
  824. */
  825. iLayout();
  826. client.runDescendentsLayout(this);
  827. return false;
  828. } else {
  829. /*
  830. * Size has not changed so we do not need to propagate the event
  831. * further
  832. */
  833. return true;
  834. }
  835. }
  836. private int borderW = -1;
  837. private int getContentAreaBorderWidth() {
  838. if (borderW < 0) {
  839. borderW = Util.measureHorizontalBorder(contentNode);
  840. }
  841. return borderW;
  842. }
  843. private final RenderSpace renderSpace = new RenderSpace(0, 0, true);
  844. public RenderSpace getAllocatedSpace(Widget child) {
  845. // All tabs have equal amount of space allocated
  846. return renderSpace;
  847. }
  848. @Override
  849. protected int getTabCount() {
  850. return tb.getWidgetCount();
  851. }
  852. @Override
  853. protected Paintable getTab(int index) {
  854. if (tp.getWidgetCount() > index) {
  855. return (Paintable) tp.getWidget(index);
  856. }
  857. return null;
  858. }
  859. @Override
  860. protected void removeTab(int index) {
  861. tb.removeTab(index);
  862. /*
  863. * This must be checked because renderTab automatically removes the
  864. * active tab content when it changes
  865. */
  866. if (tp.getWidgetCount() > index) {
  867. tp.remove(index);
  868. }
  869. }
  870. }