123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019 |
- /*
- * Copyright 2000-2016 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.Element;
- import com.google.gwt.dom.client.Style;
- import com.google.gwt.dom.client.Style.Display;
- import com.google.gwt.dom.client.Style.Overflow;
- import com.google.gwt.dom.client.Style.Unit;
- 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.HasMouseDownHandlers;
- 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.dom.client.MouseDownEvent;
- import com.google.gwt.event.dom.client.MouseDownHandler;
- import com.google.gwt.event.shared.HandlerRegistration;
- import com.google.gwt.regexp.shared.MatchResult;
- import com.google.gwt.regexp.shared.RegExp;
- import com.google.gwt.user.client.Command;
- import com.google.gwt.user.client.DOM;
- 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.ComputedStyle;
- import com.vaadin.client.Focusable;
- import com.vaadin.client.TooltipInfo;
- import com.vaadin.client.VCaption;
- import com.vaadin.client.VTooltip;
- import com.vaadin.client.WidgetUtil;
- import com.vaadin.client.ui.aria.AriaHelper;
- import com.vaadin.shared.AbstractComponentState;
- import com.vaadin.shared.ComponentConstants;
- import com.vaadin.shared.EventId;
- import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc;
- import com.vaadin.shared.ui.ComponentStateUtil;
- import com.vaadin.shared.ui.tabsheet.TabState;
- import com.vaadin.shared.ui.tabsheet.TabsheetServerRpc;
- import com.vaadin.shared.ui.tabsheet.TabsheetState;
-
- public class VTabsheet extends VTabsheetBase
- implements Focusable, SubPartAware {
-
- private static final String PREV_SCROLLER_DISABLED_CLASSNAME = "Prev-disabled";
-
- 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, HasMouseDownHandlers, 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();
- setTabulatorIndex(-1);
- setStyleName(div, DIV_CLASSNAME);
-
- DOM.appendChild(td, div);
-
- tabCaption = new TabCaption(this);
- add(tabCaption);
-
- Roles.getTabRole().setAriaLabelledbyProperty(getElement(),
- Id.of(tabCaption.getElement()));
- }
-
- public boolean isHiddenOnServer() {
- return hiddenOnServer;
- }
-
- public void setHiddenOnServer(boolean hiddenOnServer) {
- this.hiddenOnServer = hiddenOnServer;
- Roles.getTabRole().setAriaHiddenState(getElement(), hiddenOnServer);
- }
-
- @Override
- protected com.google.gwt.user.client.Element getContainerElement() {
- // Attach caption element to div, not td
- return DOM.asOld(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) {
- getElement().setTabIndex(tabIndex);
- }
-
- public boolean isClosable() {
- return tabCaption.isClosable();
- }
-
- public void onClose() {
- closeHandler.onClose(new VCloseEvent(this));
- }
-
- public VTabsheet getTabsheet() {
- return tabBar.getTabsheet();
- }
-
- private void updateFromState(TabState tabState) {
- tabCaption.setCaptionAsHtml(getTabsheet().isTabCaptionsAsHtml());
- tabCaption.update(tabState);
- // Apply the styleName set for the tab
- String newStyleName = tabState.styleName;
- // 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 = tabState.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 addMouseDownHandler(
- MouseDownHandler handler) {
- return addDomHandler(handler, MouseDownEvent.getType());
- }
-
- @Override
- public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
- return addDomHandler(handler, KeyDownEvent.getType());
- }
-
- public void focus() {
- getTabsheet().scrollIntoView(this);
- 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) {
- super(tab.getTabsheet().connector.getConnection());
- this.tab = tab;
-
- AriaHelper.ensureHasId(getElement());
- }
-
- private boolean update(TabState tabState) {
- if (tabState.description != null
- || tabState.componentError != null) {
- setTooltipInfo(new TooltipInfo(tabState.description,
- tabState.descriptionContentMode,
- tabState.componentError, this,
- tabState.componentErrorLevel));
- } else {
- setTooltipInfo(null);
- }
-
- // TODO need to call this instead of super because the caption does
- // not have an owner
- String captionString = tabState.caption.isEmpty() ? null
- : tabState.caption;
- boolean ret = updateCaptionWithoutOwner(captionString,
- !tabState.enabled, hasAttribute(tabState.description),
- hasAttribute(tabState.componentError),
- tabState.componentErrorLevel,
- tab.getTabsheet().connector.getResourceUrl(
- ComponentConstants.ICON_RESOURCE + tabState.key),
- tabState.iconAltText);
-
- setClosable(tabState.closable);
-
- return ret;
- }
-
- private boolean hasAttribute(String string) {
- return string != null && !string.trim().isEmpty();
- }
-
- 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 += WidgetUtil.getRequiredWidth(closeButton);
- }
- return width;
- }
-
- public com.google.gwt.user.client.Element getCloseButton() {
- return DOM.asOld(closeButton);
- }
-
- }
-
- static class TabBar extends ComplexPanel implements 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 com.google.gwt.user.client.Element getContainerElement() {
- return DOM.asOld(tr);
- }
-
- /**
- * Gets the number of tabs from the tab bar.
- *
- * @return the number of tabs from the tab bar.
- */
- public int getTabCount() {
- return getWidgetCount();
- }
-
- /**
- * Adds a tab to the tab bar.
- *
- * @return the added tab.
- */
- 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);
- }
-
- getTabsheet().selectionHandler.registerTab(t);
-
- t.setCloseHandler(this);
-
- return t;
- }
-
- /**
- * Gets the tab sheet instance where the tab bar is attached to.
- *
- * @return the tab sheet instance where the tab bar is attached to.
- */
- public VTabsheet getTabsheet() {
- return tabsheet;
- }
-
- public Tab getTab(int index) {
- if (index < 0 || index >= getTabCount()) {
- return null;
- }
- return (Tab) super.getWidget(index);
- }
-
- private int getTabIndex(String tabId) {
- if (tabId == null) {
- return -1;
- }
- for (int i = 0; i < getTabCount(); i++) {
- if (tabId.equals(getTab(i).id)) {
- return i;
- }
- }
- return -1;
- }
-
- public void selectTab(int index) {
- final Tab newSelected = getTab(index);
- final Tab oldSelected = selected;
- if (oldSelected == newSelected) {
- return;
- }
-
- 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();
-
- // Scroll the tab into view if it is not already, after layout
- Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() {
- @Override
- public void execute() {
- getTabsheet()
- .scrollIntoView(getTab(tabsheet.activeTabIndex));
- }
- });
- }
-
- public Tab 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);
- }
-
- return newNavigated;
- }
-
- 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?
-
- int scrollerIndexCandidate = getTabIndex(
- getTabsheet().scrollerPositionTabId);
- if (scrollerIndexCandidate < 0) {
- // The tab with id scrollerPositionTabId has been removed
- scrollerIndexCandidate = getTabsheet().scrollerIndex;
- }
- scrollerIndexCandidate = selectNewShownTab(scrollerIndexCandidate);
- if (scrollerIndexCandidate >= 0
- && scrollerIndexCandidate < getTabCount()) {
- getTabsheet().scrollIntoView(getTab(scrollerIndexCandidate));
- }
- }
-
- private int selectNewShownTab(int oldPosition) {
- // After removing a tab, find a new scroll position. In most
- // cases the scroll position does not change, but if the tab
- // at the scroll position was removed, need to find a nearby
- // tab that is visible.
- for (int i = oldPosition; i < getTabCount(); i++) {
- Tab tab = getTab(i);
- if (!tab.isHiddenOnServer()) {
- return i;
- }
- }
-
- for (int i = oldPosition - 1; i >= 0; i--) {
- Tab tab = getTab(i);
- if (!tab.isHiddenOnServer()) {
- return i;
- }
- }
-
- return -1;
- }
-
- private boolean isFirstVisibleTab(int index) {
- return getFirstVisibleTab() == index;
- }
-
- /**
- * Returns the index of the first visible tab on the server
- */
- 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;
- }
- }
-
- /**
- * Returns the index of the first visible tab in browser.
- */
- private int getFirstVisibleTabClient() {
- int tabs = getTabCount();
- int i = 0;
- while (i < tabs && !getTab(i).isVisible()) {
- i++;
- }
-
- 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;
- }
-
- private void recalculateCaptionWidths() {
- for (int i = 0; i < getTabCount(); ++i) {
- getTab(i).recalculateCaptionWidth();
- }
- }
- }
-
- // TODO using the CLASSNAME directly makes primaryStyleName for TabSheet of
- // very limited use - all use of style names should be refactored in the
- // future
- public static final String CLASSNAME = TabsheetState.PRIMARY_STYLE_NAME;
-
- public static final String TABS_CLASSNAME = CLASSNAME + "-tabcontainer";
- public static final String SCROLLER_CLASSNAME = CLASSNAME + "-scroller";
-
- /** For internal use only. May be removed or replaced in the future. */
- // tabbar and 'scroller' container
- public final Element tabs;
-
- /**
- * 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();
-
- // tab-scroller element
- private final Element scroller;
- // tab-scroller next button element
- private final Element scrollerNext;
- // tab-scroller prev button element
- private final Element scrollerPrev;
-
- /**
- * The index of the first visible tab (when scrolled)
- */
- private int scrollerIndex = 0;
- /**
- * The id of the tab at position scrollerIndex. This is used for keeping the
- * scroll position unchanged when a tab is removed from the server side and
- * the removed tab lies to the left of the current scroll position. For
- * other cases scrollerIndex alone would be sufficient. Since the tab at the
- * current scroll position can be removed, scrollerIndex is required in
- * addition to this variable.
- */
- private String scrollerPositionTabId;
-
- final TabBar tb = new TabBar(this);
- /** For internal use only. May be removed or replaced in the future. */
- protected final VTabsheetPanel tabPanel = 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 (getApplicationConnection() == 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 tabIndex
- * The index of the tab to load
- *
- * @return true if the specified sheet gets loaded, otherwise false.
- */
- public boolean loadTabSheet(int tabIndex) {
- if (activeTabIndex != tabIndex && canSelectTab(tabIndex)) {
- tb.selectTab(tabIndex);
-
- activeTabIndex = tabIndex;
-
- addStyleDependentName("loading");
- // Hide the current contents so a loading indicator can be shown
- // instead
- getCurrentlyDisplayedWidget().getElement().getParentElement()
- .getStyle().setVisibility(Visibility.HIDDEN);
-
- getRpcProxy().setSelected(tabKeys.get(tabIndex).toString());
-
- waitingForResponse = true;
-
- tb.getTab(tabIndex).focus(); // move keyboard focus to active tab
-
- return true;
- }
-
- return false;
- }
-
- /**
- * Returns the currently displayed widget in the tab panel.
- *
- * @since 7.2
- * @return currently displayed content widget
- */
- public Widget getCurrentlyDisplayedWidget() {
- return tabPanel.getWidget(tabPanel.getVisibleWidget());
- }
-
- /**
- * Returns the client to server RPC proxy for the tabsheet.
- *
- * @since 7.2
- * @return RPC proxy
- */
- protected TabsheetServerRpc getRpcProxy() {
- return connector.getRpcProxy(TabsheetServerRpc.class);
- }
-
- /**
- * For internal use only.
- *
- * Avoid using this method directly and use appropriate superclass methods
- * where applicable.
- *
- * @deprecated since 7.2 - use more specific methods instead (getRpcProxy(),
- * getConnectorForWidget(Widget) etc.)
- * @return ApplicationConnection
- */
- @Deprecated
- public ApplicationConnection getApplicationConnection() {
- return client;
- }
-
- private VTooltip getVTooltip() {
- return getApplicationConnection().getVTooltip();
- }
-
- public void tabSizeMightHaveChanged(Tab tab) {
- // icon onloads may change total width of tabsheet
- if (isDynamicWidth()) {
- updateDynamicWidth();
- }
- updateTabScroller();
-
- }
-
- void sendTabClosedEvent(int tabIndex) {
- getRpcProxy().closeTab(tabKeys.get(tabIndex));
- }
-
- public VTabsheet() {
- super(CLASSNAME);
-
- // Tab scrolling
- getElement().getStyle().setOverflow(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 | Event.ONMOUSEDOWN);
-
- scrollerNext = DOM.createButton();
- scrollerNext.setTabIndex(-1);
- DOM.setElementProperty(scrollerNext, "className",
- SCROLLER_CLASSNAME + "Next");
- Roles.getTablistRole().setAriaHiddenState(scrollerNext, true);
- DOM.sinkEvents(scrollerNext, Event.ONCLICK | Event.ONMOUSEDOWN);
-
- DOM.appendChild(getElement(), tabs);
-
- // Tabs
- tabPanel.setStyleName(CLASSNAME + "-tabsheetpanel");
- contentNode = DOM.createDiv();
- Roles.getTabpanelRole().set(contentNode);
-
- deco = DOM.createDiv();
-
- 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(tabPanel, 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) {
- com.google.gwt.dom.client.Element eventTarget = DOM
- .eventGetTarget(event);
-
- if (event.getTypeInt() == Event.ONCLICK) {
-
- // Tab scrolling
- if (eventTarget == scrollerPrev || eventTarget == scrollerNext) {
- scrollAccordingToScrollTarget(eventTarget);
-
- event.stopPropagation();
- }
-
- } else if (event.getTypeInt() == Event.ONMOUSEDOWN) {
-
- if (eventTarget == scrollerPrev || eventTarget == scrollerNext) {
- // In case the focus was previously on a Tab, we need to cancel
- // the upcoming blur on the Tab which will follow this mouse
- // down event.
- focusBlurManager.cancelNextBlurSchedule();
-
- return;
- }
- }
-
- super.onBrowserEvent(event);
- }
-
- /*
- * Scroll the tab bar according to the last scrollTarget (the scroll button
- * pressed).
- */
- private void scrollAccordingToScrollTarget(
- com.google.gwt.dom.client.Element scrollTarget) {
- if (scrollTarget == null) {
- return;
- }
-
- int newFirstIndex = -1;
-
- // Scroll left.
- if (isScrolledTabs() && scrollTarget == scrollerPrev) {
- newFirstIndex = tb.scrollLeft(scrollerIndex);
-
- // Scroll right.
- } else if (isClippedTabs() && scrollTarget == scrollerNext) {
- newFirstIndex = tb.scrollRight(scrollerIndex);
- }
-
- if (newFirstIndex != -1) {
- scrollerIndex = newFirstIndex;
- scrollerPositionTabId = tb.getTab(scrollerIndex).id;
- updateTabScroller();
- }
-
- // For this to work well, make sure the method gets called only from
- // user events.
- selectionHandler.focusTabAtIndex(scrollerIndex);
- }
-
- /**
- * 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(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);
- }
- } else {
- tb.setStyleName(CLASSNAME + "-tabs");
- DOM.setElementProperty(tabs, "className", TABS_CLASSNAME);
- DOM.setElementProperty(contentNode, "className",
- CLASSNAME + "-content");
- DOM.setElementProperty(deco, "className", CLASSNAME + "-deco");
- }
- }
-
- /** 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 = tabPanel.getElement().getStyle();
- String overflow = style.getProperty("overflow");
- style.setProperty("overflow", "hidden");
- style.setPropertyPx("width", tabsWidth);
-
- boolean hasTabs = tabPanel.getWidgetCount() > 0;
-
- Style wrapperstyle = null;
- if (hasTabs) {
- wrapperstyle = getCurrentlyDisplayedWidget().getElement()
- .getParentElement().getStyle();
- wrapperstyle.setPropertyPx("width", tabsWidth);
- }
- // Get content width from actual widget
-
- int contentWidth = 0;
- if (hasTabs) {
- contentWidth = getCurrentlyDisplayedWidget().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();
- }
-
- private boolean isAllTabsBeforeIndexInvisible() {
- boolean invisible = true;
- for (int i = 0; i < scrollerIndex; i++) {
- invisible = invisible & !tb.getTab(i).isVisible();
- }
- return invisible;
- }
-
- private boolean isScrollerPrevDisabled() {
- return scrollerPrev.getClassName()
- .contains(PREV_SCROLLER_DISABLED_CLASSNAME);
- }
-
- private boolean isScrollerHidden() {
- return scroller.getStyle().getDisplay()
- .equals(Display.NONE.getCssName());
- }
-
- private boolean isIndexSkippingHiddenTabs() {
- return isAllTabsBeforeIndexInvisible()
- && (isScrollerPrevDisabled() || isScrollerHidden());
- }
-
- @Override
- public void renderTab(final TabState tabState, int index) {
- Tab tab = tb.getTab(index);
- if (tab == null) {
- tab = tb.addTab();
- }
-
- tab.updateFromState(tabState);
- tab.setEnabledOnServer((!disabledTabKeys.contains(tabKeys.get(index))));
- tab.setHiddenOnServer(!tabState.visible);
-
- if (scrolledOutOfView(index) && !isIndexSkippingHiddenTabs()) {
- // Should not set tabs visible if they are scrolled out of view
- tab.setVisible(false);
- } else {
- // When the tab was hidden and then turned visible again
- // and there is space for it, it should be in view (#17096) (#17333)
- if (isTabSetVisibleBeforeScroller(tabState, index, tab)) {
- scrollerIndex = index;
- tab.setVisible(true);
- tab.setStyleNames(false, true);
-
- // scroll to the currently selected tab if it got clipped
- // after making another tab visible
- if (isClippedTabs()) {
- scrollIntoView(getActiveTab());
- }
- } else {
- tab.setVisible(tabState.visible);
- }
- }
-
- /*
- * 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();
- }
-
- /**
- * Checks whether the tab has been set to visible and the scroller is at the
- * first visible tab. That means that the scroller has to be adjusted so
- * that the tab is visible again.
- */
- private boolean isTabSetVisibleBeforeScroller(TabState tabState, int index,
- Tab tab) {
- return isIndexSkippingHiddenTabs() && isScrollerAtFirstVisibleTab()
- && hasTabChangedVisibility(tabState, tab)
- && scrolledOutOfView(index);
- }
-
- /**
- * Checks whether the tab is visible on server but is not visible on client
- * yet.
- */
- private boolean hasTabChangedVisibility(TabState tabState, Tab tab) {
- return !tab.isVisible() && tabState.visible;
- }
-
- private boolean isScrollerAtFirstVisibleTab() {
- return tb.getFirstVisibleTabClient() == scrollerIndex;
- }
-
- /**
- * @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("");
- }
- }
-
- /**
- * Renders the widget content for a tab sheet.
- *
- * @param newWidget
- */
- public void renderContent(Widget newWidget) {
- assert tabPanel.getWidgetCount() <= 1;
-
- if (null == newWidget) {
- newWidget = new SimplePanel();
- }
-
- if (tabPanel.getWidgetCount() == 0) {
- tabPanel.add(newWidget);
- } else if (tabPanel.getWidget(0) != newWidget) {
- tabPanel.remove(0);
- tabPanel.add(newWidget);
- }
-
- assert tabPanel.getWidgetCount() <= 1;
-
- // There's never any other index than 0, but maintaining API for now
- tabPanel.showWidget(0);
-
- VTabsheet.this.iLayout();
- updateOpenTabSize();
- VTabsheet.this.removeStyleDependentName("loading");
- }
-
- /**
- * Recalculates the sizes of tab captions, causing the tabs to be rendered
- * the correct size.
- */
- private void updateTabCaptionSizes() {
- for (int tabIx = 0; tabIx < tb.getTabCount(); tabIx++) {
- tb.getTab(tabIx).recalculateCaptionWidth();
- }
- }
-
- /** For internal use only. May be removed or replaced in the future. */
- public void updateContentNodeHeight() {
- if (!isDynamicHeight()) {
- int contentHeight = getOffsetHeight();
- contentHeight -= deco.getOffsetHeight();
- contentHeight -= tb.getOffsetHeight();
-
- ComputedStyle cs = new ComputedStyle(contentNode);
- contentHeight -= Math.ceil(cs.getPaddingHeight());
- contentHeight -= Math.ceil(cs.getBorderHeight());
-
- if (contentHeight < 0) {
- contentHeight = 0;
- }
-
- // Set proper values for content element
- contentNode.getStyle().setHeight(contentHeight, Unit.PX);
- } else {
- contentNode.getStyle().clearHeight();
- }
- }
-
- /**
- * Run internal layouting.
- */
- public void iLayout() {
- updateTabScroller();
- updateTabCaptionSizes();
- }
-
- /**
- * 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();
- }
- tabPanel.fixVisibleTabSize(width, height, minWidth);
-
- }
-
- /**
- * Layouts the tab-scroller elements, and applies styles.
- */
- private void updateTabScroller() {
- if (!isDynamicWidth()) {
- tabs.getStyle().setWidth(100, Unit.PCT);
- }
-
- // 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)) {
- scroller.getStyle().clearDisplay();
- DOM.setElementProperty(scrollerPrev, "className", SCROLLER_CLASSNAME
- + (scrolled ? "Prev" : PREV_SCROLLER_DISABLED_CLASSNAME));
- DOM.setElementProperty(scrollerNext, "className",
- SCROLLER_CLASSNAME + (clipped ? "Next" : "Next-disabled"));
-
- // the active tab should be focusable if and only if it is visible
- boolean isActiveTabVisible = scrollerIndex <= activeTabIndex
- && !isClipped(tb.selected);
- tb.selected.setTabulatorIndex(
- isActiveTabVisible ? tabulatorIndex : -1);
-
- } else {
- scroller.getStyle().setDisplay(Display.NONE);
- }
-
- if (BrowserInfo.get().isSafariOrIOS()) {
- /*
- * 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();
- scrollerPositionTabId = scrollerIndex < 0 ? null
- : tb.getTab(scrollerIndex).id;
- 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);
- }
- tabPanel.clear();
-
- }
-
- @Override
- public Iterator<Widget> getWidgetIterator() {
- return tabPanel.iterator();
- }
-
- /** For internal use only. May be removed or replaced in the future. */
- public int getContentAreaBorderWidth() {
- return WidgetUtil.measureHorizontalBorder(contentNode);
- }
-
- @Override
- public int getTabCount() {
- return tb.getTabCount();
- }
-
- @Override
- public ComponentConnector getTab(int index) {
- if (tabPanel.getWidgetCount() > index) {
- Widget widget = tabPanel.getWidget(index);
- return getConnectorForWidget(widget);
- }
- return null;
- }
-
- @Override
- public void removeTab(int index) {
- tb.removeTab(index);
-
- // Removing content from tp is handled by the connector
- }
-
- @Override
- public void selectTab(int index) {
- tb.selectTab(index);
- }
-
- @Override
- public void focus() {
- getActiveTab().focus();
- }
-
- public void blur() {
- getActiveTab().blur();
- }
-
- /*
- * Gets the active tab.
- */
- private Tab getActiveTab() {
- return tb.getTab(activeTabIndex);
- }
-
- @Override
- public void setConnector(AbstractComponentConnector connector) {
- super.setConnector(connector);
-
- focusBlurManager.connector = connector;
- }
-
- /*
- * The focus and blur manager instance.
- */
- private FocusBlurManager focusBlurManager = new FocusBlurManager();
-
- /*
- * Generate the correct focus/blur events for the main TabSheet component
- * (#14304).
- *
- * The TabSheet must fire one focus event when the user clicks on the tab
- * bar (i.e. inner TabBar class) containing the Tabs or when the focus is
- * provided to the TabSheet by any means. Also one blur event should be
- * fired only when the user leaves the tab bar. After the user focus on the
- * tab bar and before leaving it, no matter how many times he's pressing the
- * Tabs or the scroll buttons, the TabSheet component should not fire any of
- * those blur/focus events.
- *
- * The only focusable elements contained in the tab bar are the Tabs (see
- * inner class Tab). The reason is the accessibility support.
- *
- * Having this in mind, the chosen solution path for our problem is to match
- * a sequence of focus/blur events on the tabs, choose only the first focus
- * and last blur events and pass only those further to the main component.
- * Any consecutive blur/focus events on 2 Tabs must be ignored.
- *
- * Because in a blur event we don't know whether or not a focus will follow,
- * we just defer a command initiated on the blur event to wait and see if
- * any focus will appear. The command will be executed after the next focus,
- * so if no focus was triggered in the mean while it'll submit the blur
- * event to the main component, otherwise it'll do nothing, so the main
- * component will not generate the blur..
- */
- private static class FocusBlurManager {
-
- // The real tab with focus on it. If the focus goes to another element
- // in the page this will be null.
- private Tab focusedTab;
-
- /*
- * Gets the focused tab.
- */
- private Tab getFocusedTab() {
- return focusedTab;
- }
-
- /*
- * Sets the local field tracking the focused tab.
- */
- private void setFocusedTab(Tab focusedTab) {
- this.focusedTab = focusedTab;
- }
-
- /*
- * The ultimate focus/blur event dispatcher.
- */
- private AbstractComponentConnector connector;
-
- /**
- * Delegate method for the onFocus event occurring on Tab.
- *
- * @since 7.2.6
- * @param newFocusTab
- * the new focused tab.
- * @see #onBlur(Tab)
- */
- public void onFocus(Tab newFocusTab) {
-
- if (connector.hasEventListener(EventId.FOCUS)) {
-
- // Send the focus event only first time when we focus on any
- // tab. The focused tab will be reseted on the last blur.
- if (focusedTab == null) {
- connector.getRpcProxy(FocusAndBlurServerRpc.class).focus();
- }
- }
-
- cancelLastBlurSchedule();
-
- setFocusedTab(newFocusTab);
- }
-
- /**
- * Delegate method for the onBlur event occurring on Tab.
- *
- * @param blurSource
- * the source of the blur.
- *
- * @see #onFocus(Tab)
- */
- public void onBlur(Tab blurSource) {
- if (focusedTab != null && focusedTab == blurSource) {
-
- if (connector.hasEventListener(EventId.BLUR)) {
- scheduleBlur(focusedTab);
- }
- }
- }
-
- /*
- * The last blur command to be executed.
- */
- private BlurCommand blurCommand;
-
- /*
- * Execute the final blur command.
- */
- private class BlurCommand implements Command {
-
- /*
- * The blur source.
- */
- private Tab blurSource;
-
- /**
- * Create the blur command using the blur source.
- *
- * @param blurSource
- * the source.
- * @param focusedTabProvider
- * provides the current focused tab.
- */
- public BlurCommand(Tab blurSource) {
- this.blurSource = blurSource;
- }
-
- /**
- * Stop the command from being executed.
- *
- * @since 7.4
- */
- public void stopSchedule() {
- blurSource = null;
- }
-
- /**
- * Schedule the command for a deferred execution.
- *
- * @since 7.4
- */
- public void scheduleDeferred() {
- Scheduler.get().scheduleDeferred(this);
- }
-
- @Override
- public void execute() {
-
- Tab focusedTab = getFocusedTab();
-
- if (blurSource == null) {
- return;
- }
-
- // The focus didn't change since this blur triggered, so
- // the new focused element is not a tab.
- if (focusedTab == blurSource) {
-
- // We're certain there's no focus anymore.
- focusedTab.removeAssistiveDescription();
- setFocusedTab(null);
-
- connector.getRpcProxy(FocusAndBlurServerRpc.class).blur();
- }
-
- // Call this to set it to null and be consistent.
- cancelLastBlurSchedule();
- }
- }
-
- /*
- * Schedule a new blur event for a deferred execution.
- */
- private void scheduleBlur(Tab blurSource) {
-
- if (nextBlurScheduleCancelled) {
-
- // This will set the stopNextBlurCommand back to false as well.
- cancelLastBlurSchedule();
-
- // Reset the status.
- nextBlurScheduleCancelled = false;
- return;
- }
-
- cancelLastBlurSchedule();
-
- blurCommand = new BlurCommand(blurSource);
- blurCommand.scheduleDeferred();
- }
-
- /**
- * Remove the last blur deferred command from execution.
- */
- public void cancelLastBlurSchedule() {
- if (blurCommand != null) {
- blurCommand.stopSchedule();
- blurCommand = null;
- }
-
- // We really want to make sure this flag gets reseted at any time
- // when something interact with the blur manager and ther's no blur
- // command scheduled (as we just canceled it).
- nextBlurScheduleCancelled = false;
- }
-
- /**
- * Cancel the next scheduled execution. This method must be called only
- * from an event occurring before the onBlur event. It's the case of IE
- * which doesn't trigger the focus event, so we're using this approach
- * to cancel the next blur event prior it's execution, calling the
- * method from mouse down event.
- */
- public void cancelNextBlurSchedule() {
-
- // Make sure there's still no other command to be executed.
- cancelLastBlurSchedule();
-
- nextBlurScheduleCancelled = true;
- }
-
- /*
- * Flag that the next deferred command won't get executed. This is
- * useful in case of IE where the user focus event don't fire and we're
- * using the mouse down event to track the focus. But the mouse down
- * event triggers before the blur, so we need to cancel the deferred
- * execution in advance.
- */
- private boolean nextBlurScheduleCancelled = false;
-
- }
-
- /*
- * The tabs selection handler instance.
- */
- private final TabSelectionHandler selectionHandler = new TabSelectionHandler();
-
- /*
- * Handle the events for selecting the tabs.
- */
- private class TabSelectionHandler implements FocusHandler, BlurHandler,
- KeyDownHandler, ClickHandler, MouseDownHandler {
-
- /** For internal use only. May be removed or replaced in the future. */
- // The current visible focused index.
- private int focusedTabIndex = 0;
-
- /**
- * Register the tab to the selection handler.
- *
- * @param tab
- * the tab to register.
- */
- public void registerTab(Tab tab) {
-
- tab.addBlurHandler(this);
- tab.addFocusHandler(this);
- tab.addKeyDownHandler(this);
- tab.addClickHandler(this);
- tab.addMouseDownHandler(this);
- }
-
- @Override
- public void onBlur(final BlurEvent event) {
-
- getVTooltip().hideTooltip();
-
- Object blurSource = event.getSource();
-
- if (blurSource instanceof Tab) {
- focusBlurManager.onBlur((Tab) blurSource);
- }
- }
-
- @Override
- public void onFocus(FocusEvent event) {
-
- if (event.getSource() instanceof Tab) {
- Tab focusSource = (Tab) event.getSource();
- focusBlurManager.onFocus(focusSource);
-
- if (focusSource.hasTooltip()) {
- focusSource.setAssistiveDescription(
- getVTooltip().getUniqueId());
- getVTooltip().showAssistive(focusSource.getTooltipInfo());
- }
-
- }
- }
-
- @Override
- public void onClick(ClickEvent event) {
-
- // IE doesn't trigger focus when click, so we need to make sure
- // the previous blur deferred command will get killed.
- focusBlurManager.cancelLastBlurSchedule();
-
- 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 = tb.getWidgetIndex(caption.getParent());
-
- tb.navigateTab(focusedTabIndex, index);
-
- focusedTabIndex = index;
-
- if (!loadTabSheet(index)) {
-
- // This needs to be called at the end, as the activeTabIndex
- // is set in the loadTabSheet.
- focus();
- }
- }
-
- @Override
- public void onMouseDown(MouseDownEvent event) {
-
- if (event.getSource() instanceof Tab) {
-
- // IE doesn't trigger focus when click, so we need to make sure
- // the
- // next blur deferred command will get killed.
- focusBlurManager.cancelNextBlurSchedule();
- }
- }
-
- @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);
-
- // Prevent the page from scrolling when hitting space
- // (select key) to select the current tab.
- event.preventDefault();
- }
- }
- }
- }
-
- /*
- * Left arrow key selection.
- */
- 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) {
- keySelectTab(newTabIndex);
- }
- }
-
- /*
- * Right arrow key selection.
- */
- 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()) {
- keySelectTab(newTabIndex);
- }
- }
-
- /*
- * Select the specified tab using left/right key.
- */
- private void keySelectTab(int newTabIndex) {
- Tab tab = tb.getTab(newTabIndex);
- if (tab == null) {
- return;
- }
-
- // Focus the tab, otherwise the selected one will loose focus and
- // TabSheet will get blurred.
- focusTabAtIndex(newTabIndex);
-
- tb.navigateTab(focusedTabIndex, newTabIndex);
-
- focusedTabIndex = newTabIndex;
- }
-
- /**
- * Focus the specified tab. Make sure to call this only from user
- * events, otherwise will break things.
- *
- * @param tabIndex
- * the index of the tab to set.
- */
- void focusTabAtIndex(int tabIndex) {
- Tab tabToFocus = tb.getTab(tabIndex);
- if (tabToFocus != null) {
- tabToFocus.focus();
- }
- }
-
- }
-
- /**
- * @return The key code of the keyboard shortcut that selects the previous
- * tab in a focused tabsheet.
- */
- protected int getPreviousTabKey() {
- return KeyCodes.KEY_LEFT;
- }
-
- /**
- * Gets the key to activate the selected tab when navigating using
- * previous/next (left/right) keys.
- *
- * @return the key to activate the selected tab.
- *
- * @see #getNextTabKey()
- * @see #getPreviousTabKey()
- */
- protected int getSelectTabKey() {
- return KeyCodes.KEY_SPACE;
- }
-
- /**
- * @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 scrollIntoView(Tab tab) {
-
- if (!tab.isHiddenOnServer()) {
-
- // Check for visibility first as clipped tabs to the right are
- // always visible.
- // On IE8 a tab with false visibility would have the bounds of the
- // full TabBar.
- if (!tab.isVisible()) {
- while (!tab.isVisible()) {
- scrollerIndex = tb.scrollLeft(scrollerIndex);
- }
- updateTabScroller();
-
- } else if (isClipped(tab)) {
- while (isClipped(tab) && scrollerIndex != -1) {
- scrollerIndex = tb.scrollRight(scrollerIndex);
- }
- updateTabScroller();
- }
- if (scrollerIndex >= 0 && scrollerIndex < tb.getTabCount()) {
- scrollerPositionTabId = tb.getTab(scrollerIndex).id;
- } else {
- scrollerPositionTabId = null;
- }
- }
- }
-
- /**
- * Makes tab bar visible.
- *
- * @since 7.2
- */
- public void showTabs() {
- tb.setVisible(true);
- removeStyleName(CLASSNAME + "-hidetabs");
- tb.recalculateCaptionWidths();
- }
-
- /**
- * Makes tab bar invisible.
- *
- * @since 7.2
- */
- public void hideTabs() {
- tb.setVisible(false);
- addStyleName(CLASSNAME + "-hidetabs");
- }
-
- /** Matches tab[ix] - used for extracting the index of the targeted tab */
- private static final RegExp SUBPART_TAB_REGEXP = RegExp
- .compile("tab\\[(\\d+)](.*)");
-
- @Override
- public com.google.gwt.user.client.Element getSubPartElement(
- String subPart) {
- if ("tabpanel".equals(subPart)) {
- return DOM.asOld(tabPanel.getElement().getFirstChildElement());
- } else if (SUBPART_TAB_REGEXP.test(subPart)) {
- MatchResult result = SUBPART_TAB_REGEXP.exec(subPart);
- int tabIx = Integer.valueOf(result.getGroup(1));
- Tab tab = tb.getTab(tabIx);
- if (tab != null) {
- if ("/close".equals(result.getGroup(2))) {
- if (tab.isClosable()) {
- return tab.tabCaption.getCloseButton();
- }
- } else {
- return tab.tabCaption.getElement();
- }
- }
- }
- return null;
- }
-
- @Override
- public String getSubPartName(
- com.google.gwt.user.client.Element subElement) {
- if (tabPanel.getElement().equals(subElement.getParentElement())
- || tabPanel.getElement().equals(subElement)) {
- return "tabpanel";
- } else {
- for (int i = 0; i < tb.getTabCount(); ++i) {
- Tab tab = tb.getTab(i);
- if (tab.isClosable() && tab.tabCaption.getCloseButton()
- .isOrHasChild(subElement)) {
- return "tab[" + i + "]/close";
- } else if (tab.getElement().isOrHasChild(subElement)) {
- return "tab[" + i + "]";
- }
- }
- }
- return null;
- }
- }
|