Skip to content

Instantly share code, notes, and snippets.

@jonnywray
Created September 23, 2010 22:12
Show Gist options
  • Save jonnywray/594489 to your computer and use it in GitHub Desktop.
Save jonnywray/594489 to your computer and use it in GitHub Desktop.
Wicket CollapsiblePanel: allows the show and hide of an inner panel while keeping a header and border
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
/**
* Wicket panel that can be opened and closed
*
* @author Jonny Wray
*/
public abstract class CollapsiblePanel extends Panel {
private ResourceReference closed = new ResourceReference(CollapsiblePanel.class, "bullet_toggle_plus.png");
private ResourceReference open = new ResourceReference(CollapsiblePanel.class, "bullet_toggle_minus.png");
private boolean visible = false;
protected Panel innerPanel;
/**
* Construct the panel
*
* @param id Panel ID
* @param titleModel Model used to get the panel title
* @param defaultOpen Is the default state open
*/
public CollapsiblePanel(String id, IModel<String> titleModel, boolean defaultOpen){
super(id);
this.visible = defaultOpen;
innerPanel = getInnerPanel("innerPanel");
innerPanel.setVisible(visible);
innerPanel.setOutputMarkupId(true);
innerPanel.setOutputMarkupPlaceholderTag(true);
add(innerPanel);
final Image showHideImage = new Image("showHideIcon"){
private static final long serialVersionUID = 8638737301579767296L;
@Override
public ResourceReference getImageResourceReference(){
return visible ? open : closed;
}
};
showHideImage.setOutputMarkupId(true);
IndicatingAjaxLink showHideLink = new IndicatingAjaxLink("showHideLink"){
private static final long serialVersionUID = -1929927616508773911L;
@Override
public void onClick(AjaxRequestTarget target) {
visible = !visible;
innerPanel.setVisible(visible);
target.addComponent(innerPanel);
target.addComponent(showHideImage);
}
};
showHideLink.add(showHideImage);
add(new Label("titlePanel", titleModel));
add(showHideLink);
}
/**
* Construct the panel contained within the collapsible panel
*
* @param markupId ID that should be used for the inner panel
* @return The inner panel displayed when collapsible is open
*/
protected abstract Panel getInnerPanel(String markupId);
}
<!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" xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.3-strict.dtd" xml:lang="en" lang="en">
<wicket:panel>
<fieldset class="collapsiblePanel">
<legend><h3><a href="#" wicket:id="showHideLink"><img wicket:id="showHideIcon" align="absmiddle" border="0"/></a> <span wicket:id="titlePanel"></span></h3></legend>
<div>
<span wicket:id="innerPanel" >[sequence]</span>
</div>
</fieldset>
</wicket:panel>
</html>
.collapsiblePanel{
border: thin;
border-color: #eee;
border-style: solid;
margin-bottom:5px;
}
@avchavan
Copy link

Hi, How to use this?
Can you give an example.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment