diff --git a/modules/web/src/com/haulmont/cuba/web/toolkit/ui/CubaManagedTabSheet.java b/modules/web/src/com/haulmont/cuba/web/toolkit/ui/CubaManagedTabSheet.java index 2e5fed8294..0de6eaf253 100644 --- a/modules/web/src/com/haulmont/cuba/web/toolkit/ui/CubaManagedTabSheet.java +++ b/modules/web/src/com/haulmont/cuba/web/toolkit/ui/CubaManagedTabSheet.java @@ -133,6 +133,7 @@ public class CubaManagedTabSheet extends CssLayout closeHandler.onTabClose(this, tabContent); } + @Override public TabSheetBehaviour getTabSheetBehaviour() { return behaviour; } @@ -608,7 +609,8 @@ public class CubaManagedTabSheet extends CssLayout @Override public void setTabTestId(String tabId, String testId) { - tabSheet.tabbedHeader.setTestId(((TabImpl) tabSheet.tabIds.get(tabId)).getTabbarTab(), testId); + TabImpl tabImpl = (TabImpl) tabSheet.tabIds.get(tabId); + tabSheet.tabbedHeader.setTestId(tabImpl.getTabbarTab(), testId); } @Override diff --git a/modules/web/src/com/haulmont/cuba/web/toolkit/ui/CubaTabSheet.java b/modules/web/src/com/haulmont/cuba/web/toolkit/ui/CubaTabSheet.java index 169caa4ff8..a6e8e35074 100644 --- a/modules/web/src/com/haulmont/cuba/web/toolkit/ui/CubaTabSheet.java +++ b/modules/web/src/com/haulmont/cuba/web/toolkit/ui/CubaTabSheet.java @@ -41,7 +41,7 @@ public class CubaTabSheet extends DDTabSheet implements Action.Container, HasTab protected Stack openedComponents = new Stack<>(); - protected HashSet actionHandlers = new HashSet<>(); + protected Set actionHandlers = null; // lazily initialized protected KeyMapper actionMapper = null; @@ -56,7 +56,7 @@ public class CubaTabSheet extends DDTabSheet implements Action.Container, HasTab public void onTabContextMenu(int tabIndex) { Tab tab = getTab(tabIndex); if (tab != null) { - HashSet actions = getActions(CubaTabSheet.this.getActionTarget(tab)); + Set actions = getActions(CubaTabSheet.this.getActionTarget(tab)); if (!actions.isEmpty()) { actionMapper = new KeyMapper<>(); @@ -81,7 +81,14 @@ public class CubaTabSheet extends DDTabSheet implements Action.Container, HasTab if (tab != null) { if (actionMapper != null) { Action action = actionMapper.get(actionKey); - Action.Handler[] handlers = actionHandlers.toArray(new Action.Handler[actionHandlers.size()]); + Action.Handler[] handlers; + + if (actionHandlers != null) { + handlers = actionHandlers.toArray(new Action.Handler[actionHandlers.size()]); + } else { + handlers = new Action.Handler[0]; + } + for (Action.Handler handler : handlers) { handler.handleAction(action, this, CubaTabSheet.this.getActionTarget(tab)); } @@ -114,7 +121,7 @@ public class CubaTabSheet extends DDTabSheet implements Action.Container, HasTab behaviour = new TabSheetBehaviourImpl(this); } - protected HashSet getActions(Component actionTarget) { + protected Set getActions(Component actionTarget) { HashSet actions = new LinkedHashSet<>(); if (actionHandlers != null) { for (Action.Handler handler : actionHandlers) { @@ -136,7 +143,7 @@ public class CubaTabSheet extends DDTabSheet implements Action.Container, HasTab public void beforeClientResponse(boolean initial) { super.beforeClientResponse(initial); - getState().hasActionsHandlers = !actionHandlers.isEmpty(); + getState().hasActionsHandlers = actionHandlers != null && !actionHandlers.isEmpty(); } public Component getPreviousTab(Component tab) { @@ -203,7 +210,12 @@ public class CubaTabSheet extends DDTabSheet implements Action.Container, HasTab @Override public void removeComponent(Component c) { + Tab tab = tabs.get(c); + super.removeComponent(c); + + tabIds.inverse().remove(tab); + if (c != null && closeHandlers != null) { closeHandlers.remove(c); if (closeHandlers.isEmpty()) { @@ -212,18 +224,50 @@ public class CubaTabSheet extends DDTabSheet implements Action.Container, HasTab } } + @Override + public void replaceComponent(Component oldComponent, Component newComponent) { + Tab oldTab = tabs.get(oldComponent); + + super.replaceComponent(oldComponent, newComponent); + + Tab newTab = tabs.get(newComponent); + + String oldTabId = tabIds.inverse().get(oldTab); + String newTabId = tabIds.inverse().get(newTab); + + if (oldTabId != null) { + tabIds.remove(oldTabId); + if (newTab != null) { + tabIds.put(oldTabId, newTab); + } + } + if (newTabId != null) { + tabIds.remove(newTabId); + if (oldTab != null) { + tabIds.put(newTabId, oldTab); + } + } + } + @Override public void addActionHandler(Action.Handler actionHandler) { + if (actionHandlers == null) { + actionHandlers = new LinkedHashSet<>(); + } actionHandlers.add(actionHandler); } @Override public void removeActionHandler(Action.Handler actionHandler) { - actionHandlers.remove(actionHandler); + if (actionHandlers != null) { + actionHandlers.remove(actionHandler); + } } public void moveTab(Component c, int position) { Tab oldTab = getTab(c); + String tabId = tabIds.inverse().get(oldTab); + String tabCubaId = getCubaId(oldTab); // do not detach close handler @@ -241,6 +285,11 @@ public class CubaTabSheet extends DDTabSheet implements Action.Container, HasTab newTab.setStyleName(oldTab.getStyleName()); setCubaId(newTab, tabCubaId); + + if (tabId != null) { + tabIds.remove(tabId); + tabIds.put(tabId, newTab); + } } public void setTabCloseHandler(Component tabContent, TabCloseHandler closeHandler) { @@ -337,7 +386,8 @@ public class CubaTabSheet extends DDTabSheet implements Action.Container, HasTab @Override public String getTab(int position) { - return tabSheet.getTab(position).getId(); + Tab tab = tabSheet.getTab(position); + return tabSheet.tabIds.inverse().get(tab); } @Override