tags:

views:

54

answers:

2

I have a tab panel where I add tabs dynamically.

At a given point it can look like:

tabPanel.add(new HTML("Dashboard"), new Hyperlink("Dashboard", "dashboard"));       
tabPanel.add(new CashGameTabPage(this), new Hyperlink("Cash Games", "cash"));
tabPanel.add(new TournamentTabPage(this), new Hyperlink("Tournament", "tournament"));

I would like to check if a Tab already exists. If it exists, I should get its index. If it does not exist I should get 0. I was thinking as a function such as:

public static int getIndexIfAlreadyExists(DecoratedTabPanel tabPanel, String title) {
    int tabcount = tabPanel.getTabBar().getTabCount();      
    for(int i = 0; i < tabcount;i++) {              
       if(/*TODO get a Tab Text */.equals(title))
          return i;
    return 0;
}

I would like to get

getIndexIfAlreadyExists(tabPanel, "Dashboard") -> 0
getIndexIfAlreadyExists(tabPanel, "Cash Games") -> 1
getIndexIfAlreadyExists(tabPanel, "Tournament") -> 2

However I do not manage to find a method to retrieve the Text. Any idea how to achieve this behaviour.

Thanks in advanced.

+1  A: 

TabPanel doesn't really make this easy. It may be possible to get the Hyperlink widget somehow via TabPanel.getTabBar().getTab(index), but I'm not sure.

This is approximately what I would do:

public class TabPanelModel {

    private final List<String> titleList = new ArrayList<String>();
    private final TabPanel tabPanel;

    public TabPanelModel(final TabPanel tabPanel) {
        this.tabPanel = tabPanel;
    }

    public void addToPanel(Widget widget, final String linktext, final String targetHistoryToken) {
        tabPanel.add(widget, new Hyperlink(linktext, targetHistoryToken));
        titleList.add(linktext);
    }

    /* similar for remove, ... */

    public int getIndexIfAlreadyExists(final String linktext) {
        return titleList.indexOf(linktext) + 1;
    }

}

The approach uses a small amount of additional memory, but I believe that it's better to have such information in a clean model anyway.

Chris Lercher
+1  A: 

I think you might be looking for TabBar.getTabHTML(int index).

However, I typically do something like Chris suggested, but create a data object for the tabs themselves

public interface Tab {
  String getName(); // or Widget
  Widget getContent();
}

and add tabs via a wrapper on TabPanel:

List<Tab> tabs;

public void addTab(Tab tab) {
  tabs.add(tab);
  tabPanel.add(tab.getContent(), tab.getName);
}

public Tab getTab(int i) {...}

Also, if you don't use the new LayoutPanel classes, you should give them a look.

spankalee