]> source.dussan.org Git - vaadin-framework.git/commitdiff
#3698 and #4505 fire tab change event when current tab gets disabled/hidden/removed
authorHenri Sara <henri.sara@itmill.com>
Tue, 25 May 2010 06:39:34 +0000 (06:39 +0000)
committerHenri Sara <henri.sara@itmill.com>
Tue, 25 May 2010 06:39:34 +0000 (06:39 +0000)
svn changeset:13337/svn branch:6.3

src/com/vaadin/ui/TabSheet.java

index 77f2653f0a834e150114c2bdd91f1befcc21b7ac..a69e89ee171fbe9518599a6e91df76284d605c9f 100644 (file)
@@ -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<Component> 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();
         }