diff options
5 files changed, 359 insertions, 2 deletions
diff --git a/src/com/vaadin/ui/TabSheet.java b/src/com/vaadin/ui/TabSheet.java index 9b4b6f7abb..6e4cf12476 100644 --- a/src/com/vaadin/ui/TabSheet.java +++ b/src/com/vaadin/ui/TabSheet.java @@ -221,6 +221,32 @@ public class TabSheet extends AbstractComponentContainer { * @return the created {@link Tab} */ public Tab addTab(Component c, String caption, Resource icon) { + return addTab(c, caption, icon, components.size()); + } + + /** + * Adds a new tab into TabSheet. + * + * 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 - should not be null. + * @param caption + * the caption to be set for the component and used rendered in + * tab bar + * @param icon + * the icon to be set for the component and used rendered in tab + * bar + * @param index + * the index at where the the tab should be added. + * @return the created {@link Tab} + */ + public Tab addTab(Component c, String caption, Resource icon, int index) { if (c == null) { return null; } else if (tabs.containsKey(c)) { @@ -229,7 +255,14 @@ public class TabSheet extends AbstractComponentContainer { tab.setIcon(icon); return tab; } else { - components.addLast(c); + if (index >= components.size()) { + components.addLast(c); + } else if (index < 0) { + components.addFirst(c); + } else { + components.add(index, c); + } + Tab tab = new TabSheetTabImpl(caption, icon); tabs.put(c, tab); @@ -254,12 +287,28 @@ public class TabSheet extends AbstractComponentContainer { * @return the created {@link Tab} */ public Tab addTab(Component c) { + return addTab(c, components.size()); + } + + /** + * 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 - should not be null. + * @param index + * The index where the tab should be added + * @return the created {@link Tab} + */ + public Tab addTab(Component c, int index) { if (c == null) { return null; } else if (tabs.containsKey(c)) { return tabs.get(c); } else { - return addTab(c, c.getCaption(), c.getIcon()); + return addTab(c, c.getCaption(), c.getIcon(), index); } } @@ -482,6 +531,22 @@ public class TabSheet extends AbstractComponentContainer { } /** + * Returns the {@link Tab} (metadata) for a component. The {@link Tab} + * object can be used for setting caption,icon, etc for the tab. + * + * @param index + * the index of the tab + * @return + */ + public Tab getTab(int index) { + Component c = components.get(index); + if (c != null) { + return tabs.get(c); + } + return null; + } + + /** * Sets the selected tab. The tab is identified by the tab content * component. * @@ -913,6 +978,10 @@ public class TabSheet extends AbstractComponentContainer { */ public ErrorMessage getComponentError(); + /** + * Get the component related to the Tab + */ + public Component getComponent(); } /** @@ -1011,6 +1080,15 @@ public class TabSheet extends AbstractComponentContainer { this.componentError = componentError; requestRepaint(); } + + public Component getComponent() { + for (Map.Entry<Component, Tab> entry : tabs.entrySet()) { + if (entry.getValue() == this) { + return entry.getKey(); + } + } + return null; + } } /** @@ -1051,4 +1129,31 @@ public class TabSheet extends AbstractComponentContainer { public void setCloseHandler(CloseHandler handler) { closeHandler = handler; } + + /** + * Sets the poistion of the tab. + * + * @param tab + * The tab + * @param index + * The new index of the tab + */ + public void setTabIndex(Tab tab, int index) { + int oldIndex = getTabIndex(tab); + components.remove(oldIndex); + components.add(index, tab.getComponent()); + requestRepaint(); + } + + /** + * Gets the position of the tab + * + * @param tab + * The tab + * @return + */ + public int getTabIndex(Tab tab) { + return components.indexOf(tab.getComponent()); + } + } diff --git a/tests/src/com/vaadin/tests/components/tabsheet/TabGetAndReplaceComponent.html b/tests/src/com/vaadin/tests/components/tabsheet/TabGetAndReplaceComponent.html new file mode 100644 index 0000000000..fce39361e1 --- /dev/null +++ b/tests/src/com/vaadin/tests/components/tabsheet/TabGetAndReplaceComponent.html @@ -0,0 +1,72 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head profile="http://selenium-ide.openqa.org/profiles/test-case"> +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> +<link rel="selenium.base" href="" /> +<title>New Test</title> +</head> +<body> +<table cellpadding="1" cellspacing="1" border="1"> +<thead> +<tr><td rowspan="1" colspan="3">New Test</td></tr> +</thead><tbody> +<tr> + <td>open</td> + <td>/run/com.vaadin.tests.components.tabsheet.TabGetAndReplaceComponent?restartApplication</td> + <td></td> +</tr> +<tr> + <td>mouseClick</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabGetAndReplaceComponent::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTabsheet[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[1]/domChild[0]/domChild[0]/domChild[0]</td> + <td>18,13</td> +</tr> +<tr> + <td>assertText</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabGetAndReplaceComponent::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTabsheet[0]/VTabsheetPanel[0]/VLabel[1]</td> + <td>Content 2</td> +</tr> +<tr> + <td>click</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabGetAndReplaceComponent::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VButton[0]/domChild[0]/domChild[0]</td> + <td></td> +</tr> +<tr> + <td>assertText</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabGetAndReplaceComponent::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTabsheet[0]/VTabsheetPanel[0]/VLabel[1]</td> + <td>Replacement 2</td> +</tr> +<tr> + <td>mouseClick</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabGetAndReplaceComponent::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTabsheet[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[2]/domChild[0]/domChild[0]/domChild[0]</td> + <td>41,13</td> +</tr> +<tr> + <td>mouseClick</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabGetAndReplaceComponent::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTabsheet[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[3]/domChild[0]/domChild[0]/domChild[0]</td> + <td>16,11</td> +</tr> +<tr> + <td>mouseClick</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabGetAndReplaceComponent::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTabsheet[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[2]/domChild[0]/domChild[0]/domChild[0]</td> + <td>39,11</td> +</tr> +<tr> + <td>mouseClick</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabGetAndReplaceComponent::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTabsheet[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[1]/domChild[0]/domChild[0]/domChild[0]</td> + <td>42,12</td> +</tr> +<tr> + <td>mouseClick</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabGetAndReplaceComponent::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTabsheet[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]</td> + <td>37,10</td> +</tr> +<tr> + <td>assertText</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabGetAndReplaceComponent::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTabsheet[0]/VTabsheetPanel[0]/VLabel[0]</td> + <td>Content 1</td> +</tr> + +</tbody></table> +</body> +</html> diff --git a/tests/src/com/vaadin/tests/components/tabsheet/TabGetAndReplaceComponent.java b/tests/src/com/vaadin/tests/components/tabsheet/TabGetAndReplaceComponent.java new file mode 100644 index 0000000000..c49133dd7c --- /dev/null +++ b/tests/src/com/vaadin/tests/components/tabsheet/TabGetAndReplaceComponent.java @@ -0,0 +1,54 @@ +package com.vaadin.tests.components.tabsheet; + +import java.util.Iterator; + +import com.vaadin.tests.components.TestBase; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Component; +import com.vaadin.ui.Label; +import com.vaadin.ui.TabSheet; +import com.vaadin.ui.TabSheet.Tab; + +public class TabGetAndReplaceComponent extends TestBase { + + @Override + protected void setup() { + final TabSheet tabs = new TabSheet(); + + tabs.addTab(new Label("Content 1"), "Content 1", null); + tabs.addTab(new Label("Content 2"), "Content 2", null); + tabs.addTab(new Label("Content 3"), "Content 3", null); + tabs.addTab(new Label("Content 4"), "Content 4", null); + tabs.addTab(new Label("Content 5"), "Content 5", null); + addComponent(tabs); + + Button replace2 = new Button("Replace Content 2", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + Iterator<Component> iter = tabs.getComponentIterator(); + iter.next(); + + Component content2 = iter.next(); + Tab tab = tabs.getTab(content2); + + // Replace content + tabs.replaceComponent(tab.getComponent(), new Label( + "Replacement 2")); + + } + }); + addComponent(replace2); + } + + @Override + protected String getDescription() { + return "The tab should have a reference to the component it holds"; + } + + @Override + protected Integer getTicketNumber() { + return 6188; + } + +} diff --git a/tests/src/com/vaadin/tests/components/tabsheet/TabSheetIndexOperations.html b/tests/src/com/vaadin/tests/components/tabsheet/TabSheetIndexOperations.html new file mode 100644 index 0000000000..8db5641603 --- /dev/null +++ b/tests/src/com/vaadin/tests/components/tabsheet/TabSheetIndexOperations.html @@ -0,0 +1,52 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head profile="http://selenium-ide.openqa.org/profiles/test-case"> +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> +<link rel="selenium.base" href="" /> +<title>New Test</title> +</head> +<body> +<table cellpadding="1" cellspacing="1" border="1"> +<thead> +<tr><td rowspan="1" colspan="3">New Test</td></tr> +</thead><tbody> +<tr> + <td>open</td> + <td>/run/com.vaadin.tests.components.tabsheet.TabSheetIndexOperations?restartApplication</td> + <td></td> +</tr> +<tr> + <td>click</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabSheetIndexOperations::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VButton[0]/domChild[0]/domChild[0]</td> + <td></td> +</tr> +<tr> + <td>assertText</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabSheetIndexOperations::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTabsheet[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[2]/domChild[0]/domChild[0]/domChild[0]</td> + <td>Added Tab 1</td> +</tr> +<tr> + <td>click</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabSheetIndexOperations::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VButton[0]/domChild[0]/domChild[0]</td> + <td></td> +</tr> +<tr> + <td>assertText</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabSheetIndexOperations::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTabsheet[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[2]/domChild[0]/domChild[0]/domChild[0]</td> + <td>1 baT deddA</td> +</tr> +<tr> + <td>click</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabSheetIndexOperations::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[3]/VButton[0]/domChild[0]/domChild[0]</td> + <td></td> +</tr> +<tr> + <td>assertText</td> + <td>vaadin=runcomvaadintestscomponentstabsheetTabSheetIndexOperations::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTabsheet[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[2]/domChild[0]/domChild[0]/domChild[0]</td> + <td>Tab 1</td> +</tr> + +</tbody></table> +</body> +</html> diff --git a/tests/src/com/vaadin/tests/components/tabsheet/TabSheetIndexOperations.java b/tests/src/com/vaadin/tests/components/tabsheet/TabSheetIndexOperations.java new file mode 100644 index 0000000000..31bf1f1588 --- /dev/null +++ b/tests/src/com/vaadin/tests/components/tabsheet/TabSheetIndexOperations.java @@ -0,0 +1,74 @@ +package com.vaadin.tests.components.tabsheet; + +import com.vaadin.tests.components.TestBase; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Label; +import com.vaadin.ui.TabSheet; +import com.vaadin.ui.TabSheet.Tab; + +public class TabSheetIndexOperations extends TestBase { + + private int tabCounter = 1; + + @Override + protected void setup() { + final TabSheet tabs = new TabSheet(); + + // Add some tabs + tabs.addTab(new Label("Content 1"), "Tab 1", null); + tabs.addTab(new Label("Content 2"), "Tab 2", null); + tabs.addTab(new Label("Content 3"), "Tab 3", null); + + addComponent(tabs); + + Button addTab = new Button("Add tab at index 2", new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + tabs.addTab(new Label("Content " + tabCounter), + "Added Tab " + tabCounter, null, 2); + tabCounter++; + } + }); + addComponent(addTab); + + Button setCaption = new Button("Invert tab caption at index 2", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + Tab tab = tabs.getTab(2); + tab.setCaption(new StringBuffer(tab.getCaption()) + .reverse().toString()); + } + }); + addComponent(setCaption); + + Button move = new Button("Move selected tab to index 2", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + tabs.setTabIndex(tabs.getTab(tabs.getSelectedTab()), 2); + } + }); + addComponent(move); + + Button getIndex = new Button("Get selected tab index", new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + getMainWindow().showNotification( + "Index: " + + tabs.getTabIndex(tabs.getTab(tabs + .getSelectedTab()))); + + } + }); + addComponent(getIndex); + } + + @Override + protected String getDescription() { + return "You can use indexes to add and reorder the TabSheet"; + } + + @Override + protected Integer getTicketNumber() { + return 6188; + } + +} |