KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > sort > JavascriptSortUtil


1 package fr.improve.struts.taglib.layout.sort;
2
3 import java.util.Date JavaDoc;
4
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6
7 /**
8  * This class contains useful methods to deal with javascript collection sorting on the client.
9  *
10  * @author JN Ribette
11  *
12  */

13 public class JavascriptSortUtil {
14     private static final String JavaDoc ID_KEY = "fr.improve.struts.taglib.layout.sort.JavascriptSortUtil.ID_KEY";
15     /**
16      * Make it possible to sort numbers and date by prefixing those objects by an hidden string
17      * which will be sortable by the browser.
18      */

19     public static final String JavaDoc makeSortable(Object JavaDoc in_object) {
20         if (in_object==null) {
21             return null;
22         } else if (in_object instanceof Number JavaDoc) {
23             double lc_double = ((Number JavaDoc) in_object).doubleValue();
24             StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc(20);
25             lc_buffer.append("<!-- ");
26             lc_buffer.append(Double.doubleToLongBits(lc_double));
27             lc_buffer.append(" -->");
28             return lc_buffer.toString();
29         } else if (in_object instanceof Date JavaDoc) {
30             long lc_long = ((Date JavaDoc)in_object).getTime();
31             StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc(20);
32             lc_buffer.append("<!-- ");
33             lc_buffer.append(Long.toHexString(lc_long));
34             lc_buffer.append(" -->");
35             return lc_buffer.toString();
36         } else {
37             return null;
38         }
39     }
40     /**
41      * Returns a unique id for the collection. This allows to different several collections in a jsp.
42      */

43     public static String JavaDoc getId(HttpServletRequest JavaDoc in_request) {
44         Integer JavaDoc lc_oldId = (Integer JavaDoc) in_request.getSession().getAttribute(ID_KEY);
45         if (lc_oldId==null) {
46             lc_oldId = new Integer JavaDoc(0);
47         }
48         int lc_int = lc_oldId.intValue();
49         if (lc_int == 100) {
50             lc_int = 0;
51         }
52         Integer JavaDoc lc_newId = new Integer JavaDoc(lc_int + 1);
53         in_request.getSession().setAttribute(ID_KEY, lc_newId);
54         return lc_newId.toString();
55     }
56     
57     /**
58      * Returns the value that will be returned by the next call to getId().
59      */

60     public static String JavaDoc predictNextId(HttpServletRequest JavaDoc in_request) {
61         Integer JavaDoc lc_oldId = (Integer JavaDoc) in_request.getSession().getAttribute(ID_KEY);
62         if (lc_oldId==null) {
63             lc_oldId = new Integer JavaDoc(0);
64         }
65         int lc_int = lc_oldId.intValue();
66         if (lc_int == 100) {
67             lc_int = 0;
68         }
69         Integer JavaDoc lc_newId = new Integer JavaDoc(lc_int + 1);
70         return lc_newId.toString();
71     }
72 }
73
Popular Tags