the $time variable to the panel model panel.getModel().put("time", time); addControl(panel); } } The SimplePanelDemo template, /simple-panel-demo.htm, would reference the panel control:
$panel
The Panel template would then be merged with the Panel model and rendered in the page as:
Time time is now Sun Mar 15 07:32:51 EST 2009
Example 2 - Localization support
In this example, we demonstrate localization support by specifying the Panel content in the SimplePanelDemo.properties file. Since the Panel model and Page model are merged at runtime, the Panel template can access the Page messages. First we create the SimplePanelDemo.properties file which specifies two properties: heading and content.
heading=Welcome content=Welcome to MyCorp<p/>MyCorp is your telecommuting office portal. Its just like being there at the office!
Then we create the /panel/simple-panel.htm that references the localized Page properties. Since a Page properties are made available through the $messages map, the Panel can access the Page properties using the variables $messages.header and $messages.content:
In our page class, SimplePageDemo, we create and add the Panel instance:
public class SimplePanelDemo extends Page { public Panel panel = new Panel("panel", "/panel/simple-panel.htm"); }
In the Page above we make use of Click's autobinding feature by declaring a public Panel field. Autobinding will automatically add the Panel to the Page model. The SimplePanelDemo template, /simple-panel-demo.htm, would reference the panel control:
$panel
And the result is:
Example 3 - Reusing and Nesting Panels
Panels provide a good way to create reusable components, and since Panel is a Container it can hold child controls, even other Panels. In this example we create a reusable CustomerPanel which is added to a Border Panel. First we create the /panel/customer-panel.htm template which references the $form variable:
$form
Next up is the CustomerPanel:
public class CustomerPanel extends Panel { private Form form = new Form("form"); public CustomerPanel(String name) { super(name); // We explicitly set the customer panel template setTemplate("/panel/customer-panel.htm"); form.add(new TextField("name"); form.add(new DateField("dateJoined"); form.add(new DoubleField("holdings"); } }
The Border Panel template, /panel/border-panel.htm, will draw a Border around its contents:
Lastly we specify the NestedDemo Page, that creates a Border Panel, and adds CustomerPanel as a child.
public class NestedDemo extends Page { private Panel borderPanel = new Panel("borderPanel", "/panel/border-panel.htm"); private CustomerPanel customerPanel = new CustomerPanel("panel"); public void onInit() { // Add CustomerPanel to the Border panel parentPanel.add(childPanel); // Add border panel to page addControl(parentPanel); } }
The Page template, /nested-demo.htm, would reference the $borderPanel variable:
$borderPanel
Template Model
To render the panel's template, a model is created ( {@link #createTemplateModel()}) which is merged with the template. This model will include the page model values, plus any Panel defined model values, with the Panel's values overriding the Page defined values. In addition a number of predefined values are automatically added to the model. These values include:
attributes - the panel HTML attributes map
context - the Servlet context path, e.g. /mycorp
format - the page {@link Format} object for formatting the display of objects
this - a reference to this panel object
messages - the panel messages bundle
request - the servlet request
response - the servlet request
session - the {@link SessionMap} adaptor for the users HttpSession
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.