com.hfg.xml.msofficexml.docx.wordprocessingml.WmlTextRun Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.xml.msofficexml.docx.wordprocessingml;
import java.awt.Font;
import com.hfg.util.StringUtil;
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.drawingml.DmlGraphicData;
import com.hfg.xml.msofficexml.docx.wordprocessingml.field.WmlComplexField;
import com.hfg.xml.msofficexml.docx.wordprocessingml.field.WmlComplexFieldCharType;
import com.hfg.xml.msofficexml.part.MediaPart;
//------------------------------------------------------------------------------
/**
Represents an Office Open XML text run (<w:r>) 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 WmlTextRun extends WmlXMLTag
{
private WmlTextRunProperties mProperties;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public WmlTextRun(Docx inDocx)
{
super(WmlXML.R, inDocx);
}
//---------------------------------------------------------------------------
public WmlTextRun(Docx inDocx, String inContent)
{
this(inDocx);
addText(inContent);
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
/**
* Returns whether or not this text run has a run properties subtag.
*/
public boolean hasProperties()
{
return (getOptionalSubtagByName(WmlXML.RUN_PROPS) != null);
}
//---------------------------------------------------------------------------
public void setProperties(WmlTextRunProperties inValue)
{
mProperties = getOptionalSubtagByName(WmlXML.RUN_PROPS);
if (mProperties != null)
{
removeSubtag(mProperties);
}
mProperties = inValue;
if (inValue != null)
{
addSubtag(inValue);
}
}
//---------------------------------------------------------------------------
/**
* Returns the run properties tag if one exists or else instantiates a new one.
* @return the run properties for this table
*/
public WmlTextRunProperties getProperties()
{
if (null == mProperties)
{
// Check if it has been added via addSubtag()...
mProperties = getOptionalSubtagByName(WmlXML.RUN_PROPS);
if (null == mProperties)
{
mProperties = new WmlTextRunProperties(getParentDoc());
addSubtag(0, mProperties);
}
}
return mProperties;
}
//---------------------------------------------------------------------------
public WmlTextRun addText(String inText)
{
if (inText != null)
{
// Any tabs present in the content need to be handled
String[] pieces = inText.split("\t");
for (int i = 0; i < pieces.length; i++)
{
String piece = pieces[i];
if (piece.length() > 0)
{
addSubtag(new WmlText().setContent(piece));
}
if (i < pieces.length - 1)
{
addSubtag(new WmlTab(getParentDoc()));
}
}
}
return this;
}
//---------------------------------------------------------------------------
public void addSymbol(Font inFont, String inHexCode)
{
addSymbol(inFont.getName(), inHexCode);
}
//---------------------------------------------------------------------------
public void addSymbol(String inFontName, String inHexCode)
{
XMLTag symbolTag = new XMLTag(WmlXML.SYMBOL);
symbolTag.setAttribute(WmlXML.FONT_ATT, inFontName);
symbolTag.setAttribute(WmlXML.CHAR_ATT, inHexCode);
addSubtag(symbolTag);
}
/* TODO
//---------------------------------------------------------------------------
public WmlTextRun addImage(OutputStream inStream)
{
addSubtag(new WmlText().setContent(inText));
return this;
}
*/
//---------------------------------------------------------------------------
public void addMedia(MediaPart inMediaPart)
{
Docx docx = getParentDoc();
if (null == docx)
{
throw new DocxException("No reference to the parent doc available!?");
}
WmlInline inline = addDrawing().getInline();
inline.getDrawingObjNonVisualProperties().setName(inMediaPart.getFile().getName());
if (inMediaPart.getDimensions() != null)
{
inline.setWidth(inMediaPart.getDimensions().getWidth());
inline.setHeight(inMediaPart.getDimensions().getHeight());
}
//TODO: Temp
inline.getEffectExtent();
inline.getCommonDrawingMLNonVisualProperties().getGraphicFrameLocks().setDisallowAspectRatioChange(true);
DmlGraphicData graphicData = inline.getGraphic().getGraphicData();
graphicData.addMedia(inMediaPart);
}
//---------------------------------------------------------------------------
public WmlDrawing addDrawing()
{
WmlDrawing drawingTag = new WmlDrawing(getParentDoc());
addSubtag(drawingTag);
return drawingTag;
}
//---------------------------------------------------------------------------
public WmlTextRun br()
{
return br(1);
}
//---------------------------------------------------------------------------
public WmlTextRun br(int inCount)
{
for (int i = 0; i < inCount; i++)
{
addSubtag(new XMLTag(WmlXML.BR));
}
return this;
}
//---------------------------------------------------------------------------
public WmlTextRun addPageBreak()
{
XMLTag br = new XMLTag(WmlXML.BR);
br.setAttribute(WmlXML.TYPE_ATT, "page");
addSubtag(br);
return this;
}
//---------------------------------------------------------------------------
// 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 WmlComplexField addComplexField(WmlComplexFieldCharType inType)
{
WmlComplexField field = new WmlComplexField(getParentDoc(), inType);
addSubtag(field);
return field;
}
//---------------------------------------------------------------------------
public void addInstructionText(CharSequence inValue)
{
XMLTag tag = new XMLTag(WmlXML.INSTRUCTION_TEXT);
tag.setAttribute(WmlXML.SPACE_ATT, "preserve");
tag.setContent(inValue);
addSubtag(tag);
}
}