From 5e516e776ecfadf8f343360644ada9fbd1e22ce8 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Wed, 26 May 2010 11:08:15 +0000 Subject: [PATCH] #4641 Adding the same component multiple times to a TabSheet caused NPE svn changeset:13373/svn branch:6.3 --- src/com/vaadin/ui/TabSheet.java | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/com/vaadin/ui/TabSheet.java b/src/com/vaadin/ui/TabSheet.java index 051004aeee..f6dae38372 100644 --- a/src/com/vaadin/ui/TabSheet.java +++ b/src/com/vaadin/ui/TabSheet.java @@ -170,8 +170,12 @@ public class TabSheet extends AbstractComponentContainer { * The first tab added to a tab sheet is automatically selected and a tab * selection event is fired. * + * If the component is already present in the tab sheet, changes its caption + * and icon and returns the corresponding (old) tab, preserving other tab + * metadata. + * * @param c - * the component to be added onto tab. + * the component to be added onto tab - should not be null. * @param caption * the caption to be set for the component and used rendered in * tab bar @@ -181,7 +185,14 @@ public class TabSheet extends AbstractComponentContainer { * @return the created {@link Tab} */ public Tab addTab(Component c, String caption, Resource icon) { - if (c != null) { + if (c == null) { + return null; + } else if (tabs.containsKey(c)) { + Tab tab = tabs.get(c); + tab.setCaption(caption); + tab.setIcon(icon); + return tab; + } else { components.addLast(c); Tab tab = new TabSheetTabImpl(caption, icon); @@ -193,8 +204,6 @@ public class TabSheet extends AbstractComponentContainer { super.addComponent(c); requestRepaint(); return tab; - } else { - return null; } } @@ -202,15 +211,20 @@ public class TabSheet extends AbstractComponentContainer { * Adds a new tab into TabSheet. Component caption and icon are copied to * the tab metadata at creation time. * + * If the tab sheet already contains the component, its tab is returned. + * * @param c - * the component to be added onto tab. + * the component to be added onto tab - should not be null. * @return the created {@link Tab} */ public Tab addTab(Component c) { - if (c != null) { + if (c == null) { + return null; + } else if (tabs.containsKey(c)) { + return tabs.get(c); + } else { return addTab(c, c.getCaption(), c.getIcon()); } - return null; } /** -- 2.39.5