1 17 package org.alfresco.web.ui.common.component.data; 18 19 import java.io.IOException ; 20 import java.text.MessageFormat ; 21 import java.util.Map ; 22 import java.util.ResourceBundle ; 23 24 import javax.faces.component.NamingContainer; 25 import javax.faces.component.UICommand; 26 import javax.faces.component.UIComponent; 27 import javax.faces.context.FacesContext; 28 import javax.faces.context.ResponseWriter; 29 import javax.faces.event.AbortProcessingException; 30 import javax.faces.event.ActionEvent; 31 import javax.faces.event.FacesEvent; 32 33 import org.apache.log4j.Logger; 34 35 import org.alfresco.web.app.Application; 36 import org.alfresco.web.data.IDataContainer; 37 import org.alfresco.web.ui.common.Utils; 38 import org.alfresco.web.ui.common.WebResources; 39 40 43 public class UIDataPager extends UICommand 44 { 45 private static Logger s_logger = Logger.getLogger(IDataContainer.class); 46 47 private static final String LAST_PAGE = "last_page"; 48 private static final String NEXT_PAGE = "next_page"; 49 private static final String PREVIOUS_PAGE = "prev_page"; 50 private static final String FIRST_PAGE = "first_page"; 51 private static final String MSG_PAGEINFO = "page_info"; 52 53 54 57 60 public UIDataPager() 61 { 62 setRendererType(null); 63 } 64 65 66 69 72 public void encodeBegin(FacesContext context) throws IOException 73 { 74 IDataContainer dataContainer = getDataContainer(); 75 if (dataContainer == null) 76 { 77 throw new IllegalStateException ("Must nest UISortLink inside component implementing IDataContainer!"); 78 } 79 80 if (isRendered() == false || dataContainer.getPageSize() == -1) 83 { 84 return; 85 } 86 87 ResponseWriter out = context.getResponseWriter(); 88 ResourceBundle bundle = Application.getBundle(context); 89 StringBuilder buf = new StringBuilder (512); 90 91 int currentPage = dataContainer.getCurrentPage(); 92 int pageCount = dataContainer.getPageCount(); 93 94 buf.append("<span"); 95 if (getAttributes().get("style") != null) 96 { 97 buf.append(" style=\"") 98 .append(getAttributes().get("style")) 99 .append('"'); 100 } 101 if (getAttributes().get("styleClass") != null) 102 { 103 buf.append(" class=") 104 .append(getAttributes().get("styleClass")); 105 } 106 buf.append('>'); 107 108 buf.append(MessageFormat.format(bundle.getString(MSG_PAGEINFO), new Object [] { 110 Integer.toString(currentPage + 1), Integer.toString(pageCount) 112 })); 113 114 buf.append(" "); 115 116 if (currentPage != 0) 119 { 120 buf.append("<a HREF='#' onclick=\""); 121 buf.append(generateEventScript(0)); 122 buf.append("\">"); 123 buf.append(Utils.buildImageTag(context, WebResources.IMAGE_FIRSTPAGE, 16, 16, bundle.getString(FIRST_PAGE))); 124 buf.append("</a>"); 125 } 126 else 127 { 128 buf.append(Utils.buildImageTag(context, WebResources.IMAGE_FIRSTPAGE_NONE, 16, 16, null)); 129 } 130 131 if (currentPage != 0) 133 { 134 buf.append("<a HREF='#' onclick=\""); 135 buf.append(generateEventScript(currentPage - 1)); 136 buf.append("\">"); 137 buf.append(Utils.buildImageTag(context, WebResources.IMAGE_PREVIOUSPAGE, 16, 16, bundle.getString(PREVIOUS_PAGE))); 138 buf.append("</a>"); 139 } 140 else 141 { 142 buf.append(Utils.buildImageTag(context, WebResources.IMAGE_PREVIOUSPAGE_NONE, 16, 16, null)); 143 } 144 145 buf.append(" "); 146 147 int totalIndex = (pageCount < 10 ? pageCount : 10); 149 for (int i=0; i<totalIndex; i++) 150 { 151 if (i != currentPage) 152 { 153 buf.append("<a HREF='#' onclick=\"") 154 .append(generateEventScript(i)) 155 .append("\">") 156 .append(i + 1) 157 .append("</a> "); 158 } 159 else 160 { 161 buf.append("<b>") 162 .append(i + 1) 163 .append("</b> "); 164 } 165 } 166 if (pageCount >= 20) 168 { 169 buf.append("... "); 170 totalIndex = (pageCount / 10) * 10; 171 totalIndex = (totalIndex < 100 ? totalIndex : 100); 172 for (int i=19; i<totalIndex; i += 10) 173 { 174 if (i != currentPage) 175 { 176 buf.append("<a HREF='#' onclick=\"") 177 .append(generateEventScript(i)) 178 .append("\">") 179 .append(i + 1) 180 .append("</a> "); 181 } 182 else 183 { 184 buf.append("<b>") 185 .append(i + 1) 186 .append("</b> "); 187 } 188 } 189 } 190 if ((pageCount > 10) && (pageCount % 10 != 0)) 192 { 193 if (pageCount-1 != currentPage) 194 { 195 if (pageCount < 20) 196 { 197 buf.append("... "); 198 } 199 buf.append("<a HREF='#' onclick=\"") 200 .append(generateEventScript(pageCount-1)) 201 .append("\">") 202 .append(pageCount) 203 .append("</a> "); 204 } 205 else 206 { 207 if (pageCount < 20) 208 { 209 buf.append("... "); 210 } 211 buf.append("<b>") 212 .append(pageCount) 213 .append("</b> "); 214 } 215 } 216 217 if ((dataContainer.getCurrentPage() < dataContainer.getPageCount() - 1) == true) 219 { 220 buf.append("<a HREF='#' onclick=\""); 221 buf.append(generateEventScript(currentPage + 1)); 222 buf.append("\">"); 223 buf.append(Utils.buildImageTag(context, WebResources.IMAGE_NEXTPAGE, 16, 16, bundle.getString(NEXT_PAGE))); 224 buf.append("</a>"); 225 } 226 else 227 { 228 buf.append(Utils.buildImageTag(context, WebResources.IMAGE_NEXTPAGE_NONE, 16, 16, null)); 229 } 230 231 if ((dataContainer.getCurrentPage() < dataContainer.getPageCount() - 1) == true) 233 { 234 buf.append("<a HREF='#' onclick=\""); 235 buf.append(generateEventScript(dataContainer.getPageCount() - 1)); 236 buf.append("\">"); 237 buf.append(Utils.buildImageTag(context, WebResources.IMAGE_LASTPAGE, 16, 16, bundle.getString(LAST_PAGE))); 238 buf.append("</a>"); 239 } 240 else 241 { 242 buf.append(Utils.buildImageTag(context, WebResources.IMAGE_LASTPAGE_NONE, 16, 16, null)); 243 } 244 245 buf.append("</span>"); 246 247 out.write(buf.toString()); 248 } 249 250 253 public void decode(FacesContext context) 254 { 255 Map requestMap = context.getExternalContext().getRequestParameterMap(); 256 String fieldId = getHiddenFieldName(); 257 String value = (String )requestMap.get(fieldId); 258 if (value != null && value.length() != 0) 259 { 260 PageEvent actionEvent = new PageEvent(this, Integer.valueOf(value).intValue()); 264 this.queueEvent(actionEvent); 265 } 266 } 267 268 271 public void broadcast(FacesEvent event) throws AbortProcessingException 272 { 273 if (event instanceof PageEvent == false) 274 { 275 super.broadcast(event); 277 } 278 else 279 { 280 if (s_logger.isDebugEnabled()) 282 s_logger.debug("Handling paging event to index: " + ((PageEvent)event).Page); 283 getDataContainer().setCurrentPage(((PageEvent)event).Page); 284 } 285 } 286 287 288 291 294 private IDataContainer getDataContainer() 295 { 296 return Utils.getParentDataContainer(getFacesContext(), this); 297 } 298 299 304 private String generateEventScript(int page) 305 { 306 return Utils.generateFormSubmit(getFacesContext(), this, getHiddenFieldName(), Integer.toString(page)); 307 } 308 309 316 private String getHiddenFieldName() 317 { 318 UIComponent dataContainer = (UIComponent)Utils.getParentDataContainer(getFacesContext(), this); 319 return dataContainer.getClientId(getFacesContext()) + NamingContainer.SEPARATOR_CHAR + "pager"; 320 } 321 322 323 326 329 private static class PageEvent extends ActionEvent 330 { 331 public PageEvent(UIComponent component, int page) 332 { 333 super(component); 334 Page = page; 335 } 336 337 public int Page = 0; 338 } 339 } 340 | Popular Tags |