View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.exporter;
20  
21  import javax.faces.component.ActionSource;
22  import javax.faces.component.UIComponent;
23  import javax.faces.webapp.UIComponentTag;
24  import javax.servlet.jsp.JspException;
25  import javax.servlet.jsp.tagext.Tag;
26  import javax.servlet.jsp.tagext.TagSupport;
27  
28  /**
29   * Export datatable contents to an excel file or a pdf file.
30   * 
31   * @JSFJspTag
32   *   name="s:exporterActionListener"
33   *   bodyContent="JSP" 
34   * 
35   * This class is acting as the tag handler for the Exporter ActionListener.
36   */
37  public class ExporterActionListenerTag extends TagSupport {
38  
39      private static final long serialVersionUID = -1455677614701939262L;
40      private String _fileType;
41      private String _fileName;
42      private String _for;
43      
44      public int doStartTag() throws JspException {
45  
46          // check whether the attributes are not null
47          if (_for == null) 
48          {
49              throw new JspException("for attribute not set");
50          }
51  
52          if (_fileType == null) 
53          {
54              throw new JspException("fileType attribute not set");
55          }
56  
57          // find the parent UIComponentTag which should be an ActionSource.
58          UIComponentTag componentTag = UIComponentTag
59                  .getParentUIComponentTag(pageContext);
60  
61          if (componentTag == null) 
62          {
63              throw new JspException("ExporterActionListenerTag has no UIComponentTag ancestor");
64          }
65  
66          if (componentTag.getCreated()) 
67          {
68  
69              // if the component was just created, so we add the Listener.
70              UIComponent component = componentTag.getComponentInstance();
71  
72              if (component instanceof ActionSource) 
73              {
74                  ExporterActionListener exporterActionListener = new ExporterActionListener();
75  
76                  exporterActionListener.setFor(_for);
77                  exporterActionListener.setFileType(_fileType);
78                  exporterActionListener.setFilename(_fileName);
79  
80                  ((ActionSource) component)
81                          .addActionListener(exporterActionListener);
82              }
83              else 
84              {
85                  throw new JspException("Component " + component.getId()
86                          + " is no ActionSource");
87              }
88          }
89  
90          return Tag.SKIP_BODY;
91      }
92  
93      public void release() {
94          super.release();
95          _fileType = null;
96          _fileName = null;
97          _for = null;
98      }
99      
100     public String getFilename() {
101         return _fileName;
102     }
103 
104     /**
105      * Default name of the generated excel or pdf file, if not specified value of the "for" attribute will be used
106      * 
107      * @JSFJspAttribute
108      */
109     public void setFilename(String _filename) {
110         this._fileName = _filename;
111     }
112 
113     public String getFileType() {
114         return _fileType;
115     }
116 
117     /**
118      * "PDF" or "XLS"
119      * 
120      * @JSFJspAttribute
121      */
122     public void setFileType(String type) {
123         _fileType = type;
124     }
125 
126     public String getFor() {
127         return _for;
128     }
129 
130     /**
131      * Id of the datatable component whose values to be exported
132      * 
133      * @JSFJspAttribute
134      */
135     public void setFor(String _for) {
136         this._for = _for;
137     }
138 }