📄 cell.java
字号:
/* * $Id: Cell.java,v 1.69 2002/11/19 08:33:32 blowagie Exp $ * $Name: $ * * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie. * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (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.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above. If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ */package com.lowagie.text;import java.awt.Color;import java.util.ArrayList;import java.util.Iterator;import java.util.Properties;import com.lowagie.text.markup.*;/** * A <CODE>Cell</CODE> is a <CODE>Rectangle</CODE> containing other * <CODE>Element</CODE>s. * <P> * A <CODE>Cell</CODE> must be added to a <CODE>Table</CODE>. * The <CODE>Table</CODE> will place the <CODE>Cell</CODE> in * a <CODE>Row</CODE>. * <P> * Example: * <BLOCKQUOTE><PRE> * Table table = new Table(3); * table.setBorderWidth(1); * table.setBorderColor(new Color(0, 0, 255)); * table.setCellpadding(5); * table.setCellspacing(5); * <STRONG>Cell cell = new Cell("header");</STRONG> * <STRONG>cell.setHeader(true);</STRONG> * <STRONG>cell.setColspan(3);</STRONG> * table.addCell(cell); * <STRONG>cell = new Cell("example cell with colspan 1 and rowspan 2");</STRONG> * <STRONG>cell.setRowspan(2);</STRONG> * <STRONG>cell.setBorderColor(new Color(255, 0, 0));</STRONG> * table.addCell(cell); * table.addCell("1.1"); * table.addCell("2.1"); * table.addCell("1.2"); * table.addCell("2.2"); * </PRE></BLOCKQUOTE> * * @see Rectangle * @see Element * @see Table * @see Row */public class Cell extends Rectangle implements TextElementArray { // static final membervariable/** This constant can be used as empty cell. */ public static final Cell EMPTY_CELL = new Cell(true);/** This constant can be used as empty cell. */ public static final Cell DUMMY_CELL = new Cell(true); static { DUMMY_CELL.setColspan(3); DUMMY_CELL.setBorder(NO_BORDER); } // membervariables/** This is the <CODE>ArrayList</CODE> of <CODE>Element</CODE>s. */ protected ArrayList arrayList = null;/** This is the horizontal alignment. */ protected int horizontalAlignment = Element.ALIGN_UNDEFINED;/** This is the vertical alignment. */ protected int verticalAlignment = Element.ALIGN_UNDEFINED;/** This is the vertical alignment. */ protected String width;/** This is the colspan. */ protected int colspan = 1;/** This is the rowspan. */ protected int rowspan = 1;/** This is the leading. */ float leading = Float.NaN;/** Is this <CODE>Cell</CODE> a header? */ protected boolean header;/** Will the element have to be wrapped? */ protected boolean noWrap; // constructors/** * Constructs an empty <CODE>Cell</CODE>. */ public Cell() { // creates a Rectangle with BY DEFAULT a border of 0.5 super(0, 0, 0, 0); setBorder(UNDEFINED); setBorderWidth(0.5f); // initializes the arraylist and adds an element arrayList = new ArrayList(); }/** * Constructs an empty <CODE>Cell</CODE> (for internal use only). * * @param dummy a dummy value */ public Cell(boolean dummy) { this(); arrayList.add(new Paragraph(0)); }/** * Constructs a <CODE>Cell</CODE> with a certain content. * <P> * The <CODE>String</CODE> will be converted into a <CODE>Paragraph</CODE>. * * @param content a <CODE>String</CODE> */ public Cell(String content) { // creates a Rectangle with BY DEFAULT a border of 0.5 super(0, 0, 0, 0); setBorder(UNDEFINED); setBorderWidth(0.5f); // initializes the arraylist and adds an element arrayList = new ArrayList(); try { addElement(new Paragraph(content)); } catch(BadElementException bee) { } }/** * Constructs a <CODE>Cell</CODE> with a certain <CODE>Element</CODE>. * <P> * if the element is a <CODE>ListItem</CODE>, <CODE>Row</CODE> or * <CODE>Cell</CODE>, an exception will be thrown. * * @param element the element * @throws BadElementException when the creator was called with a <CODE>ListItem</CODE>, <CODE>Row</CODE> or <CODE>Cell</CODE> */ public Cell(Element element) throws BadElementException { // creates a Rectangle with BY DEFAULT a border of 0.5 super(0, 0, 0, 0); setBorder(UNDEFINED); setBorderWidth(0.5f); try { Phrase p = (Phrase)element; leading = p.leading(); } catch(Exception e) { // empty on purpose } // initializes the arraylist and adds an element arrayList = new ArrayList(); addElement(element); }/** * Returns a <CODE>Cell</CODE> that has been constructed taking in account * the value of some <VAR>attributes</VAR>. * * @param attributes Some attributes */ public Cell(Properties attributes) { this(); String value; if ((value = (String)attributes.remove(ElementTags.HORIZONTALALIGN)) != null) { setHorizontalAlignment(value); } if ((value = (String)attributes.remove(ElementTags.VERTICALALIGN)) != null) { setVerticalAlignment(value); } if ((value = (String)attributes.remove(ElementTags.WIDTH)) != null) { setWidth(value); } if ((value = (String)attributes.remove(ElementTags.COLSPAN)) != null) { setColspan(Integer.parseInt(value)); } if ((value = (String)attributes.remove(ElementTags.ROWSPAN)) != null) { setRowspan(Integer.parseInt(value)); } if ((value = (String)attributes.remove(ElementTags.LEADING)) != null) { setLeading(Float.valueOf(value + "f").floatValue()); } if ((value = (String)attributes.remove(ElementTags.HEADER)) != null) { setHeader(new Boolean(value).booleanValue()); } if ((value = (String)attributes.remove(ElementTags.NOWRAP)) != null) { setNoWrap(new Boolean(value).booleanValue()); } if ((value = (String)attributes.remove(ElementTags.BORDERWIDTH)) != null) { setBorderWidth(Float.valueOf(value + "f").floatValue()); } int border = 0; if ((value = (String)attributes.remove(ElementTags.LEFT)) != null) { if (new Boolean(value).booleanValue()) border |= Rectangle.LEFT; } if ((value = (String)attributes.remove(ElementTags.RIGHT)) != null) { if (new Boolean(value).booleanValue()) border |= Rectangle.RIGHT; } if ((value = (String)attributes.remove(ElementTags.TOP)) != null) { if (new Boolean(value).booleanValue()) border |= Rectangle.TOP; } if ((value = (String)attributes.remove(ElementTags.BOTTOM)) != null) { if (new Boolean(value).booleanValue()) border |= Rectangle.BOTTOM; } setBorder(border); String r = (String)attributes.remove(ElementTags.RED); String g = (String)attributes.remove(ElementTags.GREEN); String b = (String)attributes.remove(ElementTags.BLUE); if (r != null || g != null || b != null) { int red = 0; int green = 0; int blue = 0; if (r != null) red = Integer.parseInt(r); if (g != null) green = Integer.parseInt(g); if (b != null) blue = Integer.parseInt(b); setBorderColor(new Color(red, green, blue)); } else if ((value = (String)attributes.remove(ElementTags.BORDERCOLOR)) != null) { setBorderColor(MarkupParser.decodeColor(value)); } r = (String)attributes.remove(ElementTags.BGRED); g = (String)attributes.remove(ElementTags.BGGREEN); b = (String)attributes.remove(ElementTags.BGBLUE); if (r != null || g != null || b != null) { int red = 0; int green = 0; int blue = 0; if (r != null) red = Integer.parseInt(r); if (g != null) green = Integer.parseInt(g); if (b != null) blue = Integer.parseInt(b); setBackgroundColor(new Color(red, green, blue)); } else if ((value = (String)attributes.remove(ElementTags.BACKGROUNDCOLOR)) != null) { setBackgroundColor(MarkupParser.decodeColor(value)); } if ((value = (String)attributes.remove(ElementTags.GRAYFILL)) != null) { setGrayFill(Float.valueOf(value + "f").floatValue()); } if (attributes.size() > 0) setMarkupAttributes(attributes); } // implementation of the Element-methods/** * Processes the element by adding it (or the different parts) to an * <CODE>ElementListener</CODE>. * * @param listener an <CODE>ElementListener</CODE> * @return <CODE>true</CODE> if the element was processed successfully */ public boolean process(ElementListener listener) { try { return listener.add(this); } catch(DocumentException de) { return false; } }/** * Gets the type of the text element. * * @return a type */ public int type() { return Element.CELL; }/** * Gets all the chunks in this element. * * @return an <CODE>ArrayList</CODE> */ public ArrayList getChunks() { ArrayList tmp = new ArrayList(); for (Iterator i = arrayList.iterator(); i.hasNext(); ) { tmp.addAll(((Element) i.next()).getChunks()); } return tmp; } // methods to set the membervariables/** * Adds an element to this <CODE>Cell</CODE>. * <P> * Remark: you can't add <CODE>ListItem</CODE>s, <CODE>Row</CODE>s, <CODE>Cell</CODE>s, * <CODE>JPEG</CODE>s, <CODE>GIF</CODE>s or <CODE>PNG</CODE>s to a <CODE>Cell</CODE>. * * @param element The <CODE>Element</CODE> to add * @throws BadElementException if the method was called with a <CODE>ListItem</CODE>, <CODE>Row</CODE> or <CODE>Cell</CODE> */ public void addElement(Element element) throws BadElementException { if (isTable()) { Table table = (Table) arrayList.get(0); Cell tmp = new Cell(element); tmp.setBorder(NO_BORDER); tmp.setColspan(table.columns()); table.addCell(tmp); return; } switch(element.type()) { case Element.LISTITEM: case Element.ROW: case Element.CELL: throw new BadElementException("You can't add listitems, rows or cells to a cell."); case Element.JPEG: case Element.IMGRAW: case Element.IMGTEMPLATE: case Element.GIF: case Element.PNG: arrayList.add(element); break; case Element.LIST: if (Float.isNaN(leading)) { leading = ((List) element).leading(); } if (((List) element).size() == 0) return; arrayList.add(element); return; case Element.ANCHOR: case Element.PARAGRAPH: case Element.PHRASE: if (Float.isNaN(leading)) { leading = ((Phrase) element).leading(); } if (((Phrase) element).isEmpty()) return; arrayList.add(element); return; case Element.CHUNK: if (((Chunk) element).isEmpty()) return; arrayList.add(element); return; case Element.TABLE: Table table = new Table(3); float[] widths = new float[3]; widths[1] = ((Table)element).widthPercentage(); switch(((Table)element).alignment()) { case Element.ALIGN_LEFT: widths[0] = 0f; widths[2] = 100f - widths[1]; break; case Element.ALIGN_CENTER: widths[0] = (100f - widths[1]) / 2f; widths[2] = widths[0]; break; case Element.ALIGN_RIGHT: widths[0] = 100f - widths[1]; widths[2] = 0f; } table.setWidths(widths); Cell tmp; if (arrayList.size() == 0) { table.addCell(DUMMY_CELL); } else { tmp = new Cell(); tmp.setBorder(NO_BORDER); tmp.setColspan(3); for (Iterator i = arrayList.iterator(); i.hasNext(); ) { tmp.add((Element) i.next()); } table.addCell(tmp); } tmp = new Cell(); tmp.setBorder(NO_BORDER); table.addCell(tmp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -