| < Day Day Up > |
2.10 Listeners
The event model of the Portal API is very similar to the traditional Java event model. However, there are two main points of distinction. First, there is no need to register listeners. When a portlet in installed, the Portal Server determines the listeners it implements and registers them on
There are several listeners defined in the Portal API. The ActionListener is covered in the Event handling section and the MessageListener is covered in the Messaging section. 2.10.1 PortletTitleListener
This listener allows you to dynamically set the title of the portlet. This listener requires the single method as shown in Example 2-20. This interface is particularly useful when tailoring the title to certain modes or devices. To return a title, simply use a PrintWriter object or include a JSP using the PortletContext object. While the second approach allows you to create a more dynamic title including images and so forth, you must
Example 2-20. PortletTitleListener example
public void
doTitle
(PortletRequest request, PortletResponse response)
throws PortletException, IOException {
PrintWriter out = response.getWriter();
String title = getPortletSettings().getTitle(
request.getLocale(), request.getClient());
out.print(title + "(" + request.getMode() + ")");
}
2.10.2 PortletPageListenerThis interface provides the opportunity to add content to the top and bottom of the aggregated page. Example 2-21 on page 96 illustrates a simple implementation of the PortletPageListener interface. It is important to note that content returned from the beginPage method is not placed at the top of the page but rather at the top of the aggregated content as displayed in Figure 2-13. Figure 2-13. beginPage and endPage placements
Example 2-21. PortletPageListener implementation
public class AgendaPortlet extends PortletAdapter implements
PortletPageListener {
........
public void beginPage(PortletRequest request, PortletResponse response)
throws PortletException, IOException {
PrintWriter out = response.getWriter();
out.println("This page contains my agenda.");
}
public void endPage(PortletRequest request, PortletResponse response)
throws PortletException, IOException {
PrintWriter out = response.getWriter();
out.println("End of my agenda.");
}
}
The resulting page including the top and bottom messages is
The beginPage is a
Restriction
The Home.jsp can choose to cancel calls to the PortletPageListener via the
<wps:pageRender includeBeginPage="no" includeEndPage="no">
tag. In this case, your beginPage and endPage
2.10.3 PortletSessionListener
This interface
Example 2-22. PortletSessionListener methodspublic void login (PortletRequest request) throws PortletException{ ... } public void logout (PortletSession session) throws PortletException{ ... } 2.10.4 WindowListener
Note The WindowEvent interface is deprecated; you should use the PortletWindow.getWindowState() method instead. It is included here for information and as a reference for portlets developed using previous releases. This interface will notify the portlet that the user has changed the window state. Presently, there are only three supported window states, despite the javadoc. NORMAL, MAXIMIZED and MINIMIZED states are supported. The portlet is notified of these three states through windowMaximized, windowMinimized, and windowRestored, respectively. Though only three states are currently supported, the WindowListener defines methods for windowClosing, windowOpening, windowDetached and windowClosed. This methods are never called. However, in order to implement this interface, all methods must be implemented even though several will contain empty bodies.
Note
You will need to make sure you implement the interface org.apache.jetspeed.portlet.event.WindowListener and not the AWT
Example 2-23. Implementing the WindowListenerpublic void windowMaximized (WindowEvent arg0) throws PortletException { // Some action can be performed } public void windowMinimized (WindowEvent arg0) throws PortletException { // Some action can be performed } public void windowRestored (WindowEvent arg0) throws PortletException { // Some action can be performed } public void windowClosing (WindowEvent arg0) throws PortletException { } public void windowClosed (WindowEvent arg0) throws PortletException { } public void windowDetached (WindowEvent arg0) throws PortletException { } 2.10.5 PortletSettingsAttributeListener
The PortletSettings object encapsulates the concrete portlet defined in the portlet.xml. Part of that definition includes configuration parameters that may be declared at deployment time. These parameters can be
2.10.6 PortletApplicationSettingsAttributesListenerSimilar to the PortletSettingsAttributeListener, this listener provides notification when the context parameters of the concrete application have changed, been added or removed. |
| < Day Day Up > |