KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > sqltable > SqlTableSortButtonTag


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.sqltable;
14
15 import java.io.IOException JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
17 import java.util.MissingResourceException JavaDoc;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.jsp.JspException JavaDoc;
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 /**
30  * @author av
31  * @since Oct 6, 2004
32  */

33 public class SqlTableSortButtonTag extends SqlTableNestedTag {
34   String JavaDoc column;
35   private static final Logger logger = Logger.getLogger(SqlTableSortButtonTag.class);
36
37   static class SortButtonHandler implements RequestListener {
38     private String JavaDoc column;
39     private boolean descending;
40     private SqlTable table;
41
42     SortButtonHandler(SqlTable table, String JavaDoc 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 JavaDoc {
49       Scroller.enableScroller(context);
50       table.setOrderBy(column);
51       table.setDescending(descending);
52     }
53   }
54
55   public int doStartTag() throws JspException JavaDoc {
56     try {
57       if (!isRenderActions())
58         return SKIP_BODY;
59       
60       SqlTable table = getTable();
61       if (table.getOrderBy() == null)
62         throw new JspException JavaDoc("sqlTable must have the orderBy attribute set to use sort buttons");
63       RequestListener rl = getSortButtonHandler(table);
64       String JavaDoc name = table.nextId();
65       table.addRequestListener(name, null, rl);
66
67       String JavaDoc imgSrc = getSortButtonImageSource(table);
68       Object JavaDoc[] args = { name, imgSrc};
69       Resources res = getResources();
70       String JavaDoc imgTag = res.getString("sqltable.sort.imgtag", args);
71
72       pageContext.getOut().print(imgTag);
73     } catch (UnsupportedEncodingException JavaDoc e) {
74       logger.error(null, e);
75     } catch (MissingResourceException JavaDoc e) {
76       logger.error(null, e);
77     } catch (IOException JavaDoc e) {
78       logger.error(null, e);
79     }
80
81     return SKIP_BODY;
82   }
83
84   private RequestListener getSortButtonHandler(SqlTable table) {
85     String JavaDoc currentColumn = table.getOrderBy();
86     // click on the current column changes sort direction
87
if (currentColumn.equals(column))
88       return new SortButtonHandler(table, column, !table.isDescending());
89     // click on other column does not change direction
90
return new SortButtonHandler(table, column, table.isDescending());
91   }
92
93   private String JavaDoc getSortButtonImageSource(SqlTable table) throws JspException JavaDoc {
94     Resources res = getResources();
95     String JavaDoc contextPath = ((HttpServletRequest JavaDoc) pageContext.getRequest()).getContextPath();
96     String JavaDoc currentColumn = table.getOrderBy();
97     if (currentColumn == null)
98       throw new JspException JavaDoc("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 JavaDoc column) {
110     this.column = column;
111   }
112
113 }
114
Popular Tags