The problem is that the tabbed panels have to know the state of other panels to 
generate correct stateful links. So we need some object that keeps track of all 
tabbed panels and their states. This object must be initialized before the 
newLink method is called. But this method is called during construction, so it 
can not know other tabbed panels when the first tabbed panel is constructed.

This object can be "pageParameters". The only problem is, that this object 
doesn't reflect the actual state in every case, because it is also used for url 
construction. The solution is to remove items from pageParameters after url 
construction if they don't reflect the state.

And here comes the solution. It is now possible to use multiple 
BookmarkableTabbedPanels within one page.



package com.helmbold.wicket.components;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.extensions.markup.html.tabs.ITab;
import org.apache.wicket.extensions.markup.html.tabs.TabbedPanel;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;


public class BookmarkableTabbedPanel extends TabbedPanel
{

    private PageParameters pageParameters;
    private String tabParameterName;
    private int defaultTabIndex = 0;
  private List unbookmarkableTabIndex = new ArrayList();


  /**
   * Using this constructor the following defaults take effect:
   * <ul>
   *    <li>tabParameterName = component id</li>
   *    <li>defaultTabIndex = 0</li>
   * </ul>
     * @param id component id
     * @param tabs list of ITab objects used to represent tabs
   * @param pageParameters Container for parameters to a requested page. A
   * parameter for the selected tab will be inserted.
     */
  public BookmarkableTabbedPanel(
          String id,
          List<ITab> tabs,
          PageParameters pageParameters)
  {
    super(id, tabs);
    this.pageParameters = pageParameters;
    this.tabParameterName = id;
    
    if (pageParameters.containsKey(tabParameterName))
    {
      String tab = pageParameters.getString(tabParameterName);
      try
      {
        setSelectedTab(Integer.parseInt(tab));
      }
      catch (NumberFormatException e)
      {
        setSelectedTab(defaultTabIndex);
      }
    }
    else
      setSelectedTab(defaultTabIndex);
  }


    /**
     * @param id component id
     * @param tabs list of ITab objects used to represent tabs
     * @param defaultTabIndex Set the tab to by displayed by default. The url
     * for this tab will not contain any tab specific information. If you want 
to 
     * display the first tab by default, you can use the constructor without 
this 
     * parameter.
   * @param pageParameters Container for parameters to a requested page. A
   * parameter for the selected tab will be inserted.
     */
    public BookmarkableTabbedPanel(
        String id, 
        List<ITab> tabs,
        int defaultTabIndex,
        String tabParameterName,
    PageParameters pageParameters,
    int ...unbookmarkableTabIndex) 
    {
        this(id, tabs, pageParameters);
        this.defaultTabIndex = defaultTabIndex;
        setSelectedTab(defaultTabIndex);
        this.tabParameterName = tabParameterName;
    for(int element : unbookmarkableTabIndex)
      this.unbookmarkableTabIndex.add(element);
    }

    
    @Override
  protected WebMarkupContainer newLink(String linkId, int index)
  {
    WebMarkupContainer link;

    // create default (not bookmarkable) links for the specified tabs.
    if (unbookmarkableTabIndex.contains(index))
      link = super.newLink(linkId, index);
    // create bookmarkable links
    else
    {
      if (index == defaultTabIndex)
        pageParameters.remove(tabParameterName);
      else
        pageParameters.put(tabParameterName, "" + index);
      link = new BookmarkablePageLink(
              linkId, getPage().getClass(), pageParameters);
      
      /* Overwrite tabIndexes only used for link cunstruction, but doesn't
       * reflect the actual state.
       */
      if (index != getSelectedTab())        
        pageParameters.put(tabParameterName, "" + getSelectedTab());
    }

    if (index == getSelectedTab())
      link.setEnabled(false);
    
    return link;
  }
  
}


Regards
Christian






---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to