/**
 * @author smallufo 
 * Created on 2009/2/5 
 */ 
package quickstart;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.border.Border;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;

public class PulldownBorder extends Border
{
  private final WebMarkupContainer content;
  
  private boolean expanded;
  
  
  public PulldownBorder(String id , String title , boolean defaultExpanded)
  {
    super(id);
    
    setOutputMarkupPlaceholderTag(true);
    setRenderBodyOnly(false);
    
    Label titleLabel = new Label("title" , title );
    expanded = defaultExpanded;
    
    content = new WebMarkupContainer("content");
    
    content.setOutputMarkupPlaceholderTag(true);
    content.setVisible(defaultExpanded);
    content.setRenderBodyOnly(false);
    add(content);
    content.add(getBodyContainer());
    
    final Label collapseExpandText = new Label("collapseExpandText");
    collapseExpandText.setOutputMarkupPlaceholderTag(true);
    if (content.isVisible())
      collapseExpandText.setModel(new Model("collapse"));
    else
      collapseExpandText.setModel(new Model("expand"));
   
    Link collapseExpandLink = new AjaxFallbackLink("collapseExpandLink")
    {
      @Override
      public void onClick(AjaxRequestTarget target)
      {
        expanded=!expanded;
        content.setVisible(expanded);
        target.addComponent(content);
        
        MarkupContainer mc = findParent(ExpandCollapseListener.class);
        if (mc != null)
        {
          if (mc instanceof ExpandCollapseListener)
          {
            ExpandCollapseListener listener = (ExpandCollapseListener) mc;
            listener.setExpanded(expanded , target);
          }
        }
          
        if (!expanded)
        {
          // visible to invisible
          target.addComponent(collapseExpandText.setModel(new Model("expand")));
        }
        else
        {
          // invisible to visible
          target.addComponent(collapseExpandText.setModel(new Model("collapse")));
        }
      }
    };
    add(collapseExpandLink);
    collapseExpandLink.add(collapseExpandText);
    collapseExpandLink.add(titleLabel);
  }

}
