summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorBogdan Udrescu <bogdan@vaadin.com>2014-08-13 10:02:42 +0300
committerVaadin Code Review <review@vaadin.com>2014-08-25 12:35:17 +0000
commit117c45ae1c3753e38c2ac41296ebc043c74b835e (patch)
treedd9d02ed2e579a4faf33dc755caf490d86b4dc2d /server
parentb77b2045be287f2c8cc7745c02d39c4ad6c50399 (diff)
downloadvaadin-framework-117c45ae1c3753e38c2ac41296ebc043c74b835e.tar.gz
vaadin-framework-117c45ae1c3753e38c2ac41296ebc043c74b835e.zip
Improve the selection tab algorithm after removing the selected tab
(#6876) New protected method TabSheet.selectedTabIndexAfterTabRemove where anyone can implement his/hers own algorithm for selecting a new tab. Change-Id: I6a3dd62e7fc84e4dacb08d30d567f357678dd7e4
Diffstat (limited to 'server')
-rw-r--r--server/src/com/vaadin/ui/TabSheet.java55
1 files changed, 55 insertions, 0 deletions
diff --git a/server/src/com/vaadin/ui/TabSheet.java b/server/src/com/vaadin/ui/TabSheet.java
index 8b13ecf1a4..0e4ea0d034 100644
--- a/server/src/com/vaadin/ui/TabSheet.java
+++ b/server/src/com/vaadin/ui/TabSheet.java
@@ -193,6 +193,9 @@ public class TabSheet extends AbstractComponentContainer implements Focusable,
@Override
public void removeComponent(Component component) {
if (component != null && components.contains(component)) {
+
+ int componentIndex = components.indexOf(component);
+
super.removeComponent(component);
keyMapper.remove(component);
components.remove(component);
@@ -206,6 +209,24 @@ public class TabSheet extends AbstractComponentContainer implements Focusable,
if (components.isEmpty()) {
setSelected(null);
} else {
+
+ int newSelectedIndex = selectedTabIndexAfterTabRemove(componentIndex);
+
+ // Make sure the component actually exists, in case someone
+ // override it and provide a non existing component.
+ if (0 <= newSelectedIndex
+ && newSelectedIndex < components.size()) {
+
+ Component newSelectedComponent = components
+ .get(newSelectedIndex);
+
+ // Select only if the tab is enabled.
+ Tab newSelectedTab = tabs.get(newSelectedComponent);
+ if (newSelectedTab.isEnabled()) {
+ setSelected(newSelectedComponent);
+ }
+ }
+
// select the first enabled and visible tab, if any
updateSelection();
fireSelectedTabChange();
@@ -216,6 +237,40 @@ public class TabSheet extends AbstractComponentContainer implements Focusable,
}
/**
+ * Called when a selected tab is removed to specify the new tab to select.
+ * Can be overridden to provide another algorithm that calculates the new
+ * selection.
+ *
+ * Current implementation will choose the first enabled tab to the right of
+ * the removed tab if it's not the last one, otherwise will choose the
+ * closer enabled tab to the left.
+ *
+ * @since
+ * @param removedTabIndex
+ * the index of the selected tab which was just remove.
+ * @return the index of the tab to be selected or -1 if there are no more
+ * enabled tabs to select.
+ */
+ protected int selectedTabIndexAfterTabRemove(int removedTabIndex) {
+
+ for (int i = removedTabIndex; i < components.size(); i++) {
+ Tab tab = getTab(i);
+ if (tab.isEnabled()) {
+ return i;
+ }
+ }
+
+ for (int i = removedTabIndex - 1; i >= 0; i--) {
+ Tab tab = getTab(i);
+ if (tab.isEnabled()) {
+ return i;
+ }
+ }
+
+ return -1;
+ }
+
+ /**
* Removes a {@link Tab} and the component associated with it, as previously
* added with {@link #addTab(Component)},
* {@link #addTab(Component, String, Resource)} or