Browse Source

- Changed get/setTabIndex to get/setTabPosition since tabIndex might be associated with tabbing

- Changed implementation of addTab (with position) so that it never moves a existing tab.
- Added unit tests

#6188

svn changeset:16617/svn branch:6.5
tags/6.7.0.beta1
John Alhroos 13 years ago
parent
commit
7b676e2d71

+ 18
- 19
src/com/vaadin/ui/TabSheet.java View File

@@ -232,7 +232,7 @@ public class TabSheet extends AbstractComponentContainer {
*
* 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.
* metadata like the position.
*
* @param c
* the component to be added onto tab - should not be null.
@@ -242,21 +242,20 @@ public class TabSheet extends AbstractComponentContainer {
* @param icon
* the icon to be set for the component and used rendered in tab
* bar
* @param index
* @param position
* 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) {
public Tab addTab(Component c, String caption, Resource icon, int position) {
if (c == null) {
return null;
} else if (tabs.containsKey(c)) {
Tab tab = tabs.get(c);
setTabIndex(tab, index);
tab.setCaption(caption);
tab.setIcon(icon);
return tab;
} else {
components.add(index, c);
components.add(position, c);

Tab tab = new TabSheetTabImpl(caption, icon);

@@ -293,17 +292,17 @@ public class TabSheet extends AbstractComponentContainer {
*
* @param c
* the component to be added onto tab - should not be null.
* @param index
* @param position
* The index where the tab should be added
* @return the created {@link Tab}
*/
public Tab addTab(Component c, int index) {
public Tab addTab(Component c, int position) {
if (c == null) {
return null;
} else if (tabs.containsKey(c)) {
return tabs.get(c);
} else {
return addTab(c, c.getCaption(), c.getIcon(), index);
return addTab(c, c.getCaption(), c.getIcon(), position);
}
}

@@ -529,12 +528,12 @@ 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
* @param position
* the position of the tab
* @return
*/
public Tab getTab(int index) {
Component c = components.get(index);
public Tab getTab(int position) {
Component c = components.get(position);
if (c != null) {
return tabs.get(c);
}
@@ -1130,13 +1129,13 @@ public class TabSheet extends AbstractComponentContainer {
*
* @param tab
* The tab
* @param index
* The new index of the tab
* @param position
* The new position of the tab
*/
public void setTabIndex(Tab tab, int index) {
int oldIndex = getTabIndex(tab);
components.remove(oldIndex);
components.add(index, tab.getComponent());
public void setTabPosition(Tab tab, int position) {
int oldPosition = getTabPosition(tab);
components.remove(oldPosition);
components.add(position, tab.getComponent());
requestRepaint();
}

@@ -1147,7 +1146,7 @@ public class TabSheet extends AbstractComponentContainer {
* The tab
* @return
*/
public int getTabIndex(Tab tab) {
public int getTabPosition(Tab tab) {
return components.indexOf(tab.getComponent());
}


+ 3
- 2
tests/src/com/vaadin/tests/components/tabsheet/TabSheetIndexOperations.java View File

@@ -44,7 +44,8 @@ public class TabSheetIndexOperations extends TestBase {
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);
tabs.setTabPosition(tabs.getTab(tabs.getSelectedTab()),
2);
}
});
addComponent(move);
@@ -53,7 +54,7 @@ public class TabSheetIndexOperations extends TestBase {
public void buttonClick(ClickEvent event) {
getMainWindow().showNotification(
"Index: "
+ tabs.getTabIndex(tabs.getTab(tabs
+ tabs.getTabPosition(tabs.getTab(tabs
.getSelectedTab())));
}

+ 95
- 0
tests/src/com/vaadin/tests/server/component/tabsheet/TestTabSheet.java View File

@@ -10,6 +10,7 @@ import org.junit.Test;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.Tab;
public class TestTabSheet {
@@ -26,4 +27,98 @@ public class TestTabSheet {
assertEquals(false, iter.hasNext());
assertNotNull(tabSheet.getTab(c));
}
@Test
public void getComponentFromTab() {
Component c = new Label("abc");
TabSheet tabSheet = new TabSheet();
Tab tab = tabSheet.addTab(c);
assertEquals(c, tab.getComponent());
}
@Test
public void addTabWithComponentOnly() {
TabSheet tabSheet = new TabSheet();
Tab tab1 = tabSheet.addTab(new Label("aaa"));
Tab tab2 = tabSheet.addTab(new Label("bbb"));
Tab tab3 = tabSheet.addTab(new Label("ccc"));
// Check right order of tabs
assertEquals(0, tabSheet.getTabPosition(tab1));
assertEquals(1, tabSheet.getTabPosition(tab2));
assertEquals(2, tabSheet.getTabPosition(tab3));
// Calling addTab with existing component does not move tab
tabSheet.addTab(tab1.getComponent());
// Check right order of tabs
assertEquals(0, tabSheet.getTabPosition(tab1));
assertEquals(1, tabSheet.getTabPosition(tab2));
assertEquals(2, tabSheet.getTabPosition(tab3));
}
@Test
public void addTabWithComponentAndIndex() {
TabSheet tabSheet = new TabSheet();
Tab tab1 = tabSheet.addTab(new Label("aaa"));
Tab tab2 = tabSheet.addTab(new Label("bbb"));
Tab tab3 = tabSheet.addTab(new Label("ccc"));
Tab tab4 = tabSheet.addTab(new Label("ddd"), 1);
Tab tab5 = tabSheet.addTab(new Label("eee"), 3);
assertEquals(0, tabSheet.getTabPosition(tab1));
assertEquals(1, tabSheet.getTabPosition(tab4));
assertEquals(2, tabSheet.getTabPosition(tab2));
assertEquals(3, tabSheet.getTabPosition(tab5));
assertEquals(4, tabSheet.getTabPosition(tab3));
// Calling addTab with existing component does not move tab
tabSheet.addTab(tab1.getComponent(), 3);
assertEquals(0, tabSheet.getTabPosition(tab1));
assertEquals(1, tabSheet.getTabPosition(tab4));
assertEquals(2, tabSheet.getTabPosition(tab2));
assertEquals(3, tabSheet.getTabPosition(tab5));
assertEquals(4, tabSheet.getTabPosition(tab3));
}
@Test
public void addTabWithAllParameters() {
TabSheet tabSheet = new TabSheet();
Tab tab1 = tabSheet.addTab(new Label("aaa"));
Tab tab2 = tabSheet.addTab(new Label("bbb"));
Tab tab3 = tabSheet.addTab(new Label("ccc"));
Tab tab4 = tabSheet.addTab(new Label("ddd"), "ddd", null, 1);
Tab tab5 = tabSheet.addTab(new Label("eee"), "eee", null, 3);
assertEquals(0, tabSheet.getTabPosition(tab1));
assertEquals(1, tabSheet.getTabPosition(tab4));
assertEquals(2, tabSheet.getTabPosition(tab2));
assertEquals(3, tabSheet.getTabPosition(tab5));
assertEquals(4, tabSheet.getTabPosition(tab3));
// Calling addTab with existing component does not move tab
tabSheet.addTab(tab1.getComponent(), "xxx", null, 3);
assertEquals(0, tabSheet.getTabPosition(tab1));
assertEquals(1, tabSheet.getTabPosition(tab4));
assertEquals(2, tabSheet.getTabPosition(tab2));
assertEquals(3, tabSheet.getTabPosition(tab5));
assertEquals(4, tabSheet.getTabPosition(tab3));
}
@Test
public void getTabByPosition() {
TabSheet tabSheet = new TabSheet();
Tab tab1 = tabSheet.addTab(new Label("aaa"));
Tab tab2 = tabSheet.addTab(new Label("bbb"));
Tab tab3 = tabSheet.addTab(new Label("ccc"));
assertEquals(tab1, tabSheet.getTab(0));
assertEquals(tab2, tabSheet.getTab(1));
assertEquals(tab3, tabSheet.getTab(2));
}
}

Loading…
Cancel
Save