View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.wcm.webui.paginator;
18  
19  import org.exoplatform.commons.exception.ExoMessageException;
20  import org.exoplatform.resolver.ResourceResolver;
21  import org.exoplatform.services.log.ExoLogger;
22  import org.exoplatform.services.log.Log;
23  import org.exoplatform.webui.application.WebuiRequestContext;
24  import org.exoplatform.webui.config.annotation.ComponentConfig;
25  import org.exoplatform.webui.config.annotation.EventConfig;
26  import org.exoplatform.webui.core.UIComponent;
27  import org.exoplatform.webui.core.UIPageIterator;
28  import org.exoplatform.webui.core.lifecycle.Lifecycle;
29  import org.exoplatform.webui.event.Event;
30  import org.exoplatform.webui.event.EventListener;
31  
32  /**
33   * Created by The eXo Platform SAS
34   * Author : Hoa Pham
35   * hoa.phamvu@exoplatform.com
36   * Oct 23, 2008
37   */
38  @ComponentConfig(
39      lifecycle = Lifecycle.class,
40      events = @EventConfig(listeners = UICustomizeablePaginator.ShowPageActionListener.class )
41  )
42  public class UICustomizeablePaginator extends UIPageIterator {
43  
44    private static final Log LOG = ExoLogger.getLogger(UICustomizeablePaginator.class.getName());
45    
46    /** The template path. */
47    private String templatePath;
48  
49    /** The resource resolver. */
50    private ResourceResolver resourceResolver;
51  
52    /** Page Mode */
53    private String pageMode;
54  
55    /**
56     * Instantiates a new uI customizeable paginator.
57     */
58    public UICustomizeablePaginator() {
59    }
60  
61    /**
62     * Gets the total pages.
63     *
64     * @return the total pages
65     */
66    public int getTotalPages() { return getPageList().getAvailablePage(); }
67  
68    /**
69     * Gets the total items.
70     *
71     * @return the total items
72     */
73    public int getTotalItems() { return getPageList().getAvailable(); }
74  
75    /**
76     * Gets the item per page.
77     *
78     * @return the item per page
79     */
80    public int getItemPerPage() { return getPageList().getPageSize(); }
81  
82    /**
83     * Inits the.
84     *
85     * @param resourceResolver the resource resolver
86     * @param templatePath the template path
87     */
88    public void init(ResourceResolver resourceResolver, String templatePath) {
89      this.resourceResolver = resourceResolver;
90      this.templatePath = templatePath;
91    }
92  
93    /**
94     * Sets the template path.
95     *
96     * @param path the new template path
97     */
98    public void setTemplatePath(String path) { this.templatePath = path; }
99  
100   /**
101    * Sets the resource resolver.
102    *
103    * @param resolver the new resource resolver
104    */
105   public void setResourceResolver(ResourceResolver resolver) { this.resourceResolver = resolver; }
106 
107   /* (non-Javadoc)
108    * @see org.exoplatform.webui.core.UIComponent#getTemplate()
109    */
110   public String getTemplate() {
111     if(templatePath != null)
112       return templatePath;
113     return super.getTemplate();
114   }
115 
116   /**
117    * gets the page mode (none, more or pagination)
118    * @return PageMode
119    */
120   public String getPageMode() {
121     return pageMode;
122   }
123 
124   /**
125    * sets the page mode (none, more or pagination)
126    *
127    * @param pageMode
128    */
129   public void setPageMode(String pageMode) {
130     this.pageMode = pageMode;
131   }
132   
133   /*
134    * (non-Javadoc)
135    * @see
136    * org.exoplatform.webui.core.UIComponent#getTemplateResourceResolver(org.
137    * exoplatform.webui.application.WebuiRequestContext, java.lang.String)
138    */
139   public ResourceResolver getTemplateResourceResolver(WebuiRequestContext context,String template) {
140     if(resourceResolver != null)
141       return resourceResolver;
142     return super.getTemplateResourceResolver(context,template);
143   }
144 
145   /**
146    * The listener interface for receiving showPageAction events.
147    * The class that is interested in processing a showPageAction
148    * event implements this interface, and the object created
149    * with that class is registered with a component using the
150    * component's <code>addShowPageActionListener</code> method. When
151    * the showPageAction event occurs, that object's appropriate
152    * method is invoked.
153    *
154    */
155   static  public class ShowPageActionListener extends EventListener<UICustomizeablePaginator> {
156 
157     /* (non-Javadoc)
158      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
159      */
160     public void execute(Event<UICustomizeablePaginator> event) throws Exception {
161       UICustomizeablePaginator uiPaginator = event.getSource() ;
162       int page = Integer.parseInt(event.getRequestContext().getRequestParameter(OBJECTID)) ;
163       try {
164         uiPaginator.setCurrentPage(page) ;
165       } catch (ExoMessageException e) {
166         if (LOG.isWarnEnabled()) {
167           LOG.warn(e.getMessage());
168         }
169       }
170       UIComponent parent = uiPaginator.getParent();
171       if(parent == null) return ;
172       event.getRequestContext().addUIComponentToUpdateByAjax(parent);
173       parent.broadcast(event,event.getExecutionPhase());
174     }
175   }
176 }