View Javadoc

1   package org.apache.commons.monitoring.servlet.jsp;
2   
3   import java.util.Collection;
4   import java.util.Collections;
5   import java.util.LinkedList;
6   import java.util.List;
7   
8   import javax.servlet.jsp.JspException;
9   import javax.servlet.jsp.JspTagException;
10  import javax.servlet.jsp.tagext.TagSupport;
11  
12  import org.apache.commons.monitoring.Repository;
13  import org.apache.commons.monitoring.servlet.ServletContextUtil;
14  
15  public abstract class AbstractSelectTag
16      extends TagSupport
17  {
18      private String id;
19  
20      private String name;
21  
22      String repository;
23  
24      public AbstractSelectTag()
25      {
26          super();
27      }
28  
29      protected Repository getRepository()
30          throws JspException
31      {
32          return TagUtils.getRepository( pageContext, repository );
33      }
34  
35      @Override
36      public int doEndTag()
37          throws JspException
38      {
39          StringBuffer out = new StringBuffer( "<select" );
40          TagUtils.setAttribute( out, "name", name );
41          TagUtils.setAttribute( out, "id", id );
42          out.append( ">" );
43          List<String> categories = new LinkedList<String>( getElements() );
44          Collections.sort( categories );
45          for ( String category : categories )
46          {
47              out.append( "<option value='" );
48              out.append( category );
49              out.append( "'>" );
50              out.append( category );
51              out.append( "</option>" );
52          }
53  
54          out.append( "</select>" );
55          try
56          {
57              pageContext.getOut().print( out.toString() );
58          }
59          catch ( Exception e )
60          {
61              throw new JspTagException( "CategoriesTag : " + e.getMessage() );
62          }
63  
64          return EVAL_PAGE;
65      }
66  
67      /**
68       * The set of elements to display as a select list
69       *
70       * @return
71       */
72      protected abstract Collection<? extends String> getElements()
73          throws JspException;
74  
75      public void setId( String id )
76      {
77          this.id = id;
78      }
79  
80      public void setName( String name )
81      {
82          this.name = name;
83      }
84  
85      /**
86       * @param repository the repository to set
87       */
88      public void setRepository( String repository )
89      {
90          this.repository = repository;
91      }
92  
93  }