2.10 Listeners

 <  Day Day Up  >  

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 behalf of the portlet. Secondly, since the registration is taken care of by the Portal Server, it is not possible to specify which portlets a particular portlet wishes to register for. Therefore, portlets implementing listeners need to carefully plan for unsollicited and unexpected events.

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 remain mindful of the limited space in the title bar.

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 PortletPageListener

This 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

graphics/02fig13.jpg

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 illustrated in Figure 2-13.

The beginPage is a convenient method when you need to include Javascript functions needed by your portlet. However. be very conscious of any content you decide to display in the beginPage method as it may adversely affect the overall aggregation of the page. Furthermore, because the page is aggregated, be sure that any functions or global variables you declare have properly encoded the namespace of the portlet to ensure there are no naming collisions. Use the response.encodeNamespace to do this.

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 methods will not be called.


2.10.3 PortletSessionListener

This interface requests the Portal Server to notify the portlet if an authenticated user has accessed the portlet. This interface is already implemented by the PortletAdapter class which is traditionally the parent of most custom portlets. This interface defines the two methods shown in Example 2-22. Figure 2-10 on page 80 illustrates where in the life cycle of the portlet these methods are called. The functionality of the login and logout methods is detailed in 2.7, "Portlet life cycle" on page 80.

Example 2-22. PortletSessionListener methods
 public 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 counterpart since some development environments will offer both.


Example 2-23. Implementing the WindowListener
 public 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 altered and new ones can be added at runtime. The PortletSettingsAttributeListener notifies your portlet if the configuration parameters are changed at runtime.

2.10.6 PortletApplicationSettingsAttributesListener

Similar to the PortletSettingsAttributeListener, this listener provides notification when the context parameters of the concrete application have changed, been added or removed.

 <  Day Day Up  >  


IBM WebSphere Portal V5 A Guide for Portlet Application Development
IBM Websphere Portal V5: A Guide for Portlet Application Development
ISBN: 0738498513
EAN: 2147483647
Year: 2004
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net