View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbSortTag.java,v 1.20 2006/01/13 13:38:51 hkollmann Exp $
3    * $Revision: 1.20 $
4    * $Date: 2006/01/13 13:38:51 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.taglib;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import org.dbforms.util.*;
30  
31  import java.util.*;
32  
33  import javax.servlet.http.*;
34  import javax.servlet.jsp.*;
35  
36  
37  
38  /***
39   * this tag renders a dabase-datadriven LABEL, which is apassive element (it
40   * can't be changed by the user) - it is predestinated for use with read-only
41   * data (i.e. primary keys you don't want the user to change, etc)
42   *
43   * @author Joachim Peer
44   */
45  public class DbSortTag extends AbstractDbBaseHandlerTag
46     implements javax.servlet.jsp.tagext.TryCatchFinally {
47     private static Log logCat = LogFactory.getLog(DbSortTag.class); // logging category for this class
48  
49     /***
50      * DOCUMENT ME!
51      *
52      * @return DOCUMENT ME!
53      *
54      * @throws javax.servlet.jsp.JspException DOCUMENT ME!
55      * @throws JspException DOCUMENT ME!
56      */
57     public int doEndTag() throws javax.servlet.jsp.JspException {
58        try {
59           if (!getField()
60                         .hasIsKeySet() && !getField()
61                                                     .hasSortableSet()) {
62              logCat.warn("you should declare " + getField().getName()
63                          + " as key or as sortable in your config file, if you use it as ordering field!");
64           }
65  
66           HttpServletRequest request = (HttpServletRequest) pageContext
67                                        .getRequest();
68  
69           String             oldValue = ParseUtil.getParameter(request,
70                                                                getField().getSortFieldName());
71  
72           StringBuffer tagBuf = new StringBuffer();
73           tagBuf.append("<select name=\"");
74           tagBuf.append(getField().getSortFieldName());
75           tagBuf.append("\" size=\"0\" onChange=\"javascript:document.dbform.submit()\" >");
76  
77           String strAsc  = "Ascending";
78           String strDesc = "Descending";
79           String strNone = "None";
80  
81           // Internationalization			
82           if (getParentForm()
83                        .hasCaptionResourceSet()) {
84              Locale reqLocale = MessageResources.getLocale(request);
85  
86              // get message resource or if null take the default (english)
87              strAsc = MessageResources.getMessage("dbforms.select.sort.ascending",
88                                                   reqLocale, "Ascending");
89              strDesc = MessageResources.getMessage("dbforms.select.sort.descending",
90                                                    reqLocale, "Descending");
91              strNone = MessageResources.getMessage("dbforms.select.sort.none",
92                                                    reqLocale, "None");
93           }
94  
95           // ---- ascending ----
96           tagBuf.append("<option value=\"asc\"");
97  
98           if ("asc".equalsIgnoreCase(oldValue)) {
99              tagBuf.append(" selected ");
100          }
101 
102          tagBuf.append(">")
103                .append(strAsc);
104 
105          // ---- descending ----
106          tagBuf.append("<option value=\"desc\"");
107 
108          if ("desc".equalsIgnoreCase(oldValue)) {
109             tagBuf.append(" selected ");
110          }
111 
112          tagBuf.append(">")
113                .append(strDesc);
114 
115          // ---- no sorting ----
116          tagBuf.append("<option value=\"none\" ");
117 
118          if ((oldValue == null) || "none".equals(oldValue)) {
119             tagBuf.append(" selected ");
120          }
121 
122          tagBuf.append(">")
123                .append(strNone);
124 
125          tagBuf.append("</select>");
126 
127          pageContext.getOut()
128                     .write(tagBuf.toString());
129       } catch (java.io.IOException ioe) {
130          throw new JspException("IO Error: " + ioe.getMessage());
131       }
132 
133       return EVAL_PAGE;
134    }
135 
136 
137    /***
138     * DOCUMENT ME!
139     */
140    public void doFinally() {
141       super.doFinally();
142    }
143 }