001package com.hfg.xml.msofficexml.docx.wordprocessingml; 002 003import java.net.URL; 004 005import com.hfg.xml.XMLTag; 006import com.hfg.xml.msofficexml.docx.Docx; 007import com.hfg.xml.msofficexml.docx.DocxException; 008import com.hfg.xml.msofficexml.docx.part.WmlContentPart; 009 010//------------------------------------------------------------------------------ 011/** 012 Represents an Office Open XML paragraph (<w:p>) tag. 013 014 @author J. Alex Taylor, hairyfatguy.com 015 */ 016//------------------------------------------------------------------------------ 017// com.hfg XML/HTML Coding Library 018// 019// This library is free software; you can redistribute it and/or 020// modify it under the terms of the GNU Lesser General Public 021// License as published by the Free Software Foundation; either 022// version 2.1 of the License, or (at your option) any later version. 023// 024// This library is distributed in the hope that it will be useful, 025// but WITHOUT ANY WARRANTY; without even the implied warranty of 026// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 027// Lesser General Public License for more details. 028// 029// You should have received a copy of the GNU Lesser General Public 030// License along with this library; if not, write to the Free Software 031// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 032// 033// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 034// jataylor@hairyfatguy.com 035//------------------------------------------------------------------------------ 036 037 038public class WmlParagraph extends WmlXMLTag 039{ 040 private WmlParagraphProperties mProperties; 041 042 //########################################################################### 043 // CONSTRUCTORS 044 //########################################################################### 045 046 //--------------------------------------------------------------------------- 047 public WmlParagraph(Docx inDocx) 048 { 049 super(WmlXML.P, inDocx); 050 } 051 052 //--------------------------------------------------------------------------- 053 public WmlParagraph(WmlContentPart inParentPart) 054 { 055 super(WmlXML.P, inParentPart); 056 } 057 058 //########################################################################### 059 // PUBLIC METHODS 060 //########################################################################### 061 062 //--------------------------------------------------------------------------- 063 /** 064 * Returns the paragraph properties tag if one exists or else instantiates a new one. 065 * @return the paragraph properties for this table 066 */ 067 public WmlParagraphProperties getProperties() 068 { 069 if (null == mProperties) 070 { 071 // Check if it has been added via addSubtag()... 072 mProperties = getOptionalSubtagByName(WmlXML.PARAGRAPH_PROPS); 073 if (null == mProperties) 074 { 075 mProperties = new WmlParagraphProperties(getParentDoc()); 076 addSubtag(0, mProperties); 077 } 078 } 079 080 return mProperties; 081 } 082 083 //--------------------------------------------------------------------------- 084 public WmlTextRun addTextRun() 085 { 086 WmlTextRun run = new WmlTextRun(getParentDoc()); 087 addSubtag(run); 088 089 return run; 090 } 091 092 //--------------------------------------------------------------------------- 093 public WmlTextRun addTextRun(String inContent) 094 { 095 WmlTextRun run = addTextRun(); 096 run.addText(inContent); 097 098 return run; 099 } 100 101 //--------------------------------------------------------------------------- 102 // The comment itself is placed in the CommentsPart and a reference is retained. 103 public void addComment(WmlParagraph inValue) 104 { 105 Docx docx = getParentDoc(); 106 if (null == docx) 107 { 108 throw new DocxException("No reference to the parent doc available!?"); 109 } 110 111 int commentId = docx.getCommentsPart().addComment(inValue); 112 113 XMLTag commentRefTag = new XMLTag(WmlXML.COMMENT_REF); 114 commentRefTag.setAttribute(WmlXML.ID_ATT, commentId); 115 116 addSubtag(commentRefTag); 117 } 118 119 //--------------------------------------------------------------------------- 120 public WmlHyperlink addHyperlink(URL inURL, String inName) 121 { 122 WmlHyperlink hyperlink = new WmlHyperlink(inURL, getParentDoc()); 123 124 hyperlink.addTextRun(inName); 125 addSubtag(hyperlink); 126 127 return hyperlink; 128 } 129 130 //--------------------------------------------------------------------------- 131 public WmlHyperlink addHyperlink(URL inURL) 132 { 133 WmlHyperlink hyperlink = new WmlHyperlink(inURL, getParentDoc()); 134 addSubtag(hyperlink); 135 136 return hyperlink; 137 } 138 139 //--------------------------------------------------------------------------- 140 public WmlParagraph br() 141 { 142 return br(1); 143 } 144 145 //--------------------------------------------------------------------------- 146 public WmlParagraph br(int inCount) 147 { 148 for (int i = 0; i < inCount; i++) 149 { 150 addTextRun().br(); 151 } 152 153 return this; 154 } 155 156 //--------------------------------------------------------------------------- 157 public WmlParagraph addPageBreak() 158 { 159 addTextRun().addPageBreak(); 160 161 return this; 162 } 163 164 //--------------------------------------------------------------------------- 165 public WmlBookmark startBookmark() 166 { 167 WmlBookmark bookmark = new WmlBookmark(getParentDoc()); 168 169 startBookmark(bookmark); 170 171 return bookmark; 172 } 173 174 //--------------------------------------------------------------------------- 175 public void startBookmark(WmlBookmark inBookmark) 176 { 177 addSubtag(inBookmark.generateBookmarkStart()); 178 } 179 180 //--------------------------------------------------------------------------- 181 public void endBookmark(WmlBookmark inBookmark) 182 { 183 addSubtag(inBookmark.generateBookmarkEnd()); 184 } 185 186}