12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148 |
- /*
- @VaadinApache2LicenseForJavaFiles@
- */
-
- package com.vaadin.terminal.gwt.client.ui;
-
- import java.util.Iterator;
- import java.util.Set;
-
- import com.google.gwt.core.client.Scheduler;
- import com.google.gwt.dom.client.DivElement;
- import com.google.gwt.dom.client.Style;
- import com.google.gwt.dom.client.TableCellElement;
- import com.google.gwt.dom.client.TableElement;
- import com.google.gwt.event.dom.client.ClickEvent;
- import com.google.gwt.event.dom.client.ClickHandler;
- import com.google.gwt.user.client.Command;
- import com.google.gwt.user.client.DOM;
- import com.google.gwt.user.client.Element;
- import com.google.gwt.user.client.Event;
- import com.google.gwt.user.client.ui.ComplexPanel;
- import com.google.gwt.user.client.ui.SimplePanel;
- import com.google.gwt.user.client.ui.Widget;
- import com.vaadin.terminal.gwt.client.ApplicationConnection;
- import com.vaadin.terminal.gwt.client.BrowserInfo;
- import com.vaadin.terminal.gwt.client.ComponentState;
- import com.vaadin.terminal.gwt.client.RenderInformation;
- import com.vaadin.terminal.gwt.client.RenderSpace;
- import com.vaadin.terminal.gwt.client.TooltipInfo;
- import com.vaadin.terminal.gwt.client.UIDL;
- import com.vaadin.terminal.gwt.client.Util;
- import com.vaadin.terminal.gwt.client.VCaption;
- import com.vaadin.terminal.gwt.client.VPaintableMap;
- import com.vaadin.terminal.gwt.client.VPaintableWidget;
- import com.vaadin.terminal.gwt.client.ui.label.VLabel;
-
- public class VTabsheet extends VTabsheetBase {
-
- private static class VCloseEvent {
- private Tab tab;
-
- VCloseEvent(Tab tab) {
- this.tab = tab;
- }
-
- public Tab getTab() {
- return tab;
- }
-
- }
-
- private interface VCloseHandler {
- public void onClose(VCloseEvent event);
- }
-
- /**
- * Representation of a single "tab" shown in the TabBar
- *
- */
- private static class Tab extends SimplePanel {
- private static final String TD_CLASSNAME = CLASSNAME + "-tabitemcell";
- private static final String TD_FIRST_CLASSNAME = TD_CLASSNAME
- + "-first";
- private static final String TD_SELECTED_CLASSNAME = TD_CLASSNAME
- + "-selected";
- private static final String TD_SELECTED_FIRST_CLASSNAME = TD_SELECTED_CLASSNAME
- + "-first";
- private static final String TD_DISABLED_CLASSNAME = TD_CLASSNAME
- + "-disabled";
-
- private static final String DIV_CLASSNAME = CLASSNAME + "-tabitem";
- private static final String DIV_SELECTED_CLASSNAME = DIV_CLASSNAME
- + "-selected";
-
- private TabCaption tabCaption;
- Element td = getElement();
- private VCloseHandler closeHandler;
-
- private boolean enabledOnServer = true;
- private Element div;
- private TabBar tabBar;
- private boolean hiddenOnServer = false;
-
- private String styleName;
-
- private Tab(TabBar tabBar) {
- super(DOM.createTD());
- this.tabBar = tabBar;
- setStyleName(td, TD_CLASSNAME);
-
- div = DOM.createDiv();
- setStyleName(div, DIV_CLASSNAME);
-
- DOM.appendChild(td, div);
-
- tabCaption = new TabCaption(this, getTabsheet()
- .getApplicationConnection());
- add(tabCaption);
-
- }
-
- public boolean isHiddenOnServer() {
- return hiddenOnServer;
- }
-
- public void setHiddenOnServer(boolean hiddenOnServer) {
- this.hiddenOnServer = hiddenOnServer;
- }
-
- @Override
- protected Element getContainerElement() {
- // Attach caption element to div, not td
- return div;
- }
-
- public boolean isEnabledOnServer() {
- return enabledOnServer;
- }
-
- public void setEnabledOnServer(boolean enabled) {
- enabledOnServer = enabled;
- setStyleName(td, TD_DISABLED_CLASSNAME, !enabled);
- }
-
- public void addClickHandler(ClickHandler handler) {
- tabCaption.addClickHandler(handler);
- }
-
- public void setCloseHandler(VCloseHandler closeHandler) {
- this.closeHandler = closeHandler;
- }
-
- /**
- * Toggles the style names for the Tab
- *
- * @param selected
- * true if the Tab is selected
- * @param first
- * true if the Tab is the first visible Tab
- */
- public void setStyleNames(boolean selected, boolean first) {
- setStyleName(td, TD_FIRST_CLASSNAME, first);
- setStyleName(td, TD_SELECTED_CLASSNAME, selected);
- setStyleName(td, TD_SELECTED_FIRST_CLASSNAME, selected && first);
- setStyleName(div, DIV_SELECTED_CLASSNAME, selected);
- }
-
- public void onClose() {
- closeHandler.onClose(new VCloseEvent(this));
- }
-
- public VTabsheet getTabsheet() {
- return tabBar.getTabsheet();
- }
-
- public void updateFromUIDL(UIDL tabUidl) {
- tabCaption.updateCaption(tabUidl);
-
- // Apply the styleName set for the tab
- String newStyleName = tabUidl.getStringAttribute(TAB_STYLE_NAME);
- // Find the nth td element
- if (newStyleName != null && newStyleName.length() != 0) {
- if (!newStyleName.equals(styleName)) {
- // If we have a new style name
- if (styleName != null && styleName.length() != 0) {
- // Remove old style name if present
- td.removeClassName(TD_CLASSNAME + "-" + styleName);
- }
- // Set new style name
- td.addClassName(TD_CLASSNAME + "-" + newStyleName);
- styleName = newStyleName;
- }
- } else if (styleName != null) {
- // Remove the set stylename if no stylename is present in the
- // uidl
- td.removeClassName(TD_CLASSNAME + "-" + styleName);
- styleName = null;
- }
- }
-
- public void recalculateCaptionWidth() {
- tabCaption.setWidth(tabCaption.getRequiredWidth() + "px");
- }
-
- }
-
- private static class TabCaption extends VCaption {
-
- private boolean closable = false;
- private Element closeButton;
- private Tab tab;
- private ApplicationConnection client;
-
- TabCaption(Tab tab, ApplicationConnection client) {
- super(client);
- this.client = client;
- this.tab = tab;
- }
-
- @Override
- public boolean updateCaption(UIDL uidl) {
- if (uidl.hasAttribute(VTabsheetBasePaintable.ATTRIBUTE_TAB_DESCRIPTION)
- || uidl.hasAttribute(VAbstractPaintableWidget.ATTRIBUTE_ERROR)) {
- TooltipInfo tooltipInfo = new TooltipInfo();
- tooltipInfo
- .setTitle(uidl
- .getStringAttribute(VTabsheetBasePaintable.ATTRIBUTE_TAB_DESCRIPTION));
- if (uidl.hasAttribute(VAbstractPaintableWidget.ATTRIBUTE_ERROR)) {
- tooltipInfo.setErrorUidl(uidl.getErrors());
- }
- client.registerWidgetTooltip(getTabsheet(), getElement(),
- tooltipInfo);
- } else {
- client.registerWidgetTooltip(getTabsheet(), getElement(), null);
- }
-
- // TODO need to call this instead of super because the caption does
- // not have an owner
- boolean ret = updateCaptionWithoutOwner(
- uidl,
- uidl.getStringAttribute(VTabsheetBasePaintable.ATTRIBUTE_TAB_CAPTION),
- uidl.hasAttribute(VTabsheetBasePaintable.ATTRIBUTE_TAB_DISABLED),
- uidl.hasAttribute(VTabsheetBasePaintable.ATTRIBUTE_TAB_DESCRIPTION));
-
- setClosable(uidl.hasAttribute("closable"));
-
- return ret;
- }
-
- private VTabsheet getTabsheet() {
- return tab.getTabsheet();
- }
-
- @Override
- public void onBrowserEvent(Event event) {
- if (closable && event.getTypeInt() == Event.ONCLICK
- && event.getEventTarget().cast() == closeButton) {
- tab.onClose();
- event.stopPropagation();
- event.preventDefault();
- }
-
- super.onBrowserEvent(event);
-
- if (event.getTypeInt() == Event.ONLOAD) {
- getTabsheet().tabSizeMightHaveChanged(getTab());
- }
- client.handleWidgetTooltipEvent(event, getTabsheet(), getElement());
- }
-
- public Tab getTab() {
- return tab;
- }
-
- public void setClosable(boolean closable) {
- this.closable = closable;
- if (closable && closeButton == null) {
- closeButton = DOM.createSpan();
- closeButton.setInnerHTML("×");
- closeButton
- .setClassName(VTabsheet.CLASSNAME + "-caption-close");
- getElement().insertBefore(closeButton,
- getElement().getLastChild());
- } else if (!closable && closeButton != null) {
- getElement().removeChild(closeButton);
- closeButton = null;
- }
- if (closable) {
- addStyleDependentName("closable");
- } else {
- removeStyleDependentName("closable");
- }
- }
-
- @Override
- public int getRequiredWidth() {
- int width = super.getRequiredWidth();
- if (closeButton != null) {
- width += Util.getRequiredWidth(closeButton);
- }
- return width;
- }
-
- }
-
- static class TabBar extends ComplexPanel implements ClickHandler,
- VCloseHandler {
-
- private final Element tr = DOM.createTR();
-
- private final Element spacerTd = DOM.createTD();
-
- private Tab selected;
-
- private VTabsheet tabsheet;
-
- TabBar(VTabsheet tabsheet) {
- this.tabsheet = tabsheet;
-
- Element el = DOM.createTable();
- Element tbody = DOM.createTBody();
- DOM.appendChild(el, tbody);
- DOM.appendChild(tbody, tr);
- setStyleName(spacerTd, CLASSNAME + "-spacertd");
- DOM.appendChild(tr, spacerTd);
- DOM.appendChild(spacerTd, DOM.createDiv());
- setElement(el);
- }
-
- public void onClose(VCloseEvent event) {
- Tab tab = event.getTab();
- if (!tab.isEnabledOnServer()) {
- return;
- }
- int tabIndex = getWidgetIndex(tab);
- getTabsheet().sendTabClosedEvent(tabIndex);
- }
-
- protected Element getContainerElement() {
- return tr;
- }
-
- public int getTabCount() {
- return getWidgetCount();
- }
-
- public Tab addTab() {
- Tab t = new Tab(this);
- int tabIndex = getTabCount();
-
- // Logical attach
- insert(t, tr, tabIndex, true);
-
- if (tabIndex == 0) {
- // Set the "first" style
- t.setStyleNames(false, true);
- }
-
- t.addClickHandler(this);
- t.setCloseHandler(this);
-
- return t;
- }
-
- public void onClick(ClickEvent event) {
- Widget caption = (Widget) event.getSource();
- int index = getWidgetIndex(caption.getParent());
- getTabsheet().onTabSelected(index);
- }
-
- public VTabsheet getTabsheet() {
- return tabsheet;
- }
-
- public Tab getTab(int index) {
- if (index < 0 || index >= getTabCount()) {
- return null;
- }
- return (Tab) super.getWidget(index);
- }
-
- public void selectTab(int index) {
- final Tab newSelected = getTab(index);
- final Tab oldSelected = selected;
-
- newSelected.setStyleNames(true, isFirstVisibleTab(index));
-
- if (oldSelected != null && oldSelected != newSelected) {
- oldSelected.setStyleNames(false,
- isFirstVisibleTab(getWidgetIndex(oldSelected)));
- }
-
- // Update the field holding the currently selected tab
- selected = newSelected;
-
- // The selected tab might need more (or less) space
- newSelected.recalculateCaptionWidth();
- getTab(tabsheet.activeTabIndex).recalculateCaptionWidth();
- }
-
- public void removeTab(int i) {
- Tab tab = getTab(i);
- if (tab == null) {
- return;
- }
-
- remove(tab);
-
- /*
- * If this widget was selected we need to unmark it as the last
- * selected
- */
- if (tab == selected) {
- selected = null;
- }
-
- // FIXME: Shouldn't something be selected instead?
- }
-
- private boolean isFirstVisibleTab(int index) {
- return getFirstVisibleTab() == index;
- }
-
- /**
- * Returns the index of the first visible tab
- *
- * @return
- */
- private int getFirstVisibleTab() {
- return getNextVisibleTab(-1);
- }
-
- /**
- * Find the next visible tab. Returns -1 if none is found.
- *
- * @param i
- * @return
- */
- private int getNextVisibleTab(int i) {
- int tabs = getTabCount();
- do {
- i++;
- } while (i < tabs && getTab(i).isHiddenOnServer());
-
- if (i == tabs) {
- return -1;
- } else {
- return i;
- }
- }
-
- /**
- * Find the previous visible tab. Returns -1 if none is found.
- *
- * @param i
- * @return
- */
- private int getPreviousVisibleTab(int i) {
- do {
- i--;
- } while (i >= 0 && getTab(i).isHiddenOnServer());
-
- return i;
-
- }
-
- public int scrollLeft(int currentFirstVisible) {
- int prevVisible = getPreviousVisibleTab(currentFirstVisible);
- if (prevVisible == -1) {
- return -1;
- }
-
- Tab newFirst = getTab(prevVisible);
- newFirst.setVisible(true);
- newFirst.recalculateCaptionWidth();
-
- return prevVisible;
- }
-
- public int scrollRight(int currentFirstVisible) {
- int nextVisible = getNextVisibleTab(currentFirstVisible);
- if (nextVisible == -1) {
- return -1;
- }
- Tab currentFirst = getTab(currentFirstVisible);
- currentFirst.setVisible(false);
- currentFirst.recalculateCaptionWidth();
- return nextVisible;
- }
-
- }
-
- public static final String CLASSNAME = "v-tabsheet";
-
- public static final String TABS_CLASSNAME = "v-tabsheet-tabcontainer";
- public static final String SCROLLER_CLASSNAME = "v-tabsheet-scroller";
-
- // Can't use "style" as it's already in use
- public static final String TAB_STYLE_NAME = "tabstyle";
-
- final Element tabs; // tabbar and 'scroller' container
- private final Element scroller; // tab-scroller element
- private final Element scrollerNext; // tab-scroller next button element
- private final Element scrollerPrev; // tab-scroller prev button element
-
- /**
- * The index of the first visible tab (when scrolled)
- */
- private int scrollerIndex = 0;
-
- private final TabBar tb = new TabBar(this);
- final VTabsheetPanel tp = new VTabsheetPanel();
- private final Element contentNode, deco;
-
- private String height;
- private String width;
-
- boolean waitingForResponse;
-
- final RenderInformation renderInformation = new RenderInformation();
-
- /**
- * Previous visible widget is set invisible with CSS (not display: none, but
- * visibility: hidden), to avoid flickering during render process. Normal
- * visibility must be returned later when new widget is rendered.
- */
- private Widget previousVisibleWidget;
-
- boolean rendering = false;
-
- private String currentStyle;
-
- private void onTabSelected(final int tabIndex) {
- if (disabled || waitingForResponse) {
- return;
- }
- final Object tabKey = tabKeys.get(tabIndex);
- if (disabledTabKeys.contains(tabKey)) {
- return;
- }
- if (client != null && activeTabIndex != tabIndex) {
- tb.selectTab(tabIndex);
- addStyleDependentName("loading");
- // run updating variables in deferred command to bypass some FF
- // optimization issues
- Scheduler.get().scheduleDeferred(new Command() {
- public void execute() {
- previousVisibleWidget = tp.getWidget(tp.getVisibleWidget());
- DOM.setStyleAttribute(
- DOM.getParent(previousVisibleWidget.getElement()),
- "visibility", "hidden");
- client.updateVariable(id, "selected", tabKeys.get(tabIndex)
- .toString(), true);
- }
- });
- waitingForResponse = true;
- }
- }
-
- public ApplicationConnection getApplicationConnection() {
- return client;
- }
-
- public void tabSizeMightHaveChanged(Tab tab) {
- // icon onloads may change total width of tabsheet
- if (isDynamicWidth()) {
- updateDynamicWidth();
- }
- updateTabScroller();
-
- }
-
- void sendTabClosedEvent(int tabIndex) {
- client.updateVariable(id, "close", tabKeys.get(tabIndex), true);
- }
-
- boolean isDynamicWidth() {
- return width == null || width.equals("");
- }
-
- boolean isDynamicHeight() {
- return height == null || height.equals("");
- }
-
- public VTabsheet() {
- super(CLASSNAME);
-
- // Tab scrolling
- DOM.setStyleAttribute(getElement(), "overflow", "hidden");
- tabs = DOM.createDiv();
- DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
- scroller = DOM.createDiv();
-
- DOM.setElementProperty(scroller, "className", SCROLLER_CLASSNAME);
- scrollerPrev = DOM.createButton();
- DOM.setElementProperty(scrollerPrev, "className", SCROLLER_CLASSNAME
- + "Prev");
- DOM.sinkEvents(scrollerPrev, Event.ONCLICK);
- scrollerNext = DOM.createButton();
- DOM.setElementProperty(scrollerNext, "className", SCROLLER_CLASSNAME
- + "Next");
- DOM.sinkEvents(scrollerNext, Event.ONCLICK);
- DOM.appendChild(getElement(), tabs);
-
- // Tabs
- tp.setStyleName(CLASSNAME + "-tabsheetpanel");
- contentNode = DOM.createDiv();
-
- deco = DOM.createDiv();
-
- addStyleDependentName("loading"); // Indicate initial progress
- tb.setStyleName(CLASSNAME + "-tabs");
- DOM.setElementProperty(contentNode, "className", CLASSNAME + "-content");
- DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
-
- add(tb, tabs);
- DOM.appendChild(scroller, scrollerPrev);
- DOM.appendChild(scroller, scrollerNext);
-
- DOM.appendChild(getElement(), contentNode);
- add(tp, contentNode);
- DOM.appendChild(getElement(), deco);
-
- DOM.appendChild(tabs, scroller);
-
- // TODO Use for Safari only. Fix annoying 1px first cell in TabBar.
- // DOM.setStyleAttribute(DOM.getFirstChild(DOM.getFirstChild(DOM
- // .getFirstChild(tb.getElement()))), "display", "none");
-
- }
-
- @Override
- public void onBrowserEvent(Event event) {
-
- // Tab scrolling
- if (isScrolledTabs() && DOM.eventGetTarget(event) == scrollerPrev) {
- int newFirstIndex = tb.scrollLeft(scrollerIndex);
- if (newFirstIndex != -1) {
- scrollerIndex = newFirstIndex;
- updateTabScroller();
- }
- } else if (isClippedTabs() && DOM.eventGetTarget(event) == scrollerNext) {
- int newFirstIndex = tb.scrollRight(scrollerIndex);
-
- if (newFirstIndex != -1) {
- scrollerIndex = newFirstIndex;
- updateTabScroller();
- }
- } else {
- super.onBrowserEvent(event);
- }
- }
-
- /**
- * Checks if the tab with the selected index has been scrolled out of the
- * view (on the left side).
- *
- * @param index
- * @return
- */
- private boolean scrolledOutOfView(int index) {
- return scrollerIndex > index;
- }
-
- void handleStyleNames(UIDL uidl, ComponentState state) {
- // Add proper stylenames for all elements (easier to prevent unwanted
- // style inheritance)
- if (state.hasStyles()) {
- final String style = state.getStyle();
- if (currentStyle != style) {
- currentStyle = style;
- final String[] styles = style.split(" ");
- final String tabsBaseClass = TABS_CLASSNAME;
- String tabsClass = tabsBaseClass;
- final String contentBaseClass = CLASSNAME + "-content";
- String contentClass = contentBaseClass;
- final String decoBaseClass = CLASSNAME + "-deco";
- String decoClass = decoBaseClass;
- for (int i = 0; i < styles.length; i++) {
- tb.addStyleDependentName(styles[i]);
- tabsClass += " " + tabsBaseClass + "-" + styles[i];
- contentClass += " " + contentBaseClass + "-" + styles[i];
- decoClass += " " + decoBaseClass + "-" + styles[i];
- }
- DOM.setElementProperty(tabs, "className", tabsClass);
- DOM.setElementProperty(contentNode, "className", contentClass);
- DOM.setElementProperty(deco, "className", decoClass);
- borderW = -1;
- }
- } else {
- tb.setStyleName(CLASSNAME + "-tabs");
- DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
- DOM.setElementProperty(contentNode, "className", CLASSNAME
- + "-content");
- DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
- }
-
- if (uidl.hasAttribute("hidetabs")) {
- tb.setVisible(false);
- addStyleName(CLASSNAME + "-hidetabs");
- } else {
- tb.setVisible(true);
- removeStyleName(CLASSNAME + "-hidetabs");
- }
- }
-
- void updateDynamicWidth() {
- // Find width consumed by tabs
- TableCellElement spacerCell = ((TableElement) tb.getElement().cast())
- .getRows().getItem(0).getCells().getItem(tb.getTabCount());
-
- int spacerWidth = spacerCell.getOffsetWidth();
- DivElement div = (DivElement) spacerCell.getFirstChildElement();
-
- int spacerMinWidth = spacerCell.getOffsetWidth() - div.getOffsetWidth();
-
- int tabsWidth = tb.getOffsetWidth() - spacerWidth + spacerMinWidth;
-
- // Find content width
- Style style = tp.getElement().getStyle();
- String overflow = style.getProperty("overflow");
- style.setProperty("overflow", "hidden");
- style.setPropertyPx("width", tabsWidth);
-
- boolean hasTabs = tp.getWidgetCount() > 0;
-
- Style wrapperstyle = null;
- if (hasTabs) {
- wrapperstyle = tp.getWidget(tp.getVisibleWidget()).getElement()
- .getParentElement().getStyle();
- wrapperstyle.setPropertyPx("width", tabsWidth);
- }
- // Get content width from actual widget
-
- int contentWidth = 0;
- if (hasTabs) {
- contentWidth = tp.getWidget(tp.getVisibleWidget()).getOffsetWidth();
- }
- style.setProperty("overflow", overflow);
-
- // Set widths to max(tabs,content)
- if (tabsWidth < contentWidth) {
- tabsWidth = contentWidth;
- }
-
- int outerWidth = tabsWidth + getContentAreaBorderWidth();
-
- tabs.getStyle().setPropertyPx("width", outerWidth);
- style.setPropertyPx("width", tabsWidth);
- if (hasTabs) {
- wrapperstyle.setPropertyPx("width", tabsWidth);
- }
-
- contentNode.getStyle().setPropertyPx("width", tabsWidth);
- super.setWidth(outerWidth + "px");
- updateOpenTabSize();
- }
-
- @Override
- protected void renderTab(final UIDL tabUidl, int index, boolean selected,
- boolean hidden) {
- Tab tab = tb.getTab(index);
- if (tab == null) {
- tab = tb.addTab();
- }
- tab.updateFromUIDL(tabUidl);
- tab.setEnabledOnServer((!disabledTabKeys.contains(tabKeys.get(index))));
- tab.setHiddenOnServer(hidden);
-
- if (scrolledOutOfView(index)) {
- // Should not set tabs visible if they are scrolled out of view
- hidden = true;
- }
- // Set the current visibility of the tab (in the browser)
- tab.setVisible(!hidden);
-
- /*
- * Force the width of the caption container so the content will not wrap
- * and tabs won't be too narrow in certain browsers
- */
- tab.recalculateCaptionWidth();
-
- UIDL tabContentUIDL = null;
- VPaintableWidget tabContentPaintable = null;
- Widget tabContentWidget = null;
- if (tabUidl.getChildCount() > 0) {
- tabContentUIDL = tabUidl.getChildUIDL(0);
- tabContentPaintable = client.getPaintable(tabContentUIDL);
- tabContentWidget = tabContentPaintable.getWidgetForPaintable();
- }
-
- if (tabContentPaintable != null) {
- /* This is a tab with content information */
-
- int oldIndex = tp.getWidgetIndex(tabContentWidget);
- if (oldIndex != -1 && oldIndex != index) {
- /*
- * The tab has previously been rendered in another position so
- * we must move the cached content to correct position
- */
- tp.insert(tabContentWidget, index);
- }
- } else {
- /* A tab whose content has not yet been loaded */
-
- /*
- * Make sure there is a corresponding empty tab in tp. The same
- * operation as the moving above but for not-loaded tabs.
- */
- if (index < tp.getWidgetCount()) {
- Widget oldWidget = tp.getWidget(index);
- if (!(oldWidget instanceof PlaceHolder)) {
- tp.insert(new PlaceHolder(), index);
- }
- }
-
- }
-
- if (selected) {
- renderContent(tabContentUIDL);
- tb.selectTab(index);
- } else {
- if (tabContentUIDL != null) {
- // updating a drawn child on hidden tab
- if (tp.getWidgetIndex(tabContentWidget) < 0) {
- tp.insert(tabContentWidget, index);
- }
- tabContentPaintable.updateFromUIDL(tabContentUIDL, client);
- } else if (tp.getWidgetCount() <= index) {
- tp.add(new PlaceHolder());
- }
- }
- }
-
- public class PlaceHolder extends VLabel {
- public PlaceHolder() {
- super("");
- }
- }
-
- @Override
- protected void selectTab(int index, final UIDL contentUidl) {
- if (index != activeTabIndex) {
- activeTabIndex = index;
- tb.selectTab(activeTabIndex);
- }
- renderContent(contentUidl);
- }
-
- private void renderContent(final UIDL contentUIDL) {
- final VPaintableWidget content = client.getPaintable(contentUIDL);
- if (tp.getWidgetCount() > activeTabIndex) {
- Widget old = tp.getWidget(activeTabIndex);
- if (old != content) {
- tp.remove(activeTabIndex);
- VPaintableMap paintableMap = VPaintableMap.get(client);
- if (paintableMap.isPaintable(old)) {
- paintableMap.unregisterPaintable(paintableMap
- .getPaintable(old));
- }
- tp.insert(content.getWidgetForPaintable(), activeTabIndex);
- }
- } else {
- tp.add(content.getWidgetForPaintable());
- }
-
- tp.showWidget(activeTabIndex);
-
- VTabsheet.this.iLayout();
- content.updateFromUIDL(contentUIDL, client);
- /*
- * The size of a cached, relative sized component must be updated to
- * report correct size to updateOpenTabSize().
- */
- if (contentUIDL.getBooleanAttribute("cached")) {
- client.handleComponentRelativeSize(content.getWidgetForPaintable());
- }
- updateOpenTabSize();
- VTabsheet.this.removeStyleDependentName("loading");
- if (previousVisibleWidget != null) {
- DOM.setStyleAttribute(
- DOM.getParent(previousVisibleWidget.getElement()),
- "visibility", "");
- previousVisibleWidget = null;
- }
- }
-
- @Override
- public void setHeight(String height) {
- super.setHeight(height);
- this.height = height;
- updateContentNodeHeight();
-
- if (!rendering) {
- updateOpenTabSize();
- iLayout();
- // TODO Check if this is needed
- client.runDescendentsLayout(this);
- }
- }
-
- void updateContentNodeHeight() {
- if (height != null && !"".equals(height)) {
- int contentHeight = getOffsetHeight();
- contentHeight -= DOM.getElementPropertyInt(deco, "offsetHeight");
- contentHeight -= tb.getOffsetHeight();
- if (contentHeight < 0) {
- contentHeight = 0;
- }
-
- // Set proper values for content element
- DOM.setStyleAttribute(contentNode, "height", contentHeight + "px");
- renderSpace.setHeight(contentHeight);
- } else {
- DOM.setStyleAttribute(contentNode, "height", "");
- renderSpace.setHeight(0);
- }
- }
-
- @Override
- public void setWidth(String width) {
- if ((this.width == null && width.equals(""))
- || (this.width != null && this.width.equals(width))) {
- return;
- }
-
- super.setWidth(width);
- if (width.equals("")) {
- width = null;
- }
- this.width = width;
- if (width == null) {
- renderSpace.setWidth(0);
- contentNode.getStyle().setProperty("width", "");
- } else {
- int contentWidth = getOffsetWidth() - getContentAreaBorderWidth();
- if (contentWidth < 0) {
- contentWidth = 0;
- }
- contentNode.getStyle().setProperty("width", contentWidth + "px");
- renderSpace.setWidth(contentWidth);
- }
-
- if (!rendering) {
- if (isDynamicHeight()) {
- Util.updateRelativeChildrenAndSendSizeUpdateEvent(client, tp,
- this);
- }
-
- updateOpenTabSize();
- iLayout();
- // TODO Check if this is needed
- client.runDescendentsLayout(this);
-
- }
-
- }
-
- public void iLayout() {
- updateTabScroller();
- tp.runWebkitOverflowAutoFix();
- }
-
- /**
- * Sets the size of the visible tab (component). As the tab is set to
- * position: absolute (to work around a firefox flickering bug) we must keep
- * this up-to-date by hand.
- */
- void updateOpenTabSize() {
- /*
- * The overflow=auto element must have a height specified, otherwise it
- * will be just as high as the contents and no scrollbars will appear
- */
- int height = -1;
- int width = -1;
- int minWidth = 0;
-
- if (!isDynamicHeight()) {
- height = renderSpace.getHeight();
- }
- if (!isDynamicWidth()) {
- width = renderSpace.getWidth();
- } else {
- /*
- * If the tabbar is wider than the content we need to use the tabbar
- * width as minimum width so scrollbars get placed correctly (at the
- * right edge).
- */
- minWidth = tb.getOffsetWidth() - getContentAreaBorderWidth();
- }
- tp.fixVisibleTabSize(width, height, minWidth);
-
- }
-
- /**
- * Layouts the tab-scroller elements, and applies styles.
- */
- private void updateTabScroller() {
- if (width != null) {
- DOM.setStyleAttribute(tabs, "width", width);
- }
-
- // Make sure scrollerIndex is valid
- if (scrollerIndex < 0 || scrollerIndex > tb.getTabCount()) {
- scrollerIndex = tb.getFirstVisibleTab();
- } else if (tb.getTabCount() > 0
- && tb.getTab(scrollerIndex).isHiddenOnServer()) {
- scrollerIndex = tb.getNextVisibleTab(scrollerIndex);
- }
-
- boolean scrolled = isScrolledTabs();
- boolean clipped = isClippedTabs();
- if (tb.getTabCount() > 0 && tb.isVisible() && (scrolled || clipped)) {
- DOM.setStyleAttribute(scroller, "display", "");
- DOM.setElementProperty(scrollerPrev, "className",
- SCROLLER_CLASSNAME + (scrolled ? "Prev" : "Prev-disabled"));
- DOM.setElementProperty(scrollerNext, "className",
- SCROLLER_CLASSNAME + (clipped ? "Next" : "Next-disabled"));
- } else {
- DOM.setStyleAttribute(scroller, "display", "none");
- }
-
- if (BrowserInfo.get().isSafari()) {
- // fix tab height for safari, bugs sometimes if tabs contain icons
- String property = tabs.getStyle().getProperty("height");
- if (property == null || property.equals("")) {
- tabs.getStyle().setPropertyPx("height", tb.getOffsetHeight());
- }
- /*
- * another hack for webkits. tabscroller sometimes drops without
- * "shaking it" reproducable in
- * com.vaadin.tests.components.tabsheet.TabSheetIcons
- */
- final Style style = scroller.getStyle();
- style.setProperty("whiteSpace", "normal");
- Scheduler.get().scheduleDeferred(new Command() {
- public void execute() {
- style.setProperty("whiteSpace", "");
- }
- });
- }
-
- }
-
- void showAllTabs() {
- scrollerIndex = tb.getFirstVisibleTab();
- for (int i = 0; i < tb.getTabCount(); i++) {
- Tab t = tb.getTab(i);
- if (!t.isHiddenOnServer()) {
- t.setVisible(true);
- }
- }
- }
-
- private boolean isScrolledTabs() {
- return scrollerIndex > tb.getFirstVisibleTab();
- }
-
- private boolean isClippedTabs() {
- return (tb.getOffsetWidth() - DOM.getElementPropertyInt((Element) tb
- .getContainerElement().getLastChild().cast(), "offsetWidth")) > getOffsetWidth()
- - (isScrolledTabs() ? scroller.getOffsetWidth() : 0);
- }
-
- @Override
- protected void clearPaintables() {
-
- int i = tb.getTabCount();
- while (i > 0) {
- tb.removeTab(--i);
- }
- tp.clear();
-
- }
-
- @Override
- protected Iterator<Widget> getWidgetIterator() {
- return tp.iterator();
- }
-
- public boolean hasChildComponent(Widget component) {
- if (tp.getWidgetIndex(component) < 0) {
- return false;
- } else {
- return true;
- }
- }
-
- public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
- tp.replaceComponent(oldComponent, newComponent);
- }
-
- public boolean requestLayout(Set<Widget> children) {
- if (!isDynamicHeight() && !isDynamicWidth()) {
- /*
- * If the height and width has been specified for this container the
- * child components cannot make the size of the layout change
- */
- // layout size change may affect its available space (scrollbars)
- for (Widget widget : children) {
- client.handleComponentRelativeSize(widget);
- }
- return true;
- }
-
- updateOpenTabSize();
-
- if (renderInformation.updateSize(getElement())) {
- /*
- * Size has changed so we let the child components know about the
- * new size.
- */
- iLayout();
- client.runDescendentsLayout(this);
-
- return false;
- } else {
- /*
- * Size has not changed so we do not need to propagate the event
- * further
- */
- return true;
- }
-
- }
-
- private int borderW = -1;
-
- private int getContentAreaBorderWidth() {
- if (borderW < 0) {
- borderW = Util.measureHorizontalBorder(contentNode);
- }
- return borderW;
- }
-
- private final RenderSpace renderSpace = new RenderSpace(0, 0, true);
-
- public RenderSpace getAllocatedSpace(Widget child) {
- // All tabs have equal amount of space allocated
- return renderSpace;
- }
-
- @Override
- protected int getTabCount() {
- return tb.getTabCount();
- }
-
- @Override
- protected VPaintableWidget getTab(int index) {
- if (tp.getWidgetCount() > index) {
- Widget widget = tp.getWidget(index);
- return VPaintableMap.get(client).getPaintable(widget);
- }
- return null;
- }
-
- @Override
- protected void removeTab(int index) {
- tb.removeTab(index);
- /*
- * This must be checked because renderTab automatically removes the
- * active tab content when it changes
- */
- if (tp.getWidgetCount() > index) {
- tp.remove(index);
- }
- }
- }
|