The Java Server Faces generic "garble button" – how to become wildly popular again…

1

Somewhat akin to the dbms_advanced_rewrite package application I discussed some time ago, I have now another piece of functionality that will make you once again the light of the party. Introducing: the JSF Garble Button! This is a very simple CommandButton that you can inject into any page in any application. It only requires the configuration of a single managed bean. This powerful button – when pressed – will randomly reorganize the container it is part of – as well as all of its siblings’ child-components and their children etc. The value of this piece of functionality is absolutely nothing – beyond a pretty good joke if you dress up this button as a Help or Send Me Money button. Of course instead of a button you can use a commandlink.

A simple demonstration of the utter uselessness of what this article discusses: initially our grantedly already horrible page (for want of a proper stylesheet) looks like this:

After pressing the Garble button, the page is refreshed as:


....
 

All elements in the PanelGrid that is the parent (container ) for the Garble Button have been randomly reorganized, as well as all child-components’ children, such as the buttons in portlet three and the options in the list input.

The implementation of this Garble facility is simple, as expected. It has three parts

1) The button itself, with an ActionListener attribute bound to a managed bean

2) The class that provides the managed bean

3) The Managed Bean configuration itself in the faces-config.xml file.

Step one – The Garble Button itself

The button itself is just a plain old CommandButton with a properly set ActionListener:

<h:commandButton value="Garble" actionListener="#{garble.garble}" /> 

Step two – the Garble-class

The wizard that navigates through the Component Tree, reorganizing as it goes along:

package nl.amis.jsf.util;<br /><br />import java.util.ArrayList;<br />import java.util.Collections;<br />import java.util.List;<br /><br />import javax.faces.component.UIComponent;<br />import javax.faces.event.ActionEvent;<br /><br />public class GarbleAll {<br />    public GarbleAll() {<br />    }<br /><br />    private void shuffleList(List list) {<br />        List p2 = new ArrayList();<br />        p2.addAll(list);<br />        Collections.shuffle(p2);<br />        list.clear();<br />        list.addAll(p2);<br /><br />    }<br /><br /><br />    private void garbleChildren(List children) {<br />        shuffleList(children);<br />        for (UIComponent child:(List&lt;UIComponent&gt;)children) {<br />          List grandChildren = child.getChildren();<br />          if (grandChildren.size()&gt;0) {<br />              garbleChildren(grandChildren);<br />          }<br />        }<br />    }<br /><br />    /**<br />     * This method will take the parent of the activated component (likely a container) <br />     * and go through all of its children - randomly rearranging the children, the<br />     * children's children and all their descendant.<br />     * <br />     * @param actionEvent<br />     */<br />    public void garble(ActionEvent actionEvent) {<br />        // Add event code here...<br />        UIComponent parent = <br />            (UIComponent)actionEvent.getComponent().getParent();<br />        List children = parent.getChildren();<br />        garbleChildren(children);<br />        <br />    }<br /><br />}<br />&nbsp;

Step three – the Managed Bean configuration

Nothing fancy, very straightforward:

&lt;managed-bean&gt;<br />  &lt;managed-bean-name&gt;garble&lt;/managed-bean-name&gt;<br />  &lt;managed-bean-class&gt;nl.amis.jsf.util.GarbleAll&lt;/managed-bean-class&gt;<br />  &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;<br />&lt;/managed-bean&gt; <br />

And this really all it takes to have some fun on your project – in all the spare time that productive JSF development allows you.

Note: dressing up the Garble button as a Global Help button makes the effect even more interesting!

 

 

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

1 Comment

  1. Thanks Lucas.
    Alsways nice to see you describe some “utter uselessness”. :)
    regards
    Jan Vervecken