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.commons.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  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspTag;
29  
30  /**
31   * Export datatable contents to an excel file or a pdf file.
32   * 
33   * This class is acting as the tag handler for the Exporter ActionListener.
34   */
35  @JSFJspTag(
36     name="mc:exporterActionListener",
37     bodyContent="JSP",
38     tagHandler="org.apache.myfaces.commons.exporter.FaceletsExporterActionListenerTag")
39  public class ExporterActionListenerTag extends TagSupport {
40  
41      private static final long serialVersionUID = -1455677614701939262L;
42      private String _fileType;
43      private String _fileName;
44      private String _for;
45      
46      public int doStartTag() throws JspException {
47  
48          // check whether the attributes are not null
49          if (_for == null) 
50          {
51              throw new JspException("for attribute not set");
52          }
53  
54          if (_fileType == null) 
55          {
56              throw new JspException("fileType attribute not set");
57          }
58          
59          if (_fileName == null) 
60          {
61              throw new JspException("fileName attribute not set");
62          }        
63  
64          // find the parent UIComponentTag which should be an ActionSource.
65          UIComponentTag componentTag = UIComponentTag
66                  .getParentUIComponentTag(pageContext);
67  
68          if (componentTag == null) 
69          {
70              throw new JspException("ExporterActionListenerTag has no UIComponentTag ancestor");
71          }
72  
73          if (componentTag.getCreated()) 
74          {
75  
76              // if the component was just created, so we add the Listener.
77              UIComponent component = componentTag.getComponentInstance();
78  
79              if (component instanceof ActionSource) 
80              {
81                  ExporterActionListener exporterActionListener = new ExporterActionListener();
82  
83                  exporterActionListener.setFor(_for);
84                  exporterActionListener.setFileType(_fileType);
85                  exporterActionListener.setFilename(_fileName);
86  
87                  ((ActionSource) component)
88                          .addActionListener(exporterActionListener);
89              }
90              else 
91              {
92                  throw new JspException("Component " + component.getId()
93                          + " is no ActionSource");
94              }
95          }
96  
97          return Tag.SKIP_BODY;
98      }
99  
100     public void release() {
101         super.release();
102         _fileType = null;
103         _fileName = null;
104         _for = null;
105     }
106     
107     public String getFilename() {
108         return _fileName;
109     }
110 
111     /**
112      * Default name of the generated excel or pdf file, if not specified value of the "for" attribute will be used
113      * 
114      * @JSFJspAttribute
115      */
116     public void setFilename(String _filename) {
117         this._fileName = _filename;
118     }
119 
120     public String getFileType() {
121         return _fileType;
122     }
123 
124     /**
125      * "PDF" or "XLS"
126      * 
127      * @JSFJspAttribute
128      */
129     public void setFileType(String type) {
130         _fileType = type;
131     }
132 
133     public String getFor() {
134         return _for;
135     }
136 
137     /**
138      * Id of the datatable component whose values to be exported
139      * 
140      * @JSFJspAttribute
141      */
142     public void setFor(String _for) {
143         this._for = _for;
144     }
145     
146     
147 }