From: Henri Sara Date: Tue, 25 May 2010 06:39:34 +0000 (+0000) Subject: #3698 and #4505 fire tab change event when current tab gets disabled/hidden/removed X-Git-Tag: 6.7.0.beta1~1653^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=06f72ea079f5a91181652b432fd7b10b489d4ca2;p=vaadin-framework.git #3698 and #4505 fire tab change event when current tab gets disabled/hidden/removed svn changeset:13337/svn branch:6.3 --- diff --git a/src/com/vaadin/ui/TabSheet.java b/src/com/vaadin/ui/TabSheet.java index 77f2653f0a..a69e89ee17 100644 --- a/src/com/vaadin/ui/TabSheet.java +++ b/src/com/vaadin/ui/TabSheet.java @@ -101,7 +101,8 @@ public class TabSheet extends AbstractComponentContainer { if (components.isEmpty()) { selected = null; } else { - selected = components.getFirst(); + // select the first enabled and visible tab, if any + updateSelection(); fireSelectedTabChange(); } } @@ -217,30 +218,6 @@ public class TabSheet extends AbstractComponentContainer { Tab tab = tabs.get(component); - /* - * If we have no selection, if the current selection is invisible or - * if the current selection is disabled (but the whole component is - * not) we select this tab instead - */ - Tab selectedTabInfo = null; - if (selected != null) { - selectedTabInfo = tabs.get(selected); - } - if (selected == null || selectedTabInfo == null - || !selectedTabInfo.isVisible() - || !selectedTabInfo.isEnabled()) { - - // The current selection is not valid so we need to change it - if (tab.isEnabled() && tab.isVisible()) { - selected = component; - } else { - /* - * The current selection is not valid but this tab cannot be - * selected either. - */ - selected = null; - } - } target.startTag("tab"); if (!tab.isEnabled() && tab.isVisible()) { target.addAttribute("disabled", true); @@ -414,11 +391,57 @@ public class TabSheet extends AbstractComponentContainer { public void setSelectedTab(Component c) { if (c != null && components.contains(c) && !c.equals(selected)) { selected = c; + updateSelection(); fireSelectedTabChange(); requestRepaint(); } } + /** + * Checks if the current selection is valid, and updates the selection if + * the previously selected component is not visible and enabled. + * + * This method does not fire tab change events, but the caller should do so + * if appropriate. + * + * @return true if selection was changed, false otherwise + */ + private boolean updateSelection() { + Component originalSelection = selected; + for (final Iterator i = getComponentIterator(); i.hasNext();) { + final Component component = i.next(); + + Tab tab = tabs.get(component); + + /* + * If we have no selection, if the current selection is invisible or + * if the current selection is disabled (but the whole component is + * not) we select this tab instead + */ + Tab selectedTabInfo = null; + if (selected != null) { + selectedTabInfo = tabs.get(selected); + } + if (selected == null || selectedTabInfo == null + || !selectedTabInfo.isVisible() + || !selectedTabInfo.isEnabled()) { + + // The current selection is not valid so we need to change + // it + if (tab.isEnabled() && tab.isVisible()) { + selected = component; + } else { + /* + * The current selection is not valid but this tab cannot be + * selected either. + */ + selected = null; + } + } + } + return originalSelection != selected; + } + /** * Gets the selected tab. * @@ -795,6 +818,9 @@ public class TabSheet extends AbstractComponentContainer { public void setEnabled(boolean enabled) { this.enabled = enabled; + if (updateSelection()) { + fireSelectedTabChange(); + } requestRepaint(); } @@ -804,6 +830,9 @@ public class TabSheet extends AbstractComponentContainer { public void setVisible(boolean visible) { this.visible = visible; + if (updateSelection()) { + fireSelectedTabChange(); + } requestRepaint(); }