You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ca...@apache.org on 2007/05/18 16:26:22 UTC
svn commit: r539479 - in /myfaces/tomahawk/trunk/sandbox/core/src/main:
java/org/apache/myfaces/custom/excelexport/ tld/entities/
Author: cagatay
Date: Fri May 18 07:26:21 2007
New Revision: 539479
URL: http://svn.apache.org/viewvc?view=rev&rev=539479
Log:
Add optional filename attribute that can be used to specify the name of the export excel file
Modified:
myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExport.java
myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportPhaseListener.java
myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportRenderer.java
myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportTag.java
myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/excel_export_attributes.xml
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExport.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExport.java?view=diff&rev=539479&r1=539478&r2=539479
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExport.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExport.java Fri May 18 07:26:21 2007
@@ -35,6 +35,7 @@
private static final String DEFAULT_RENDERER = "org.apache.myfaces.ExcelExportRenderer";
private String _for;
+ private String _filename;
public ExcelExport() {
setRendererType(DEFAULT_RENDERER);
@@ -60,11 +61,24 @@
public void setFor(String forValue) {
_for = forValue;
}
+
+ public String getFilename() {
+ if (_filename != null)
+ return _filename;
+
+ ValueBinding vb = getValueBinding("filename");
+ return vb != null ? RendererUtils.getStringValue(getFacesContext(), vb) : getFor();
+ }
+
+ public void setFilename(String filename) {
+ this._filename = filename;
+ }
public Object saveState(FacesContext context) {
- Object values[] = new Object[2];
+ Object values[] = new Object[3];
values[0] = super.saveState(context);
values[1] = _for;
+ values[2] = _filename;
return ((Object) (values));
}
@@ -72,6 +86,6 @@
Object values[] = (Object[]) state;
super.restoreState(context, values[0]);
_for = (String) values[1];
+ _filename = (String) values[2];
}
-
-}
+}
\ No newline at end of file
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportPhaseListener.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportPhaseListener.java?view=diff&rev=539479&r1=539478&r2=539479
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportPhaseListener.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportPhaseListener.java Fri May 18 07:26:21 2007
@@ -53,14 +53,16 @@
if(map.containsKey("excelExportTableId")) {
String tableId = (String) map.get("excelExportTableId");
+ String filename = (String) map.get("excelExportFileName");
+
HtmlDataTable dataTable = (HtmlDataTable) ComponentUtils.findComponentById( facesContext, facesContext.getViewRoot(), tableId );
HSSFWorkbook generatedExcel = generateExcel( facesContext, dataTable );
try {
Object contextResponse = facesContext.getExternalContext().getResponse();
if(contextResponse instanceof HttpServletResponse)
- writeExcelOutput(generatedExcel,(HttpServletResponse)contextResponse, tableId);
+ writeExcelOutput(generatedExcel,(HttpServletResponse)contextResponse, filename);
else if(contextResponse instanceof RenderResponse)
- writeExcelOutput(generatedExcel,(RenderResponse)contextResponse, tableId);
+ writeExcelOutput(generatedExcel,(RenderResponse)contextResponse, filename);
facesContext.getApplication().getStateManager().saveSerializedView(facesContext);
facesContext.responseComplete();
@@ -78,22 +80,22 @@
return PhaseId.RESTORE_VIEW;
}
- private void writeExcelOutput(HSSFWorkbook workBook, HttpServletResponse response, String tableId) throws IOException{
+ private void writeExcelOutput(HSSFWorkbook workBook, HttpServletResponse response, String filename) throws IOException{
response.setContentType("application/vnd.ms-excel");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
- response.setHeader("Content-disposition", "attachment;filename=" + tableId + ".xls");
+ response.setHeader("Content-disposition", "attachment;filename=" + filename + ".xls");
workBook.write(response.getOutputStream());
}
- private void writeExcelOutput(HSSFWorkbook workBook, RenderResponse response, String tableId) throws IOException{
+ private void writeExcelOutput(HSSFWorkbook workBook, RenderResponse response, String filename) throws IOException{
response.setContentType("application/vnd.ms-excel");
response.setProperty(RenderResponse.EXPIRATION_CACHE, "0");
response.setProperty("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setProperty("Pragma", "public");
- response.setProperty("Content-disposition", "attachment;filename=" + tableId + ".xls");
+ response.setProperty("Content-disposition", "attachment;filename=" + filename + ".xls");
workBook.write(response.getPortletOutputStream());
}
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportRenderer.java?view=diff&rev=539479&r1=539478&r2=539479
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportRenderer.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportRenderer.java Fri May 18 07:26:21 2007
@@ -37,18 +37,18 @@
if(component.getChildCount() == 0)
return ;
- String tableId = ((ExcelExport) component).getFor();
+ ExcelExport excelexport = (ExcelExport) component;
UIComponent child = (UIComponent) component.getChildren().get( 0 );
- if( !isDecorated(facesContext, child, tableId) )
- decorateOnClick(facesContext, child, tableId);
+ if( !isDecorated(facesContext, child, excelexport) )
+ decorateOnClick(facesContext, child, excelexport);
RendererUtils.renderChildren(facesContext, component);
}
- private boolean isDecorated(FacesContext facesContext, UIComponent child, String tableId) {
+ private boolean isDecorated(FacesContext facesContext, UIComponent child, ExcelExport excelExport) {
String onClick = (String) child.getAttributes().get("onclick");
- String jsCall = getJSCall(facesContext, tableId);
+ String jsCall = getJSCall(facesContext, excelExport);
if(onClick == null || onClick.indexOf(jsCall) == -1)
return false;
@@ -56,8 +56,8 @@
return true;
}
- private void decorateOnClick(FacesContext facesContext, UIComponent child, String tableId) {
- String jsCall = getJSCall(facesContext, tableId);
+ private void decorateOnClick(FacesContext facesContext, UIComponent child, ExcelExport excelExport) {
+ String jsCall = getJSCall(facesContext, excelExport);
String onclickEvent = (String) child.getAttributes().get("onclick");
if(onclickEvent == null)
child.getAttributes().put("onclick", jsCall);
@@ -65,13 +65,11 @@
child.getAttributes().put("onclick", onclickEvent + ";" + jsCall);
}
- private String getJSCall(FacesContext facesContext, String tableId) {
+ private String getJSCall(FacesContext facesContext, ExcelExport excelExport) {
String viewId = StringUtils.split( facesContext.getViewRoot().getViewId() , "\\.")[0];
return "window.open('"
+ facesContext.getApplication().getViewHandler().getActionURL(
facesContext, viewId) + "?excelExportTableId="
- + tableId + "');return false;";
+ + excelExport.getFor() + "&excelExportFileName=" + excelExport.getFilename() + "');return false;";
}
-
-
-}
+}
\ No newline at end of file
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportTag.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportTag.java?view=diff&rev=539479&r1=539478&r2=539479
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportTag.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportTag.java Fri May 18 07:26:21 2007
@@ -29,16 +29,19 @@
public class ExcelExportTag extends UIComponentTagBase {
private String _for;
+ private String _filename;
public void release() {
super.release();
_for = null;
+ _filename = null;
}
protected void setProperties(UIComponent component) {
super.setProperties(component);
setStringProperty(component, "for", _for);
+ setStringProperty(component, "filename", _filename);
}
public String getComponentType() {
@@ -52,9 +55,16 @@
public String getFor() {
return _for;
}
-
+
public void setFor(String aFor) {
_for = aFor;
}
+ public String getFilename() {
+ return _filename;
+ }
+
+ public void setFilename(String filename) {
+ this._filename = filename;
+ }
}
Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/excel_export_attributes.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/excel_export_attributes.xml?view=diff&rev=539479&r1=539478&r2=539479
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/excel_export_attributes.xml (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/excel_export_attributes.xml Fri May 18 07:26:21 2007
@@ -3,5 +3,12 @@
<required>true</required>
<rtexprvalue>false</rtexprvalue>
<type>java.lang.String</type>
- <description>The datatable component whose values to be exported</description>
+ <description>Id of the datatable component whose values to be exported</description>
+</attribute>
+<attribute>
+ <name>filename</name>
+ <required>false</required>
+ <rtexprvalue>false</rtexprvalue>
+ <type>java.lang.String</type>
+ <description>Default name of the generated excel file, if not specified value of the "for" attribute will be used</description>
</attribute>