1 13 package com.tonbeller.wcf.sqltable; 14 15 import java.io.IOException ; 16 import java.io.UnsupportedEncodingException ; 17 import java.util.MissingResourceException ; 18 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.jsp.JspException ; 21 22 import org.apache.log4j.Logger; 23 24 import com.tonbeller.tbutils.res.Resources; 25 import com.tonbeller.wcf.controller.RequestContext; 26 import com.tonbeller.wcf.controller.RequestListener; 27 import com.tonbeller.wcf.scroller.Scroller; 28 29 33 public class SqlTableSortButtonTag extends SqlTableNestedTag { 34 String column; 35 private static final Logger logger = Logger.getLogger(SqlTableSortButtonTag.class); 36 37 static class SortButtonHandler implements RequestListener { 38 private String column; 39 private boolean descending; 40 private SqlTable table; 41 42 SortButtonHandler(SqlTable table, String column, boolean descending) { 43 this.table = table; 44 this.column = column; 45 this.descending = descending; 46 } 47 48 public void request(RequestContext context) throws Exception { 49 Scroller.enableScroller(context); 50 table.setOrderBy(column); 51 table.setDescending(descending); 52 } 53 } 54 55 public int doStartTag() throws JspException { 56 try { 57 if (!isRenderActions()) 58 return SKIP_BODY; 59 60 SqlTable table = getTable(); 61 if (table.getOrderBy() == null) 62 throw new JspException ("sqlTable must have the orderBy attribute set to use sort buttons"); 63 RequestListener rl = getSortButtonHandler(table); 64 String name = table.nextId(); 65 table.addRequestListener(name, null, rl); 66 67 String imgSrc = getSortButtonImageSource(table); 68 Object [] args = { name, imgSrc}; 69 Resources res = getResources(); 70 String imgTag = res.getString("sqltable.sort.imgtag", args); 71 72 pageContext.getOut().print(imgTag); 73 } catch (UnsupportedEncodingException e) { 74 logger.error(null, e); 75 } catch (MissingResourceException e) { 76 logger.error(null, e); 77 } catch (IOException e) { 78 logger.error(null, e); 79 } 80 81 return SKIP_BODY; 82 } 83 84 private RequestListener getSortButtonHandler(SqlTable table) { 85 String currentColumn = table.getOrderBy(); 86 if (currentColumn.equals(column)) 88 return new SortButtonHandler(table, column, !table.isDescending()); 89 return new SortButtonHandler(table, column, table.isDescending()); 91 } 92 93 private String getSortButtonImageSource(SqlTable table) throws JspException { 94 Resources res = getResources(); 95 String contextPath = ((HttpServletRequest ) pageContext.getRequest()).getContextPath(); 96 String currentColumn = table.getOrderBy(); 97 if (currentColumn == null) 98 throw new JspException ("sqlTable must have the orderBy attribute set to use sort buttons"); 99 if (currentColumn.equals(column)) { 100 if (table.isDescending()) 101 return res.getString("sqltable.sortimg.cur.desc", contextPath); 102 return res.getString("sqltable.sortimg.cur.asc", contextPath); 103 } 104 if (table.isDescending()) 105 return res.getString("sqltable.sortimg.other.desc", contextPath); 106 return res.getString("sqltable.sortimg.other.asc", contextPath); 107 } 108 109 public void setColumn(String column) { 110 this.column = column; 111 } 112 113 } 114 | Popular Tags |