001/*
002 * $Id: IncTable.java 4632 2010-11-24 14:44:42Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text.html.simpleparser;
045
046import java.util.ArrayList;
047import java.util.Collections;
048import java.util.HashMap;
049import java.util.List;
050import java.util.Map;
051
052import com.itextpdf.text.Chunk;
053import com.itextpdf.text.Element;
054import com.itextpdf.text.ElementListener;
055import com.itextpdf.text.html.HtmlTags;
056import com.itextpdf.text.html.HtmlUtilities;
057import com.itextpdf.text.pdf.PdfPCell;
058import com.itextpdf.text.pdf.PdfPTable;
059
060/**
061 * We use a TableWrapper because PdfPTable is rather complex
062 * to put on the HTMLWorker stack.
063 * @author  psoares
064 * @since 5.0.6 (renamed)
065 */
066public class TableWrapper implements Element {
067        /**
068         * The styles that need to be applied to the table
069         * @since 5.0.6 renamed from props
070         */
071    private final Map<String, String> styles = new HashMap<String, String>();
072    /**
073     * Nested list containing the PdfPCell elements that are part of this table.
074     */
075    private final List<List<PdfPCell>> rows = new ArrayList<List<PdfPCell>>();
076
077    /**
078     * Array containing the widths of the columns.
079     * @since iText 5.0.6
080     */
081    private float[] colWidths;
082
083    /**
084     * Creates a new instance of IncTable.
085     * @param   attrs   a Map containing attributes
086     */
087    public TableWrapper(final Map<String, String> attrs) {
088        this.styles.putAll(attrs);
089    }
090
091    /**
092     * Adds a new row to the table.
093     * @param row a list of PdfPCell elements
094     */
095    public void addRow(List<PdfPCell> row) {
096        if (row != null) {
097            Collections.reverse(row);
098            rows.add(row);
099            row = null;
100        }
101    }
102
103    /**
104     * Setter for the column widths
105     * @since iText 5.0.6
106     */
107    public void setColWidths(final float[] colWidths) {
108        this.colWidths = colWidths;
109    }
110
111    /**
112     * Creates a new PdfPTable based on the info assembled
113     * in the table stub.
114     * @return  a PdfPTable
115     */
116    public PdfPTable createTable() {
117        // no rows = simplest table possible
118        if (rows.isEmpty())
119            return new PdfPTable(1);
120        // how many columns?
121        int ncol = 0;
122        for (PdfPCell pc : rows.get(0)) {
123            ncol += pc.getColspan();
124        }
125        PdfPTable table = new PdfPTable(ncol);
126        // table width
127        String width = styles.get(HtmlTags.WIDTH);
128        if (width == null)
129            table.setWidthPercentage(100);
130        else {
131            if (width.endsWith("%"))
132                table.setWidthPercentage(Float.parseFloat(width.substring(0, width.length() - 1)));
133            else {
134                table.setTotalWidth(Float.parseFloat(width));
135                table.setLockedWidth(true);
136            }
137        }
138        // horizontal alignment
139        String alignment = styles.get(HtmlTags.ALIGN);
140        int align = Element.ALIGN_LEFT;
141        if (alignment != null) {
142                align = HtmlUtilities.alignmentValue(alignment);
143        }
144        table.setHorizontalAlignment(align);
145        // column widths
146                try {
147                        if (colWidths != null)
148                                table.setWidths(colWidths);
149                } catch (Exception e) {
150                        // fail silently
151                }
152                // add the cells
153        for (List<PdfPCell> col : rows) {
154            for (PdfPCell pc : col) {
155                table.addCell(pc);
156            }
157        }
158        return table;
159    }
160
161    // these Element methods are irrelevant for a table stub.
162
163    /**
164     * @since 5.0.1
165     */
166    public List<Chunk> getChunks() {
167        return null;
168    }
169
170    /**
171     * @since 5.0.1
172     */
173    public boolean isContent() {
174        return false;
175    }
176
177    /**
178     * @since 5.0.1
179     */
180    public boolean isNestable() {
181        return false;
182    }
183
184    /**
185     * @since 5.0.1
186     */
187    public boolean process(final ElementListener listener) {
188        return false;
189    }
190
191    /**
192     * @since 5.0.1
193     */
194    public int type() {
195        return 0;
196    }
197
198}