February 9th, 2008 | |
Posted in Wicket
Having recently discovered the prototip javascript library and attended the London wicket user group meeting for February I decided to see if I could make a custom wicket component to add prototip tooltips.
Edit:
You can now get this as part of the wicketstuff-minis project here .
I created it as a custom wicket behaviour which could be added to any wicket component and it can use either a String or another wicket component as the tooltip content. It requires wicket 1.3 and Java 5 or 6 to work.
When the prototip jar file is on your classpath, and you have included the prototype and scriptaculous libraries on your page adding a prototip tooltip can be as simple as:
Label toolTargetWithString = new Label("toolTargetWithString", "I am a label which has a string tooltip");
add(toolTargetWithString);//this is the line which actually adds the prototip
toolTargetWithString.add(new PrototipBehaviour("I am a string tooltip"));
Or using a wicket component as the tooltip:
// Label with panel as a tooltip
Label toolTargetWithPanel = new Label("toolTargetWithPanel", "I am a label which has another wicket panel as a tooltip");
add(toolTargetWithPanel);//This is the panel which will be added as a tooltip
//It needs adding to the page, but does not need hiding
PanelAsTip tooltipPanel = null;
add(tooltipPanel = new PanelAsTip("toolTipPanel"));
//A panel tooltip tp add to the second label
PrototipBehaviour panelTip = new PrototipBehaviour(tooltipPanel);
toolTargetWithPanel.add(panelTip);
However prototip has a large number of optional settings. To add settings to a prototip first you need to create a PrototipSettings object, and set all of the required settings. Then you simply add the settings to the prototip behaviour. One PrototipSettings object can be added to any number of PrototipBehaviours which means that it is easy to make all of your tooltips behave in the same way.
For example:
PrototipSettings settings = new PrototipSettings();
settings.setEffect("blind").setDelay("0.2");
toolTargetWithString.add(new PrototipBehaviour("I am a string tooltip").setSettings(settings));
It is also possible to remove PrototipBehaviour via ajax, for example if you have an AjaxLink on your page:
add(new AjaxLink("remove"){
@Override
public void onClick(AjaxRequestTarget target)
{
//just call the remove method of the PrototipBehaviour passing in the AjaxRequestTarget
stringTip.remove(target);
}
});
It is even possible to add PrototipBehaviour via ajax
// Label with no initial tool tip
final Label toolTargetWithNoTooltip = new Label("toolTargetWithNoTooltip", "I am a label which starts out without any tooltips");
add(toolTargetWithNoTooltip);
//since this it to have a tooltip added via ajax it needs a markup id
toolTargetWithNoTooltip.setOutputMarkupId(true);
add(new AjaxLink("add"){
@Override
public void onClick(AjaxRequestTarget target)
{
//add the behaviour
toolTargetWithNoTooltip.add(new PrototipBehaviour("I was added via ajax!!"));
//add the label to the ajax request target
target.addComponent(toolTargetWithNoTooltip);
}
});
The creator of the prototip javascript code has given me permission to include the required javascript and css files in with my code which means that you don’t even have to download it or include it manually. However if you want to include the required files manually it is possible to make the PrototipBehaviour bypass the inclusion of the files eg:
PrototipBehaviour panelTip = new PrototipBehaviour(tooltipPanel);
panelTip.setOverrideHeaderContributor(true);
There are three different versions of the Javascript file, a normal one, a minified one and a minified gziped one. By default PrototipBehaviour includes the gziped one, however you can override this eg:
PrototipBehaviour panelTip = new PrototipBehaviour(tooltipPanel);
panelTip.setSelectedJsType(JS_TYPE.NORMAL);
Thats about it. I hope you find this useful. When I get my head around maven I will try and get this code working with it but until then you just need to include the Jar file on your classpath. I am happy to take any bug fixes or comments at richardjohnwilkinson (at) gmail (dot) com.
You can check out a real world example on the individual property pages on zoomf.com in a few weeks when we update them or for a not so real world example (by which does have source code) check out my test page which I have included.
Thanks again to Nick Stakenburg for creating prototip and I strongly recommend that you check out his website for more information about how to customise prototip and what all of the setting mean.
You can download the PrototipBehaviour and my example code from wickestuff here
Notes on licensing: The original Prototip javascript, css and image files are copyrighted to Nick Stakenburg, his licence is available at the top of the prototip.js file.