All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.msofficexml.docx.wordprocessingml.WmlParagraph Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.msofficexml.docx.wordprocessingml;

import java.net.URL;

import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.docx.Docx;
import com.hfg.xml.msofficexml.docx.DocxException;
import com.hfg.xml.msofficexml.docx.part.WmlContentPart;

//------------------------------------------------------------------------------
/**
 Represents an Office Open XML paragraph (<w:p>) tag.

 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------


public class WmlParagraph extends WmlXMLTag
{
   private WmlParagraphProperties mProperties;

   //###########################################################################
   // CONSTRUCTORS
   //###########################################################################

   //---------------------------------------------------------------------------
   public WmlParagraph(Docx inDocx)
   {
      super(WmlXML.P, inDocx);
   }

   //---------------------------------------------------------------------------
   public WmlParagraph(WmlContentPart inParentPart)
   {
      super(WmlXML.P, inParentPart);
   }

   //###########################################################################
   // PUBLIC METHODS
   //###########################################################################

   //---------------------------------------------------------------------------
   /**
    * Returns the paragraph properties tag if one exists or else instantiates a new one.
    * @return the paragraph properties for this table
    */
   public WmlParagraphProperties getProperties()
   {
      if (null == mProperties)
      {
         // Check if it has been added via addSubtag()...
         mProperties = getOptionalSubtagByName(WmlXML.PARAGRAPH_PROPS);
         if (null == mProperties)
         {
            mProperties = new WmlParagraphProperties(getParentDoc());
            addSubtag(0, mProperties);
         }
      }

      return mProperties;
   }

   //---------------------------------------------------------------------------
   public WmlTextRun addTextRun()
   {
      WmlTextRun run = new WmlTextRun(getParentDoc());
      addSubtag(run);

      return run;
   }

   //---------------------------------------------------------------------------
   public WmlTextRun addTextRun(String inContent)
   {
      WmlTextRun run = addTextRun();
      run.addText(inContent);

      return run;
   }

   //---------------------------------------------------------------------------
   // The comment itself is placed in the CommentsPart and a reference is retained.
   public void addComment(WmlParagraph inValue)
   {
      Docx docx = getParentDoc();
      if (null == docx)
      {
         throw new DocxException("No reference to the parent doc available!?");
      }

      int commentId = docx.getCommentsPart().addComment(inValue);

      XMLTag commentRefTag = new XMLTag(WmlXML.COMMENT_REF);
      commentRefTag.setAttribute(WmlXML.ID_ATT, commentId);

      addSubtag(commentRefTag);
   }

   //---------------------------------------------------------------------------
   public WmlHyperlink addHyperlink(URL inURL, String inName)
   {
      WmlHyperlink hyperlink = new WmlHyperlink(inURL, getParentDoc());

      hyperlink.addTextRun(inName);
      addSubtag(hyperlink);

      return hyperlink;
   }

   //---------------------------------------------------------------------------
   public WmlHyperlink addHyperlink(URL inURL)
   {
      WmlHyperlink hyperlink = new WmlHyperlink(inURL, getParentDoc());
      addSubtag(hyperlink);

      return hyperlink;
   }

   //---------------------------------------------------------------------------
   public WmlParagraph br()
   {
      return br(1);
   }

   //---------------------------------------------------------------------------
   public WmlParagraph br(int inCount)
   {
      for (int i = 0; i < inCount; i++)
      {
         addTextRun().br();
      }

      return this;
   }

   //---------------------------------------------------------------------------
   public WmlParagraph addPageBreak()
   {
      addTextRun().addPageBreak();

      return this;
   }

   //---------------------------------------------------------------------------
   public WmlBookmark startBookmark()
   {
      WmlBookmark bookmark = new WmlBookmark(getParentDoc());

      startBookmark(bookmark);

      return bookmark;
   }

   //---------------------------------------------------------------------------
   public void startBookmark(WmlBookmark inBookmark)
   {
      addSubtag(inBookmark.generateBookmarkStart());
   }

   //---------------------------------------------------------------------------
   public void endBookmark(WmlBookmark inBookmark)
   {
      addSubtag(inBookmark.generateBookmarkEnd());
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy