123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344 |
- /*
- * Copyright 2000-2013 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
- package com.vaadin.client.ui;
-
- import java.util.Iterator;
- import java.util.List;
-
- import com.google.gwt.aria.client.Id;
- import com.google.gwt.aria.client.LiveValue;
- import com.google.gwt.aria.client.Roles;
- import com.google.gwt.aria.client.SelectedValue;
- 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.Style.Visibility;
- import com.google.gwt.dom.client.TableCellElement;
- import com.google.gwt.dom.client.TableElement;
- import com.google.gwt.event.dom.client.BlurEvent;
- import com.google.gwt.event.dom.client.BlurHandler;
- import com.google.gwt.event.dom.client.ClickEvent;
- import com.google.gwt.event.dom.client.ClickHandler;
- import com.google.gwt.event.dom.client.FocusEvent;
- import com.google.gwt.event.dom.client.FocusHandler;
- import com.google.gwt.event.dom.client.HasBlurHandlers;
- import com.google.gwt.event.dom.client.HasFocusHandlers;
- import com.google.gwt.event.dom.client.HasKeyDownHandlers;
- import com.google.gwt.event.dom.client.KeyCodes;
- import com.google.gwt.event.dom.client.KeyDownEvent;
- import com.google.gwt.event.dom.client.KeyDownHandler;
- import com.google.gwt.event.shared.HandlerRegistration;
- 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.google.gwt.user.client.ui.impl.FocusImpl;
- import com.vaadin.client.ApplicationConnection;
- import com.vaadin.client.BrowserInfo;
- import com.vaadin.client.ComponentConnector;
- import com.vaadin.client.ConnectorMap;
- import com.vaadin.client.Focusable;
- import com.vaadin.client.TooltipInfo;
- import com.vaadin.client.UIDL;
- import com.vaadin.client.Util;
- import com.vaadin.client.VCaption;
- import com.vaadin.client.ui.aria.AriaHelper;
- import com.vaadin.shared.AbstractComponentState;
- import com.vaadin.shared.EventId;
- import com.vaadin.shared.ui.ComponentStateUtil;
- import com.vaadin.shared.ui.tabsheet.TabsheetBaseConstants;
- import com.vaadin.shared.ui.tabsheet.TabsheetConstants;
-
- public class VTabsheet extends VTabsheetBase implements Focusable,
- FocusHandler, BlurHandler, KeyDownHandler {
-
- 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
- *
- */
- public static class Tab extends SimplePanel implements HasFocusHandlers,
- HasBlurHandlers, HasKeyDownHandlers {
- 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_FOCUS_CLASSNAME = TD_CLASSNAME
- + "-focus";
- private static final String TD_FOCUS_FIRST_CLASSNAME = TD_FOCUS_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 static final String DIV_FOCUS_CLASSNAME = DIV_CLASSNAME
- + "-focus";
-
- 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 String id;
-
- private Tab(TabBar tabBar) {
- super(DOM.createTD());
- this.tabBar = tabBar;
- setStyleName(td, TD_CLASSNAME);
-
- Roles.getTabRole().set(getElement());
- Roles.getTabRole().setAriaSelectedState(getElement(),
- SelectedValue.FALSE);
-
- div = DOM.createDiv();
- focusImpl.setTabIndex(td, -1);
- setStyleName(div, DIV_CLASSNAME);
-
- DOM.appendChild(td, div);
-
- tabCaption = new TabCaption(this, getTabsheet()
- .getApplicationConnection());
- add(tabCaption);
-
- Roles.getTabRole().setAriaLabelledbyProperty(getElement(),
- Id.of(tabCaption.getElement()));
-
- addFocusHandler(getTabsheet());
- addBlurHandler(getTabsheet());
- addKeyDownHandler(getTabsheet());
- }
-
- public boolean isHiddenOnServer() {
- return hiddenOnServer;
- }
-
- public void setHiddenOnServer(boolean hiddenOnServer) {
- this.hiddenOnServer = hiddenOnServer;
- Roles.getTabRole().setAriaHiddenState(getElement(), 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;
- Roles.getTabRole().setAriaDisabledState(getElement(), !enabled);
-
- setStyleName(td, TD_DISABLED_CLASSNAME, !enabled);
- if (!enabled) {
- focusImpl.setTabIndex(td, -1);
- }
- }
-
- 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) {
- setStyleNames(selected, first, false);
- }
-
- public void setStyleNames(boolean selected, boolean first,
- boolean keyboardFocus) {
- 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);
- setStyleName(td, TD_FOCUS_CLASSNAME, keyboardFocus);
- setStyleName(td, TD_FOCUS_FIRST_CLASSNAME, keyboardFocus && first);
- setStyleName(div, DIV_FOCUS_CLASSNAME, keyboardFocus);
- }
-
- public void setTabulatorIndex(int tabIndex) {
- focusImpl.setTabIndex(td, tabIndex);
- }
-
- public boolean isClosable() {
- return tabCaption.isClosable();
- }
-
- 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(TabsheetConstants.TAB_STYLE_NAME);
- // Find the nth td element
- if (newStyleName != null && !newStyleName.isEmpty()) {
- if (!newStyleName.equals(styleName)) {
- // If we have a new style name
- if (styleName != null && !styleName.isEmpty()) {
- // 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;
- }
-
- String newId = tabUidl.getStringAttribute("id");
- if (newId != null && !newId.isEmpty()) {
- td.setId(newId);
- id = newId;
- } else if (id != null) {
- td.removeAttribute("id");
- id = null;
- }
- }
-
- public void recalculateCaptionWidth() {
- tabCaption.setWidth(tabCaption.getRequiredWidth() + "px");
- }
-
- @Override
- public HandlerRegistration addFocusHandler(FocusHandler handler) {
- return addDomHandler(handler, FocusEvent.getType());
- }
-
- @Override
- public HandlerRegistration addBlurHandler(BlurHandler handler) {
- return addDomHandler(handler, BlurEvent.getType());
- }
-
- @Override
- public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
- return addDomHandler(handler, KeyDownEvent.getType());
- }
-
- public void focus() {
- focusImpl.focus(td);
- }
-
- public void blur() {
- focusImpl.blur(td);
- }
-
- public boolean hasTooltip() {
- return tabCaption.getTooltipInfo() != null;
- }
-
- public TooltipInfo getTooltipInfo() {
- return tabCaption.getTooltipInfo();
- }
-
- public void setAssistiveDescription(String descriptionId) {
- Roles.getTablistRole().setAriaDescribedbyProperty(getElement(),
- Id.of(descriptionId));
- }
-
- public void removeAssistiveDescription() {
- Roles.getTablistRole().removeAriaDescribedbyProperty(getElement());
- }
- }
-
- public static class TabCaption extends VCaption {
-
- private boolean closable = false;
- private Element closeButton;
- private Tab tab;
-
- TabCaption(Tab tab, ApplicationConnection client) {
- super(client);
- this.tab = tab;
-
- AriaHelper.ensureHasId(getElement());
- }
-
- public boolean updateCaption(UIDL uidl) {
- if (uidl.hasAttribute(TabsheetBaseConstants.ATTRIBUTE_TAB_DESCRIPTION)
- || uidl.hasAttribute(TabsheetBaseConstants.ATTRIBUTE_TAB_ERROR_MESSAGE)) {
- setTooltipInfo(new TooltipInfo(
- uidl.getStringAttribute(TabsheetBaseConstants.ATTRIBUTE_TAB_DESCRIPTION),
- uidl.getStringAttribute(TabsheetBaseConstants.ATTRIBUTE_TAB_ERROR_MESSAGE)));
- } else {
- setTooltipInfo(null);
- }
-
- // TODO need to call this instead of super because the caption does
- // not have an owner
- boolean ret = updateCaptionWithoutOwner(
- uidl.getStringAttribute(TabsheetBaseConstants.ATTRIBUTE_TAB_CAPTION),
- uidl.hasAttribute(TabsheetBaseConstants.ATTRIBUTE_TAB_DISABLED),
- uidl.hasAttribute(TabsheetBaseConstants.ATTRIBUTE_TAB_DESCRIPTION),
- uidl.hasAttribute(TabsheetBaseConstants.ATTRIBUTE_TAB_ERROR_MESSAGE),
- uidl.getStringAttribute(TabsheetBaseConstants.ATTRIBUTE_TAB_ICON),
- uidl.getStringAttribute(TabsheetBaseConstants.ATTRIBUTE_TAB_ICON_ALT));
-
- 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());
- }
- }
-
- 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");
-
- Roles.getTabRole().setAriaHiddenState(closeButton, true);
- Roles.getTabRole().setAriaDisabledState(closeButton, true);
-
- getElement().appendChild(closeButton);
- } else if (!closable && closeButton != null) {
- getElement().removeChild(closeButton);
- closeButton = null;
- }
- if (closable) {
- addStyleDependentName("closable");
- } else {
- removeStyleDependentName("closable");
- }
- }
-
- public boolean isClosable() {
- return closable;
- }
-
- @Override
- public int getRequiredWidth() {
- int width = super.getRequiredWidth();
- if (closeButton != null) {
- width += Util.getRequiredWidth(closeButton);
- }
- return width;
- }
-
- public Element getCloseButton() {
- return closeButton;
- }
-
- }
-
- 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();
- Roles.getPresentationRole().set(el);
-
- 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);
- }
-
- @Override
- 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;
- }
-
- @Override
- public void onClick(ClickEvent event) {
- TabCaption caption = (TabCaption) event.getSource();
- Element targetElement = event.getNativeEvent().getEventTarget()
- .cast();
- // the tab should not be focused if the close button was clicked
- if (targetElement == caption.getCloseButton()) {
- return;
- }
-
- int index = getWidgetIndex(caption.getParent());
-
- getTabsheet().focus();
- getTabsheet().loadTabSheet(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), true);
- newSelected.setTabulatorIndex(getTabsheet().tabulatorIndex);
- Roles.getTabRole().setAriaSelectedState(newSelected.getElement(),
- SelectedValue.TRUE);
-
- if (oldSelected != null && oldSelected != newSelected) {
- oldSelected.setStyleNames(false,
- isFirstVisibleTab(getWidgetIndex(oldSelected)));
- oldSelected.setTabulatorIndex(-1);
-
- Roles.getTabRole().setAriaSelectedState(
- oldSelected.getElement(), SelectedValue.FALSE);
- }
-
- // 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 navigateTab(int fromIndex, int toIndex) {
- Tab newNavigated = getTab(toIndex);
- if (newNavigated == null) {
- throw new IllegalArgumentException(
- "Tab at provided index toIndex was not found");
- }
-
- Tab oldNavigated = getTab(fromIndex);
- newNavigated.setStyleNames(newNavigated.equals(selected),
- isFirstVisibleTab(toIndex), true);
-
- if (oldNavigated != null && fromIndex != toIndex) {
- oldNavigated.setStyleNames(oldNavigated.equals(selected),
- isFirstVisibleTab(fromIndex), false);
- }
- }
-
- 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";
-
- /** For internal use only. May be removed or replaced in the future. */
- public final Element tabs; // tabbar and 'scroller' container
- Tab focusedTab;
- /**
- * The tabindex property (position in the browser's focus cycle.) Named like
- * this to avoid confusion with activeTabIndex.
- */
- int tabulatorIndex = 0;
-
- private static final FocusImpl focusImpl = FocusImpl.getFocusImplForPanel();
-
- 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;
-
- final TabBar tb = new TabBar(this);
- /** For internal use only. May be removed or replaced in the future. */
- public final VTabsheetPanel tp = new VTabsheetPanel();
- /** For internal use only. May be removed or replaced in the future. */
- public final Element contentNode;
-
- private final Element deco;
-
- /** For internal use only. May be removed or replaced in the future. */
- public boolean waitingForResponse;
-
- private String currentStyle;
-
- /**
- * @return Whether the tab could be selected or not.
- */
- private boolean canSelectTab(final int tabIndex) {
- Tab tab = tb.getTab(tabIndex);
- if (client == null || disabled || waitingForResponse) {
- return false;
- }
- if (!tab.isEnabledOnServer() || tab.isHiddenOnServer()) {
- return false;
- }
-
- // Note that we return true when tabIndex == activeTabIndex; the active
- // tab could be selected, it's just a no-op.
- return true;
- }
-
- /**
- * Load the content of a tab of the provided index.
- *
- * @param index
- * of the tab to load
- */
- public void loadTabSheet(int tabIndex) {
- if (activeTabIndex != tabIndex && canSelectTab(tabIndex)) {
- tb.selectTab(tabIndex);
-
- // If this TabSheet already has focus, set the new selected tab
- // as focused.
- if (focusedTab != null) {
- focusedTab = tb.getTab(tabIndex);
- focusedTab.focus();
- }
-
- activeTabIndex = tabIndex;
- focusedTabIndex = tabIndex;
-
- addStyleDependentName("loading");
- // Hide the current contents so a loading indicator can be shown
- // instead
- Widget currentlyDisplayedWidget = tp.getWidget(tp
- .getVisibleWidget());
- currentlyDisplayedWidget.getElement().getParentElement().getStyle()
- .setVisibility(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() {
- ComponentConnector paintable = ConnectorMap.get(client).getConnector(
- this);
- return paintable.isUndefinedWidth();
- }
-
- boolean isDynamicHeight() {
- ComponentConnector paintable = ConnectorMap.get(client).getConnector(
- this);
- return paintable.isUndefinedHeight();
- }
-
- public VTabsheet() {
- super(CLASSNAME);
-
- addHandler(this, FocusEvent.getType());
- addHandler(this, BlurEvent.getType());
-
- // Tab scrolling
- DOM.setStyleAttribute(getElement(), "overflow", "hidden");
- tabs = DOM.createDiv();
- DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
- Roles.getTablistRole().set(tabs);
- Roles.getTablistRole().setAriaLiveProperty(tabs, LiveValue.OFF);
- scroller = DOM.createDiv();
- Roles.getTablistRole().setAriaHiddenState(scroller, true);
-
- DOM.setElementProperty(scroller, "className", SCROLLER_CLASSNAME);
- scrollerPrev = DOM.createButton();
- scrollerPrev.setTabIndex(-1);
- DOM.setElementProperty(scrollerPrev, "className", SCROLLER_CLASSNAME
- + "Prev");
- Roles.getTablistRole().setAriaHiddenState(scrollerPrev, true);
- DOM.sinkEvents(scrollerPrev, Event.ONCLICK);
- scrollerNext = DOM.createButton();
- scrollerNext.setTabIndex(-1);
- DOM.setElementProperty(scrollerNext, "className", SCROLLER_CLASSNAME
- + "Next");
- Roles.getTablistRole().setAriaHiddenState(scrollerNext, true);
- DOM.sinkEvents(scrollerNext, Event.ONCLICK);
- DOM.appendChild(getElement(), tabs);
-
- // Tabs
- tp.setStyleName(CLASSNAME + "-tabsheetpanel");
- contentNode = DOM.createDiv();
- Roles.getTabpanelRole().set(contentNode);
-
- 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) {
- if (event.getTypeInt() == Event.ONCLICK) {
- // Tab scrolling
- if (isScrolledTabs() && DOM.eventGetTarget(event) == scrollerPrev) {
- int newFirstIndex = tb.scrollLeft(scrollerIndex);
- if (newFirstIndex != -1) {
- scrollerIndex = newFirstIndex;
- updateTabScroller();
- }
- event.stopPropagation();
- return;
- } else if (isClippedTabs()
- && DOM.eventGetTarget(event) == scrollerNext) {
- int newFirstIndex = tb.scrollRight(scrollerIndex);
-
- if (newFirstIndex != -1) {
- scrollerIndex = newFirstIndex;
- updateTabScroller();
- }
- event.stopPropagation();
- return;
- }
- }
- 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;
- }
-
- /** For internal use only. May be removed or replaced in the future. */
- public void handleStyleNames(UIDL uidl, AbstractComponentState state) {
- // Add proper stylenames for all elements (easier to prevent unwanted
- // style inheritance)
- if (ComponentStateUtil.hasStyles(state)) {
- final List<String> styles = state.styles;
- if (currentStyle == null || !currentStyle.equals(styles.toString())) {
- currentStyle = styles.toString();
- 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 (String style : styles) {
- tb.addStyleDependentName(style);
- tabsClass += " " + tabsBaseClass + "-" + style;
- contentClass += " " + contentBaseClass + "-" + style;
- decoClass += " " + decoBaseClass + "-" + style;
- }
- 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");
- }
- }
-
- /** For internal use only. May be removed or replaced in the future. */
- public 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
- public 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();
-
- if (selected) {
- renderContent(tabUidl.getChildUIDL(0));
- tb.selectTab(index);
- }
- }
-
- /**
- * @deprecated as of 7.1, VTabsheet only keeps the active tab in the DOM
- * without any place holders.
- */
- @Deprecated
- public class PlaceHolder extends VLabel {
- public PlaceHolder() {
- super("");
- }
- }
-
- private void renderContent(final UIDL contentUIDL) {
- final ComponentConnector content = client.getPaintable(contentUIDL);
- Widget newWidget = content.getWidget();
-
- assert tp.getWidgetCount() <= 1;
-
- if (tp.getWidgetCount() == 0) {
- tp.add(newWidget);
- } else if (tp.getWidget(0) != newWidget) {
- tp.remove(0);
- tp.add(newWidget);
- }
-
- assert tp.getWidgetCount() <= 1;
-
- // There's never any other index than 0, but maintaining API for now
- tp.showWidget(0);
-
- VTabsheet.this.iLayout();
- updateOpenTabSize();
- VTabsheet.this.removeStyleDependentName("loading");
- }
-
- /** For internal use only. May be removed or replaced in the future. */
- public void updateContentNodeHeight() {
- if (!isDynamicHeight()) {
- 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");
- } else {
- DOM.setStyleAttribute(contentNode, "height", "");
- }
- }
-
- public void iLayout() {
- updateTabScroller();
- }
-
- /**
- * 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.
- * <p>
- * For internal use only. May be removed or replaced in the future.
- */
- public 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 = contentNode.getOffsetHeight();
- }
- if (!isDynamicWidth()) {
- width = contentNode.getOffsetWidth() - getContentAreaBorderWidth();
- } 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 (!isDynamicWidth()) {
- ComponentConnector paintable = ConnectorMap.get(client)
- .getConnector(this);
- DOM.setStyleAttribute(tabs, "width", paintable.getState().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() {
-
- @Override
- public void execute() {
- style.setProperty("whiteSpace", "");
- }
- });
- }
-
- }
-
- /** For internal use only. May be removed or replaced in the future. */
- public 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);
- }
-
- private boolean isClipped(Tab tab) {
- return tab.getAbsoluteLeft() + tab.getOffsetWidth() > getAbsoluteLeft()
- + getOffsetWidth() - scroller.getOffsetWidth();
- }
-
- @Override
- protected void clearPaintables() {
-
- int i = tb.getTabCount();
- while (i > 0) {
- tb.removeTab(--i);
- }
- tp.clear();
-
- }
-
- @Override
- public Iterator<Widget> getWidgetIterator() {
- return tp.iterator();
- }
-
- private int borderW = -1;
-
- /** For internal use only. May be removed or replaced in the future. */
- public int getContentAreaBorderWidth() {
- if (borderW < 0) {
- borderW = Util.measureHorizontalBorder(contentNode);
- }
- return borderW;
- }
-
- @Override
- public int getTabCount() {
- return tb.getTabCount();
- }
-
- @Override
- public ComponentConnector getTab(int index) {
- if (tp.getWidgetCount() > index) {
- Widget widget = tp.getWidget(index);
- return ConnectorMap.get(client).getConnector(widget);
- }
- return null;
- }
-
- @Override
- public void removeTab(int index) {
- tb.removeTab(index);
-
- // Removing content from tp is handled by the connector
- }
-
- @Override
- public void onBlur(BlurEvent event) {
- getApplicationConnection().getVTooltip().hideTooltip();
-
- if (focusedTab != null && event.getSource() instanceof Tab) {
- focusedTab.removeAssistiveDescription();
- focusedTab = null;
- if (client.hasEventListeners(this, EventId.BLUR)) {
- client.updateVariable(id, EventId.BLUR, "", true);
- }
- }
- }
-
- @Override
- public void onFocus(FocusEvent event) {
- if (focusedTab == null && event.getSource() instanceof Tab) {
- focusedTab = (Tab) event.getSource();
- if (client.hasEventListeners(this, EventId.FOCUS)) {
- client.updateVariable(id, EventId.FOCUS, "", true);
- }
-
- if (focusedTab.hasTooltip()) {
- focusedTab.setAssistiveDescription(getApplicationConnection()
- .getVTooltip().getUniqueId());
- getApplicationConnection().getVTooltip().showAssistive(
- focusedTab.getTooltipInfo());
- }
- }
- }
-
- @Override
- public void focus() {
- tb.getTab(activeTabIndex).focus();
- }
-
- public void blur() {
- tb.getTab(activeTabIndex).blur();
- }
-
- @Override
- public void onKeyDown(KeyDownEvent event) {
- if (event.getSource() instanceof Tab) {
- int keycode = event.getNativeEvent().getKeyCode();
-
- if (!event.isAnyModifierKeyDown()) {
- if (keycode == getPreviousTabKey()) {
- selectPreviousTab();
- event.stopPropagation();
- } else if (keycode == getNextTabKey()) {
- selectNextTab();
- event.stopPropagation();
- } else if (keycode == getCloseTabKey()) {
- Tab tab = tb.getTab(activeTabIndex);
- if (tab.isClosable()) {
- tab.onClose();
- }
- } else if (keycode == getSelectTabKey()) {
- loadTabSheet(focusedTabIndex);
- }
- }
- }
- }
-
- /**
- * @return The key code of the keyboard shortcut that selects the previous
- * tab in a focused tabsheet.
- */
- protected int getPreviousTabKey() {
- return KeyCodes.KEY_LEFT;
- }
-
- protected int getSelectTabKey() {
- return 32; // Space key
- }
-
- /**
- * @return The key code of the keyboard shortcut that selects the next tab
- * in a focused tabsheet.
- */
- protected int getNextTabKey() {
- return KeyCodes.KEY_RIGHT;
- }
-
- /**
- * @return The key code of the keyboard shortcut that closes the currently
- * selected tab in a focused tabsheet.
- */
- protected int getCloseTabKey() {
- return KeyCodes.KEY_DELETE;
- }
-
- private void selectPreviousTab() {
- int newTabIndex = focusedTabIndex;
- // Find the previous visible and enabled tab if any.
- do {
- newTabIndex--;
- } while (newTabIndex >= 0 && !canSelectTab(newTabIndex));
-
- if (newTabIndex >= 0) {
- tb.navigateTab(focusedTabIndex, newTabIndex);
- focusedTabIndex = newTabIndex;
-
- // If this TabSheet already has focus, set the new selected tab
- // as focused.
- if (focusedTab != null) {
- focusedTab = tb.getTab(focusedTabIndex);
- focusedTab.focus();
- }
-
- if (isScrolledTabs()) {
- // Scroll until the new active tab is visible
- int newScrollerIndex = scrollerIndex;
- while (tb.getTab(focusedTabIndex).getAbsoluteLeft() < getAbsoluteLeft()
- && newScrollerIndex != -1) {
- newScrollerIndex = tb.scrollLeft(newScrollerIndex);
- }
- scrollerIndex = newScrollerIndex;
- updateTabScroller();
- }
- }
- }
-
- private void selectNextTab() {
- int newTabIndex = focusedTabIndex;
- // Find the next visible and enabled tab if any.
- do {
- newTabIndex++;
- } while (newTabIndex < getTabCount() && !canSelectTab(newTabIndex));
-
- if (newTabIndex < getTabCount()) {
-
- tb.navigateTab(focusedTabIndex, newTabIndex);
- focusedTabIndex = newTabIndex;
-
- // If this TabSheet already has focus, set the new selected tab
- // as focused.
- if (focusedTab != null) {
- focusedTab = tb.getTab(focusedTabIndex);
- focusedTab.focus();
- }
-
- if (isClippedTabs()) {
- // Scroll until the new active tab is completely visible
- int newScrollerIndex = scrollerIndex;
- while (isClipped(tb.getTab(focusedTabIndex))
- && newScrollerIndex != -1) {
- newScrollerIndex = tb.scrollRight(newScrollerIndex);
- }
- scrollerIndex = newScrollerIndex;
- updateTabScroller();
- }
- }
- }
- }
|