001package com.hfg.xml.msofficexml.docx.wordprocessingml; 002 003 004import com.hfg.util.collection.CollectionUtil; 005import com.hfg.xml.msofficexml.docx.Docx; 006 007//------------------------------------------------------------------------------ 008/** 009 Represents an Office Open XML table cell (<w:tc>) tag. 010 011 @author J. Alex Taylor, hairyfatguy.com 012 */ 013//------------------------------------------------------------------------------ 014// com.hfg XML/HTML Coding Library 015// 016// This library is free software; you can redistribute it and/or 017// modify it under the terms of the GNU Lesser General Public 018// License as published by the Free Software Foundation; either 019// version 2.1 of the License, or (at your option) any later version. 020// 021// This library is distributed in the hope that it will be useful, 022// but WITHOUT ANY WARRANTY; without even the implied warranty of 023// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 024// Lesser General Public License for more details. 025// 026// You should have received a copy of the GNU Lesser General Public 027// License along with this library; if not, write to the Free Software 028// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 029// 030// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 031// jataylor@hairyfatguy.com 032//------------------------------------------------------------------------------ 033 034public class WmlTableCell extends WmlXMLTag 035{ 036 private WmlTableCellProperties mTableCellProps; 037 private WmlParagraph mParagraph; 038 039 //--------------------------------------------------------------------------- 040 public WmlTableCell(Docx inDocx) 041 { 042 super(WmlXML.TABLE_CELL, inDocx); 043 // Ensure that the cell has a paragraph or Word will complain 044 getParagraph(); 045 } 046 047 //--------------------------------------------------------------------------- 048 public WmlParagraph getParagraph() 049 { 050 if (null == mParagraph) 051 { 052 // Check it it has been added via addSubtag()... 053 mParagraph = getOptionalSubtagByName(WmlXML.P); 054 if (null == mParagraph) 055 { 056 mParagraph = new WmlParagraph(getParentDoc()); 057 addSubtag(mParagraph); 058 } 059 } 060 061 return mParagraph; 062 } 063 064 //--------------------------------------------------------------------------- 065 public void addSubtag(WmlParagraph inParagraph) 066 { 067 // If the initial empty placehoder paragraph is present, remove it. 068 if (mParagraph != null 069 && ! CollectionUtil.hasValues(mParagraph.getSubtags())) 070 { 071 removeSubtag(mParagraph); 072 } 073 074 mParagraph = inParagraph; 075 super.addSubtag(mParagraph); 076 } 077 078 //--------------------------------------------------------------------------- 079 public WmlParagraph addParagraph() 080 { 081 WmlParagraph paragraph; 082 // If the initial empty placehoder paragraph is present, return it. 083 if (mParagraph != null 084 && ! CollectionUtil.hasValues(mParagraph.getSubtags())) 085 { 086 paragraph = mParagraph; 087 } 088 else 089 { 090 paragraph = new WmlParagraph(getParentDoc()); 091 super.addSubtag(paragraph); 092 093 if (null == mParagraph) 094 { 095 mParagraph = paragraph; 096 } 097 } 098 099 return paragraph; 100 } 101 102 //--------------------------------------------------------------------------- 103 public WmlTableCell setCellProperties(WmlTableCellProperties inValue) 104 { 105 mTableCellProps = inValue; 106 return this; 107 } 108 109 //--------------------------------------------------------------------------- 110 /** 111 * Returns the table cell properties tag if one exists or else instantiates a new one. 112 * @return the table cell properties for this cell 113 */ 114 public WmlTableCellProperties getCellProperties() 115 { 116 if (null == mTableCellProps) 117 { 118 // Check it it has been added via addSubtag()... 119 mTableCellProps = (WmlTableCellProperties) getOptionalSubtagByName(WmlXML.TABLE_CELL_PROPS); 120 if (null == mTableCellProps) 121 { 122 mTableCellProps = new WmlTableCellProperties(getParentDoc()); 123 addSubtag(mTableCellProps); 124 } 125 } 126 127 return mTableCellProps; 128 } 129}