click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r824948 - in /incubator/click/trunk/click/examples/src/org/apache/click/examples: control/ control/exporter/ page/general/
Date Tue, 13 Oct 2009 22:12:20 GMT
Author: sabob
Date: Tue Oct 13 22:12:20 2009
New Revision: 824948

URL: http://svn.apache.org/viewvc?rev=824948&view=rev
Log:
updated table exporter

Added:
    incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/
    incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/AbstractTableExporter.java
    incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExcelTableExporter.java
    incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExportTable.java
    incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExportTable.properties
    incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/TableExportBanner.java
Removed:
    incubator/click/trunk/click/examples/src/org/apache/click/examples/control/ExportTable.java
Modified:
    incubator/click/trunk/click/examples/src/org/apache/click/examples/page/general/ExcelTableExportPage.java

Added: incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/AbstractTableExporter.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/AbstractTableExporter.java?rev=824948&view=auto
==============================================================================
--- incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/AbstractTableExporter.java
(added)
+++ incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/AbstractTableExporter.java
Tue Oct 13 22:12:20 2009
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.click.examples.control.exporter;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.click.ActionListener;
+import org.apache.click.Context;
+import org.apache.click.Control;
+import org.apache.click.control.AbstractControl;
+import org.apache.click.control.AbstractLink;
+import org.apache.click.control.ActionLink;
+import org.apache.click.util.ClickUtils;
+import org.apache.commons.lang.StringUtils;
+
+/**
+ *
+ */
+public abstract class AbstractTableExporter {
+
+    protected String label;
+
+    protected String imageSrc;
+
+    protected AbstractLink exportLink;
+
+    protected boolean selected;
+
+    public AbstractTableExporter() {
+    }
+
+    public AbstractTableExporter(String label) {
+        this.label = label;
+    }
+
+    public AbstractTableExporter(String label, String imageSrc) {
+        this.label = label;
+        this.imageSrc = imageSrc;
+    }
+
+    public boolean isSelected() {
+        return selected;
+    }
+
+    public void setSelected(boolean selected) {
+        this.selected = selected;
+    }
+
+    public void setExportLink(AbstractLink exportLink) {
+        this.exportLink = exportLink;
+        if (exportLink.getActionListener() == null) {
+            setActionListener(this.exportLink);
+        }
+    }
+
+    public AbstractLink getExportLink() {
+        if (exportLink == null) {
+            exportLink = new ActionLink();
+            exportLink.setLabel(getLabel());
+            exportLink.setImageSrc(getImageSrc());
+
+            if (StringUtils.isNotBlank(getLabel()) && StringUtils.isNotBlank(
+                getImageSrc())) {
+                exportLink.setRenderLabelAndImage(true);
+            }
+
+            setActionListener(exportLink);
+        }
+        return exportLink;
+    }
+
+    public String getLabel() {
+        return label;
+    }
+
+    public void setLabel(String label) {
+        this.label = label;
+    }
+
+    public String getImageSrc() {
+        return imageSrc;
+    }
+
+    public void setImageSrc(String imageSrc) {
+        this.imageSrc = imageSrc;
+    }
+
+    public abstract void setName(String name);
+
+    public void export(ExportTable table, Context context) {
+        HttpServletResponse response = context.getResponse();
+
+        // Set response headers
+        String mimeType = getMimeType();
+        response.setHeader("Content-Disposition", "attachment; filename=\"" + getFilename()
+ "\"");
+        response.setContentType(mimeType);
+        response.setHeader("Pragma", "no-cache");
+
+        OutputStream outputStream = null;
+        try {
+            ByteArrayOutputStream output = new ByteArrayOutputStream();
+            export(table, context, output);
+
+            byte[] bytes = output.toByteArray();
+
+            response.setContentLength(bytes.length);
+
+            outputStream = response.getOutputStream();
+
+            // Write out Excel Workbook to response stream
+            outputStream.write(bytes);
+            outputStream.flush();
+
+            // Specify no further rendering required
+            table.getPage().setPath(null);
+
+        } catch (IOException ioe) {
+            throw new RuntimeException(ioe);
+
+        } finally {
+            ClickUtils.close(outputStream);
+        }
+    }
+
+    protected abstract String getMimeType();
+
+    protected abstract void export(ExportTable table, Context context, OutputStream output)
+        throws IOException;
+
+    protected abstract String getFilename();
+
+    protected void setActionListener(AbstractControl control) {
+        control.setActionListener(new ActionListener() {
+
+            public boolean onAction(Control source) {
+                setSelected(true);
+                return true;
+            }
+        });
+    }
+}

Added: incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExcelTableExporter.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExcelTableExporter.java?rev=824948&view=auto
==============================================================================
--- incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExcelTableExporter.java
(added)
+++ incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExcelTableExporter.java
Tue Oct 13 22:12:20 2009
@@ -0,0 +1,324 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.click.examples.control.exporter;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.text.MessageFormat;
+import java.util.Calendar;
+import java.util.Currency;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.click.Context;
+import org.apache.click.control.Column;
+import org.apache.click.examples.util.ExampleUtils;
+import org.apache.click.util.ClickUtils;
+import org.apache.commons.lang.ObjectUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
+import org.apache.poi.hssf.usermodel.HSSFFont;
+import org.apache.poi.hssf.usermodel.HSSFRichTextString;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.DataFormat;
+
+/**
+ *
+ */
+public class ExcelTableExporter extends AbstractTableExporter {
+
+    private DataFormat cellFormat;
+
+    private HSSFWorkbook wb;
+
+    private int currentRow;
+
+    private Locale locale;
+
+    private Currency currency;
+
+    private Map<String, HSSFCellStyle> cellStyles = new HashMap();
+
+    public ExcelTableExporter() {
+    }
+
+    public ExcelTableExporter(String label) {
+        super(label);
+    }
+
+    public ExcelTableExporter(String label, String imageSrc) {
+        super(label, imageSrc);
+    }
+
+    @Override
+    public void setName(String name) {
+        getExportLink().setName(name + "-excel");
+    }
+
+    @Override
+    protected String getMimeType() {
+        return ClickUtils.getMimeType(".xls");
+    }
+
+    @Override
+    protected String getFilename() {
+        return "report.xls";
+    }
+
+    @Override
+    protected void export(ExportTable table, Context context,
+        OutputStream output) throws IOException {
+
+        wb = createWorkbook();
+        cellFormat = wb.createDataFormat();
+        locale = context.getLocale();
+        currency = Currency.getInstance(locale);
+
+        currentRow = 0;
+
+        HSSFSheet sheet =
+            wb.createSheet(StringUtils.capitalize(table.getName()));
+        createCellStyles(table, sheet, context);
+        exportTableHeader(table, sheet, context);
+        exportTableBody(table, sheet, context);
+
+        int count = 0;
+        while (count <= table.getColumnList().size()) {
+            sheet.autoSizeColumn(count++);
+        }
+
+        wb.write(output);
+    }
+
+    protected void exportTableHeader(ExportTable table, HSSFSheet sheet,
+        Context context) {
+        if (table.isExportTableHeaders()) {
+
+            HSSFRow row = createRow(sheet);
+            HSSFCellStyle style = createHeaderStyle(sheet);
+            final List tableColumns = table.getExportColumnList();
+
+            for (int i = 0, size = tableColumns.size(); i < size; i++) {
+                HSSFCell cell = row.createCell(i);
+                cell.setCellStyle(style);
+
+                Column column = (Column) tableColumns.get(i);
+                String title = column.getHeaderTitle();
+                cell.setCellValue(title);
+            }
+        }
+    }
+
+    protected void exportTableBody(ExportTable table, HSSFSheet sheet,
+        Context context) {
+        int start = table.getFirstExportRow();
+        int end = table.getLastExportRow();
+        if (end == ExportTable.ALL_ROWS) {
+            end = table.getRowList().size();
+        }
+
+        final List tableColumns = table.getExportColumnList();
+
+        for (int rowIndex = start; rowIndex < end; rowIndex++) {
+            HSSFRow excelRow = createRow(sheet);
+
+            Object row = table.getRowList().get(rowIndex);
+
+            for (int columnIndex = 0, size = tableColumns.size(); columnIndex <
+                size; columnIndex++) {
+                HSSFCell cell = excelRow.createCell(columnIndex);
+                Column column = (Column) tableColumns.get(columnIndex);
+                HSSFCellStyle style = cellStyles.get(column.getName());
+                if (style != null) {
+                    cell.setCellStyle(style);
+                }
+
+                Object columnValue =
+                    getColumnValue(row, column, context);
+
+                setCellValue(columnValue, cell);
+            }
+        }
+    }
+
+    protected Object getColumnValue(Object row, Column column, Context context) {
+        Object value = null;
+        if (column.getDecorator() != null) {
+            value = column.getDecorator().render(row, context);
+        } else {
+            value = column.getProperty(row);
+        }
+        return value;
+    }
+
+    protected HSSFWorkbook createWorkbook() {
+        HSSFWorkbook wb = new HSSFWorkbook();
+        return wb;
+    }
+
+    protected void createCellStyles(ExportTable table, HSSFSheet sheet,
+        Context context) {
+
+        final List<Column> tableColumns = table.getExportColumnList();
+        for (Column column : tableColumns) {
+            MessageFormat messageFormat = column.getMessageFormat();
+            String format = column.getFormat();
+            if (messageFormat == null && format != null) {
+                messageFormat = new MessageFormat(format, locale);
+            }
+
+            if (messageFormat != null) {
+                HSSFCellStyle style = createCellStyle(messageFormat);
+                cellStyles.put(column.getName(), style);
+            }
+        }
+    }
+
+    protected HSSFRow createRow(HSSFSheet sheet) {
+        HSSFRow row = sheet.createRow(currentRow);
+        currentRow++;
+        return row;
+    }
+
+    protected HSSFCellStyle createHeaderStyle(HSSFSheet sheet) {
+        HSSFCellStyle style = wb.createCellStyle();
+        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
+        HSSFFont font = wb.createFont();
+        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
+        style.setFont(font);
+        return style;
+    }
+
+    protected void setCellValue(Object value, HSSFCell cell) {
+        if (value instanceof Number) {
+            Number num = (Number) value;
+            cell.setCellValue(num.doubleValue());
+        } else if (value instanceof Date) {
+            cell.setCellValue((Date) value);
+        } else if (value instanceof Calendar) {
+            cell.setCellValue((Calendar) value);
+        } else if (value instanceof Boolean) {
+            cell.setCellValue((Boolean) value);
+        } else {
+            cell.setCellValue(
+                new HSSFRichTextString(ObjectUtils.toString(value)));
+        }
+    }
+
+    protected HSSFCellStyle createCellStyle(MessageFormat messageFormat) {
+        HSSFCellStyle cellStyle = null;
+        String pattern = messageFormat.toPattern();
+        cellStyle = wb.createCellStyle();
+        short format = parseCellFormat(pattern);
+        cellStyle.setDataFormat(format);
+
+        return cellStyle;
+    }
+
+    protected short parseCellFormat(String pattern) {
+        short format = 0;
+        if (pattern.indexOf("date") != -1) {
+            if (pattern.indexOf("short") != -1) {
+                format = cellFormat.getFormat(cellFormat.getFormat((short) 15));
+            } else if (pattern.indexOf("medium") != -1) {
+                format = cellFormat.getFormat("mmm dd, yyyy");
+            } else if (pattern.indexOf("long") != -1) {
+                format = cellFormat.getFormat("mmmm dd, yyyy");
+            } else if (pattern.indexOf("full") != -1) {
+                format = cellFormat.getFormat("dddd, mmmm dd, yyyy");
+            } else {
+                String custom = extractCustomPattern(pattern, "date");
+                if (StringUtils.isNotBlank(custom)) {
+                    format = cellFormat.getFormat(custom);
+                } else {
+                    format = cellFormat.getFormat("mmm dd, yyyy");
+                }
+            }
+        } else if (pattern.indexOf("time") != -1) {
+            if (pattern.indexOf("short") != -1) {
+                format = cellFormat.getFormat("hh:mm");
+            } else if (pattern.indexOf("medium") != -1) {
+                format = cellFormat.getFormat("hh:mm:ss");
+            } else if (pattern.indexOf("long") != -1) {
+                format = cellFormat.getFormat("hh:mm:ss AM/PM");
+            } else if (pattern.indexOf("full") != -1) {
+                format = cellFormat.getFormat("hh:mm:ss AM/PM");
+            } else {
+                String custom = extractCustomPattern(pattern, "time");
+                if (StringUtils.isNotBlank(custom)) {
+                    format = cellFormat.getFormat(custom);
+                } else {
+                    format = cellFormat.getFormat("hh:mm:ss");
+                }
+            }
+        } else if (pattern.indexOf("number") != -1) {
+            if (pattern.indexOf("integer") != -1) {
+                format = cellFormat.getFormat("0");
+            } else if (pattern.indexOf("currency") != -1) {
+                String currencyPattern = "#,##0.00";
+                if (currency != null) {
+                    currencyPattern = ExampleUtils.getCurrencySymbol(currency) +
+                        currencyPattern;
+                }
+                format = cellFormat.getFormat(currencyPattern);
+            } else if (pattern.indexOf("percent") != -1) {
+                format = cellFormat.getFormat("0%");
+            } else {
+                String custom = extractCustomPattern(pattern, "number");
+                if (StringUtils.isNotBlank(custom)) {
+                    format = cellFormat.getFormat(custom);
+                } else {
+                    format = cellFormat.getFormat("0");
+                }
+            }
+        } else {
+            format = cellFormat.getFormat(pattern);
+        }
+        return format;
+    }
+
+    private String extractCustomPattern(String fullPattern, String type) {
+        String custom = fullPattern;
+
+        int start = fullPattern.indexOf(type);
+        int end = fullPattern.lastIndexOf('}');
+        if (end == -1) {
+            end = fullPattern.length();
+        }
+
+        if (start != -1) {
+            // Move start after last comma
+            int lastComma = fullPattern.indexOf(",", start);
+            if (lastComma != -1) {
+                start = ++lastComma;
+            } else {
+                start = start + type.length();
+            }
+
+            custom = fullPattern.substring(start, end);
+        }
+
+        return StringUtils.trim(custom);
+    }
+}

Added: incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExportTable.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExportTable.java?rev=824948&view=auto
==============================================================================
--- incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExportTable.java
(added)
+++ incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExportTable.java
Tue Oct 13 22:12:20 2009
@@ -0,0 +1,246 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.click.examples.control.exporter;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.click.control.Column;
+import org.apache.click.control.Table;
+import org.apache.click.util.HtmlStringBuffer;
+
+/**
+ * This example provides a table that can be exported to an Excel spreadsheet.
+ */
+public class ExportTable extends Table {
+
+    final static int ALL_ROWS = Integer.MAX_VALUE;
+
+    public static final int EXPORTER_ATTACHED = 1;
+
+    public static final int EXPORTER_DETACHED = 2;
+
+    public static final int EXPORTER_INLINE = 3;
+
+    protected boolean exportTableHeaders = true;
+
+    protected int firstExportRow;
+
+    protected int lastExportRow = ALL_ROWS;
+
+    protected int exportBannerPosition = POSITION_BOTTOM;
+
+    protected int exportAttachment = EXPORTER_ATTACHED;
+
+    protected List excludedColumns;
+
+    protected List exportColumnList;
+
+    protected TableExportBanner exporter;
+
+    public ExportTable() {
+    }
+
+    public ExportTable(String name) {
+        super(name);
+    }
+
+    public TableExportBanner getExporter() {
+        if (exporter == null) {
+            exporter = new TableExportBanner(this);
+        }
+        return exporter;
+    }
+
+    public void setExporter(TableExportBanner exporter) {
+        this.exporter = exporter;
+    }
+
+    /**
+     * @return the exportTableHeaders
+     */
+    public boolean isExportTableHeaders() {
+        return exportTableHeaders;
+    }
+
+    /**
+     * @return the firstExportRow
+     */
+    public int getFirstExportRow() {
+        return firstExportRow;
+    }
+
+    /**
+     * @param firstExportRow the firstExportRow to set
+     */
+    public void setFirstExportRow(int firstExportRow) {
+        this.firstExportRow = firstExportRow;
+    }
+
+    /**
+     * @return the lastExportRow
+     */
+    public int getLastExportRow() {
+        return lastExportRow;
+    }
+
+    /**
+     * @param lastExportRow the lastExportRow to set
+     */
+    public void setLastExportRow(int lastExportRow) {
+        this.lastExportRow = lastExportRow;
+    }
+
+    /**
+     * @param exportTableHeaders the exportTableHeaders to set
+     */
+    public void setExportTableHeaders(boolean exportTableHeaders) {
+        this.exportTableHeaders = exportTableHeaders;
+    }
+
+    /**
+     * @return the exportBannerPosition
+     */
+    public int getExportBannerPosition() {
+        return exportBannerPosition;
+    }
+
+    /**
+     * @param exportBannerPosition the exportBannerPosition to set
+     */
+    public void setExportBannerPosition(int exportBannerPosition) {
+        this.exportBannerPosition = exportBannerPosition;
+    }
+
+    /**
+     * @return the excludedColumns
+     */
+    public List getExcludedColumns() {
+        if (excludedColumns == null) {
+            excludedColumns = new ArrayList();
+        }
+        return excludedColumns;
+    }
+
+    /**
+     * @param excludedColumns the excludedColumns to set
+     */
+    public void setExcludedColumns(List excludedColumns) {
+        this.excludedColumns = excludedColumns;
+    }
+
+    public List getExportColumnList() {
+        if (exportColumnList == null) {
+            exportColumnList = new ArrayList();
+            List<Column> columns = getColumnList();
+            List<String> excludes = getExcludedColumns();
+
+            for (Column column : columns) {
+                String name = column.getName();
+                if (!excludes.contains(name)) {
+                    exportColumnList.add(column);
+                }
+            }
+        }
+        return exportColumnList;
+    }
+
+    @Override
+    public void onInit() {
+        super.onInit();
+        getExporter().onInit();
+    }
+
+    @Override
+    public void onRender() {
+        super.onRender();
+        getExporter().onRender();
+    }
+
+    @Override
+    public boolean onProcess() {
+        boolean continueProcessing = super.onProcess();
+        TableExportBanner tableExporter = getExporter();
+        if (!tableExporter.onProcess()) {
+            continueProcessing = false;
+        }
+        return continueProcessing;
+    }
+
+    public void render(HtmlStringBuffer buffer) {
+        if (getExportAttachment() == EXPORTER_ATTACHED) {
+            if (getExportBannerPosition() == POSITION_BOTH ||
+                getExportBannerPosition() == POSITION_TOP) {
+                getExporter().render(buffer);
+            }
+        }
+
+        super.render(buffer);
+
+        if (getExportAttachment() == EXPORTER_ATTACHED) {
+            if (getExportBannerPosition() == POSITION_BOTH ||
+                getExportBannerPosition() == POSITION_BOTTOM) {
+                getExporter().render(buffer);
+            }
+        }
+    }
+
+    protected void renderBodyRows(HtmlStringBuffer buffer) {
+        if (getExportAttachment() == EXPORTER_INLINE) {
+            if (getExportBannerPosition() == POSITION_TOP
+                || getExportBannerPosition() == POSITION_BOTH) {
+                buffer.append("<tbody>\n");
+                buffer.append("<tr class=\"export-inline\">\n");
+                buffer.append("<td class=\"export-inline\" colspan=\"");
+                buffer.append(getColumnList().size());
+                buffer.append("\">");
+
+                getExporter().render(buffer);
+
+                buffer.append("</td></tr>\n");
+                buffer.append("</tbody>\n");
+            }
+        }
+
+        super.renderBodyRows(buffer);
+
+        if (getExportAttachment() == EXPORTER_INLINE) {
+            if (getExportBannerPosition() == POSITION_BOTTOM
+                || getExportBannerPosition() == POSITION_BOTH) {
+                buffer.append("<tbody>\n");
+                buffer.append("<tr class=\"export-inline\">\n");
+                buffer.append("<td class=\"export-inline\" colspan=\"");
+                buffer.append(getColumnList().size());
+                buffer.append("\">");
+
+                getExporter().render(buffer);
+
+                buffer.append("</td></tr>\n");
+                buffer.append("</tbody>\n");
+            }
+        }
+    }
+
+    public int getExportAttachment() {
+        return exportAttachment;
+    }
+
+    public void setExportAttachment(int exportAttachment) {
+        this.exportAttachment = exportAttachment;
+    }
+}

Added: incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExportTable.properties
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExportTable.properties?rev=824948&view=auto
==============================================================================
--- incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExportTable.properties
(added)
+++ incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/ExportTable.properties
Tue Oct 13 22:12:20 2009
@@ -0,0 +1 @@
+table-export-banner=<span class="{0}">Export options: [{1}]</span>
\ No newline at end of file

Added: incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/TableExportBanner.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/TableExportBanner.java?rev=824948&view=auto
==============================================================================
--- incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/TableExportBanner.java
(added)
+++ incubator/click/trunk/click/examples/src/org/apache/click/examples/control/exporter/TableExportBanner.java
Tue Oct 13 22:12:20 2009
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.click.examples.control.exporter;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.apache.click.Context;
+import org.apache.click.Control;
+import org.apache.click.control.Renderable;
+import org.apache.click.util.HtmlStringBuffer;
+
+/**
+ *
+ */
+public class TableExportBanner implements Renderable {
+
+    private List<AbstractTableExporter> exportFormats = new ArrayList<AbstractTableExporter>();
+
+    protected String separator;
+
+    private ExportTable table;
+
+    public TableExportBanner(ExportTable table) {
+        this.table = table;
+    }
+
+    public void add(AbstractTableExporter exportFormat) {
+        getExportFormats().add(exportFormat);
+    }
+
+    public void remove(AbstractTableExporter exportFormat) {
+        getExportFormats().remove(exportFormat);
+    }
+
+    public void render(HtmlStringBuffer buffer) {
+        renderExportBanner(buffer);
+    }
+
+    public List<AbstractTableExporter> getExportFormats() {
+        return exportFormats;
+    }
+
+    public void setExportFormats(List<AbstractTableExporter> exporters) {
+        this.exportFormats = exporters;
+    }
+
+    public void onInit() {
+        String tableName = table.getName();
+        for (AbstractTableExporter format : getExportFormats()) {
+            format.setName(tableName);
+            Control control = format.getExportLink();
+            control.onInit();
+        }
+    }
+
+    public void onRender() {
+        AbstractTableExporter selectedFormat = null;
+        for (AbstractTableExporter format : getExportFormats()) {
+            format.getExportLink().onRender();
+            if (format.isSelected()) {
+                selectedFormat = format;
+            }
+        }
+        if(selectedFormat != null) {
+            export(selectedFormat);
+        }
+    }
+
+    public boolean onProcess() {
+        boolean continueProcessing = true;
+        for (AbstractTableExporter format : getExportFormats()) {
+            if (!format.getExportLink().onProcess()) {
+                continueProcessing = false;
+            }
+        }
+        return continueProcessing;
+    }
+
+    public String getSeparator() {
+        return separator;
+    }
+
+    public void setSeparator(String separator) {
+        this.separator = separator;
+    }
+
+    public void export(AbstractTableExporter format) {
+        Context context = table.getContext();
+        format.export(table, context);
+    }
+
+    public String toString() {
+        HtmlStringBuffer buffer = new HtmlStringBuffer();
+
+        render(buffer);
+
+        return buffer.toString();
+    }
+
+    /**
+     * Render the table export banner.
+     * <p/>
+     * See the <tt>TableExportBanner.properies</tt> for the HTML template:
+     * <tt>table-export-banner</tt>.
+     *
+     * @param buffer the StringBuffer to render the paging controls to
+     */
+    protected void renderExportBanner(HtmlStringBuffer buffer) {
+
+        HtmlStringBuffer banner = new HtmlStringBuffer();
+        Iterator<AbstractTableExporter> it = getExportFormats().iterator();
+        while(it.hasNext()) {
+            AbstractTableExporter format = it.next();
+            format.getExportLink().render(banner);
+            if (it.hasNext()) {
+                banner.append(getSeparator());
+            }
+        }
+        String[] args = { getStyleClass(), banner.toString()};
+        buffer.append(table.getMessage("table-export-banner", args));
+    }
+
+    protected String getStyleClass() {
+        if (table.getExportAttachment() == ExportTable.EXPORTER_ATTACHED) {
+            return "export-attached";
+        } else if (table.getExportAttachment() == ExportTable.EXPORTER_DETACHED) {
+            return "export-detached";
+        } else if (table.getExportAttachment() == ExportTable.EXPORTER_INLINE) {
+            return "export-inline";
+        }
+        return "export";
+    }
+}

Modified: incubator/click/trunk/click/examples/src/org/apache/click/examples/page/general/ExcelTableExportPage.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/src/org/apache/click/examples/page/general/ExcelTableExportPage.java?rev=824948&r1=824947&r2=824948&view=diff
==============================================================================
--- incubator/click/trunk/click/examples/src/org/apache/click/examples/page/general/ExcelTableExportPage.java
(original)
+++ incubator/click/trunk/click/examples/src/org/apache/click/examples/page/general/ExcelTableExportPage.java
Tue Oct 13 22:12:20 2009
@@ -27,7 +27,8 @@
 import org.apache.click.control.Column;
 import org.apache.click.control.PageLink;
 import org.apache.click.control.Table;
-import org.apache.click.examples.control.ExportTable;
+import org.apache.click.examples.control.exporter.ExcelTableExporter;
+import org.apache.click.examples.control.exporter.ExportTable;
 import org.apache.click.examples.domain.Customer;
 import org.apache.click.examples.page.BorderPage;
 import org.apache.click.examples.page.EditCustomer;
@@ -43,7 +44,10 @@
 @Component
 public class ExcelTableExportPage extends BorderPage {
 
-    private ExportTable table = new ExportTable("table");
+    private ExportTable table1 = new ExportTable("table1");
+    private ExportTable table2 = new ExportTable("table2");
+    private ExportTable table3 = new ExportTable("table3");
+
     private PageLink editLink = new PageLink("Edit", EditCustomer.class);
 
     @Resource(name="customerService")
@@ -52,52 +56,88 @@
     // ------------------------------------------------------------ Constructor
 
     public ExcelTableExportPage() {
+        addColumns(table1);
+        addColumns(table2);
+        addColumns(table3);
+
+        setupExporter(table1);
+        table1.setExportAttachment(ExportTable.EXPORTER_ATTACHED);
+
+        setupExporter(table2);
+        table2.setExportAttachment(ExportTable.EXPORTER_DETACHED);
+
+        setupExporter(table3);
+        table3.setExportAttachment(ExportTable.EXPORTER_INLINE);
+
+        addControl(table1);
+        addControl(table2);
+        addControl(table3);
+        addControl(editLink);
+    }
+
+    // --------------------------------------------------------- Event Handlers
+
+    /**
+     * @see Page#onRender()
+     */
+    @Override
+    public void onRender() {
+        List<Customer> customers = customerService.getCustomersSortedByName(10);
+        table1.setRowList(customers);
+        table2.setRowList(customers);
+        table3.setRowList(customers);
+    }
+
+    // -------------------------------------------------------- Private Methods
+
+    private void setupExporter(ExportTable table) {
+        // Setup table exporting
+        ExcelTableExporter excel = new ExcelTableExporter("Excel", "/assets/images/page_excel.png");
+        table.getExporter().add(excel);
+
+        // Exclude the action column from being exported
+        table.getExcludedColumns().add("action");
+    }
+
+    private void addColumns(ExportTable table) {
+        table.setSortable(true);
         table.setClass(Table.CLASS_ITS);
 
-        table.addColumn(new Column("id"));
-        table.addColumn(new Column("name"));
-        table.addColumn(new Column("age"));
-        table.addColumn(new Column("email"));
+        // Setup table paginator
+        table.setPageSize(4);
+        table.setPaginator(new TableInlinePaginator(table));
+        table.setPaginatorAttachment(ExportTable.PAGINATOR_INLINE);
+
+        Column column = new Column("name");
+        column.setWidth("140px;");
+        table.addColumn(column);
+
+        column = new Column("email");
+        column.setAutolink(true);
+        column.setWidth("230px;");
+        table.addColumn(column);
+
+        column = new Column("age");
+        column.setTextAlign("center");
+        column.setWidth("40px;");
+        table.addColumn(column);
 
-        Column column = new Column("holdings");
+        column = new Column("holdings");
         column.setFormat("{0,number,currency}");
         column.setTextAlign("right");
+        column.setWidth("100px;");
         table.addColumn(column);
 
         column = new Column("dateJoined");
         column.setFormat("{0,date,medium}");
+        column.setWidth("100px;");
         table.addColumn(column);
 
         column = new Column("action");
-        column.setTextAlign("center");
         AbstractLink[] links = new AbstractLink[] { editLink };
         editLink.setParameter("referrer", "/general/excel-table-export.htm");
         column.setDecorator(new LinkDecorator(table, links, "id"));
         column.setSortable(false);
         table.addColumn(column);
-
-        table.setPageSize(5);
-        table.setSortable(true);
-        table.setExportBannerPosition(Table.POSITION_BOTTOM);
-
-        // Exclude the action column from being exported
-        table.getExcludedColumns().add("action");
-        table.setExportAttachment(ExportTable.EXPORTER_INLINE);
-        table.setPaginator(new TableInlinePaginator(table));
-        table.setPaginatorAttachment(ExportTable.PAGINATOR_INLINE);
-
-        addControl(table);
-        addControl(editLink);
-    }
-
-    // --------------------------------------------------------- Event Handlers
-
-    /**
-     * @see Page#onRender()
-     */
-    @Override
-    public void onRender() {
-        List<Customer> list = customerService.getCustomersSortedByName(10);
-        table.setRowList(list);
     }
 }



Mime
View raw message