001/*
002 * $Id: IncCell.java 4635 2010-11-28 17:38:03Z psoares33 $
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.List;
047
048import com.itextpdf.text.Chunk;
049import com.itextpdf.text.Element;
050import com.itextpdf.text.ElementListener;
051import com.itextpdf.text.Phrase;
052import com.itextpdf.text.TextElementArray;
053import com.itextpdf.text.html.HtmlTags;
054import com.itextpdf.text.html.HtmlUtilities;
055import com.itextpdf.text.pdf.PdfPCell;
056
057/**
058 * We use a CellWrapper because we need some extra info
059 * that isn't available in PdfPCell.
060 * @author  psoares
061 * @since 5.0.6 (renamed)
062 */
063public class CellWrapper implements TextElementArray {
064
065        /** The cell that is wrapped in this stub. */
066    private final PdfPCell cell;
067
068    /**
069     * The width of the cell.
070     * @since iText 5.0.6
071     */
072    private float width;
073
074    /**
075     * Indicates if the width is a percentage.
076     * @since iText 5.0.6
077     */
078    private boolean percentage;
079
080    /**
081     * Creates a new instance of IncCell.
082     * @param   tag             the cell that is wrapped in this object.
083     * @param   chain   properties such as width
084     * @since   5.0.6
085     */
086    public CellWrapper(final String tag, final ChainedProperties chain) {
087        this.cell = createPdfPCell(tag, chain);
088        String value = chain.getProperty(HtmlTags.WIDTH);
089        if (value != null) {
090            value = value.trim();
091                if (value.endsWith("%")) {
092                        percentage = true;
093                        value = value.substring(0, value.length() - 1);
094                }
095            width = Float.parseFloat(value);
096        }
097    }
098
099    /**
100     * Creates a PdfPCell element based on a tag and its properties.
101     * @param   tag             a cell tag
102     * @param   chain   the hierarchy chain
103     * @return the created PdfPCell
104     */
105        public PdfPCell createPdfPCell(final String tag, final ChainedProperties chain) {
106                PdfPCell cell = new PdfPCell((Phrase)null);
107        // colspan
108                String value = chain.getProperty(HtmlTags.COLSPAN);
109        if (value != null)
110            cell.setColspan(Integer.parseInt(value));
111        // rowspan
112        value = chain.getProperty(HtmlTags.ROWSPAN);
113        if (value != null)
114            cell.setRowspan(Integer.parseInt(value));
115        // horizontal alignment
116        if (tag.equals(HtmlTags.TH))
117            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
118        value = chain.getProperty(HtmlTags.ALIGN);
119        if (value != null) {
120            cell.setHorizontalAlignment(HtmlUtilities.alignmentValue(value));
121        }
122        // vertical alignment
123        value = chain.getProperty(HtmlTags.VALIGN);
124        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
125        if (value != null) {
126            cell.setVerticalAlignment(HtmlUtilities.alignmentValue(value));
127        }
128        // border
129        value = chain.getProperty(HtmlTags.BORDER);
130        float border = 0;
131        if (value != null)
132            border = Float.parseFloat(value);
133        cell.setBorderWidth(border);
134        // cellpadding
135        value = chain.getProperty(HtmlTags.CELLPADDING);
136        if (value != null)
137            cell.setPadding(Float.parseFloat(value));
138        cell.setUseDescender(true);
139        // background color
140        value = chain.getProperty(HtmlTags.BGCOLOR);
141        cell.setBackgroundColor(HtmlUtilities.decodeColor(value));
142        return cell;
143        }
144
145    /**
146     * Returns the PdfPCell.
147     * @return the PdfPCell
148     */
149    public PdfPCell getCell() {
150        return cell;
151    }
152
153    /**
154     * Getter for the cell width
155     * @return the width
156     * @since iText 5.0.6
157     */
158    public float getWidth() {
159        return width;
160    }
161
162    /**
163     * Getter for percentage
164     * @return true if the width is a percentage
165     * @since iText 5.0.6
166     */
167    public boolean isPercentage() {
168        return percentage;
169    }
170
171    /**
172     * Implements the add method of the TextElementArray interface.
173     * @param   o       an element that needs to be added to the cell.
174     */
175    public boolean add(final Element o) {
176        cell.addElement(o);
177        return true;
178    }
179
180    // these Element methods are irrelevant for a table stub.
181
182    /**
183     * @since 5.0.1
184     */
185    public List<Chunk> getChunks() {
186        return null;
187    }
188
189    /**
190     * @since 5.0.1
191     */
192    public boolean isContent() {
193        return false;
194    }
195
196    /**
197     * @since 5.0.1
198     */
199    public boolean isNestable() {
200        return false;
201    }
202
203    /**
204     * @since 5.0.1
205     */
206    public boolean process(final ElementListener listener) {
207        return false;
208    }
209
210    /**
211     * @since 5.0.1
212     */
213    public int type() {
214        return 0;
215    }
216}