I know that the user can track the list of opened tabs externally, yet I think it would be nice to have the tracking built-in in the TabBarView. I augmented the TabBarView with methods I have found useful, please feel free to include it in TabBarView if you agree:
public class MyTabBar extends TabBarView {
@NotNull
private final List<TabSheet.Tab> tabs = new ArrayList<>();
@Override
public TabSheet.Tab addTab(Component tabContent, String caption, Resource icon) {
final TabSheet.Tab tab = super.addTab(tabContent, caption, icon);
tabs.add(tab);
return tab;
}
/**
- Returns all tabs present in this tab bar.
- @return the list of tabs, not null, may be empty.
-/
@NotNull
public List<TabSheet.Tab> getTabs() {
return Collections.unmodifiableList(tabs);
}
/**
- Finds the tab by its content component.
- @param component the component, not null.
- @return the tab, not null.
- @throws IllegalArgumentException if there is no such tab.
-/
@NotNull
public TabSheet.Tab getTab(@NotNull Component component) {
for (TabSheet.Tab tab : getTabs()) {
if (tab.getComponent() == component) {
return tab;
}
}
throw new IllegalArgumentException("Parameter component: invalid value " + component + ": no such tab for that component");
}
/**
- Checks if the tab bar has any tabs registered.
- @return true if there are any tabs present - that is, {@link #getTabs()} returns a non-empty list.
-/
public boolean hasTabs() {
return !getTabs().isEmpty();
}
@Override
public void removeTab(@NotNull Component c) {
tabs.remove(getTab(c));
try {
super.removeTab(c);
} catch (NoSuchElementException ex) {
// workaround for https://dev.vaadin.com/ticket/19499#ticket
if (hasTabs()) {
setSelectedTab(getTab(0));
}
}
}
/**
- Removes all tabs from this tab bar.
-/
public void removeAllTabs() {
for (TabSheet.Tab tab : new ArrayList<>(tabs)) {
removeTab(tab);
}
}
/**
- Returns the currently selected tab.
- @return currently selected tab, null if there are no tabs present (the tab bar is empty).
-/
@Nullable
public TabSheet.Tab getSelectedTab() {
// workaround for https://dev.vaadin.com/ticket/11711
return getSelelectedTab();
}
/**
- Selects a tab based on the tab index.
- @param tabIndex the tab index.
- @throws IndexOutOfBoundsException if there is no such tab
-/
public void setSelectedTab(int tabIndex) {
setSelectedTab(getTabs().get(tabIndex));
}
/**
- Returns i-th tab.
- @param tabIndex the tab index.
- @return i-th tab, not null.
- @throws IndexOutOfBoundsException if there is no such tab
-/
@NotNull
public TabSheet.Tab getTab(int tabIndex) {
return getTabs().get(tabIndex);
}
@NotNull
public Component getTabComponent(int tabIndex) {
return getTab(tabIndex).getComponent();
}
}
Originally by mvysny2
I know that the user can track the list of opened tabs externally, yet I think it would be nice to have the tracking built-in in the TabBarView. I augmented the TabBarView with methods I have found useful, please feel free to include it in TabBarView if you agree:
Imported from https://dev.vaadin.com/ issue #19500