com.hfg.xml.msofficexml.xlsx.spreadsheetml.style.SsmlFont 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.xlsx.spreadsheetml.style;
import com.hfg.graphics.ColorUtil;
import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.xlsx.ExcelIndexedColor;
import com.hfg.xml.msofficexml.xlsx.Xlsx;
import com.hfg.xml.msofficexml.xlsx.part.StylesPart;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXML;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXMLTag;
import java.awt.*;
//------------------------------------------------------------------------------
/**
Represents an Office Open XML spreadsheetml (<ssml:font>) 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 SsmlFont extends SsmlXMLTag
{
private Integer mIndex;
private XMLTag mSizeTag;
private XMLTag mNameTag;
private XMLTag mColorTag;
private XMLTag mBoldTag;
private XMLTag mItalicTag;
private XMLTag mShadowTag;
private XMLTag mStrikeThroughTag;
private XMLTag mTextVertAlignTag;
private XMLTag mUnderlineTag;
// TODO: Family tag
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//---------------------------------------------------------------------------
public SsmlFont(SsmlXMLTag inSsmlXMLTag)
{
this(inSsmlXMLTag.getParentDoc());
}
//---------------------------------------------------------------------------
public SsmlFont(Xlsx inXlsx)
{
this(inXlsx.getStylesPart());
}
//---------------------------------------------------------------------------
public SsmlFont(Font inFont, Xlsx inXlsx)
{
this(inXlsx);
setName(inFont.getName());
setSize(inFont.getSize());
if (inFont.isBold())
{
setBold(true);
}
if (inFont.isItalic())
{
setItalic(true);
}
}
//---------------------------------------------------------------------------
public SsmlFont(StylesPart inStylesPart)
{
super(SsmlXML.FONT, inStylesPart.getParentDoc());
// Register with the styles part
mIndex = inStylesPart.defineFont(this);
}
//---------------------------------------------------------------------------
public SsmlFont(Xlsx inXlsx, XMLTag inXMLTag)
{
super(SsmlXML.FONT, inXlsx);
XMLTag sizeTag = inXMLTag.getOptionalSubtagByName(SsmlXML.SIZE);
if (sizeTag != null)
{
mSizeTag = sizeTag;
}
XMLTag nameTag = inXMLTag.getOptionalSubtagByName(SsmlXML.NAME);
if (nameTag != null)
{
mNameTag = nameTag;
}
XMLTag colorTag = inXMLTag.getOptionalSubtagByName(SsmlXML.COLOR);
if (colorTag != null)
{
mColorTag = colorTag;
}
XMLTag boldTag = inXMLTag.getOptionalSubtagByName(SsmlXML.BOLD);
if (boldTag != null)
{
mBoldTag = boldTag;
}
XMLTag italicTag = inXMLTag.getOptionalSubtagByName(SsmlXML.ITALIC);
if (italicTag != null)
{
mItalicTag = italicTag;
}
XMLTag shadowTag = inXMLTag.getOptionalSubtagByName(SsmlXML.SHADOW);
if (shadowTag != null)
{
mShadowTag = shadowTag;
}
XMLTag strikeTag = inXMLTag.getOptionalSubtagByName(SsmlXML.STRIKE);
if (strikeTag != null)
{
mStrikeThroughTag = strikeTag;
}
XMLTag textVertTag = inXMLTag.getOptionalSubtagByName(SsmlXML.VERTICAL_ALIGN);
if (textVertTag != null)
{
mTextVertAlignTag = textVertTag;
}
XMLTag underlineTag = inXMLTag.getOptionalSubtagByName(SsmlXML.UNDERLINE);
if (underlineTag != null)
{
mUnderlineTag = underlineTag;
}
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//---------------------------------------------------------------------------
@Override
public SsmlFont clone()
{
SsmlFont clone = (SsmlFont) super.clone();
// These cached values will be repopulated if needed
clone.mSizeTag = null;
clone.mNameTag = null;
clone.mColorTag = null;
clone.mBoldTag = null;
clone.mItalicTag = null;
clone.mShadowTag = null;
clone.mStrikeThroughTag = null;
clone.mTextVertAlignTag = null;
clone.mUnderlineTag = null;
// Register with the styles part
clone.mIndex = getParentDoc().getStylesPart().defineFont(clone);
return clone;
}
//---------------------------------------------------------------------------
public Font toFont()
{
int style = (mBoldTag != null ? Font.BOLD : 0) + (mItalicTag != null ? Font.ITALIC : 0);
return new Font(getName(), style, getSize());
}
//---------------------------------------------------------------------------
public Integer getIndex()
{
return mIndex;
}
//---------------------------------------------------------------------------
public SsmlFont setSize(int inValue)
{
if (null == mSizeTag)
{
// Check it it has been added via addSubtag()...
mSizeTag = getOptionalSubtagByName(SsmlXML.SIZE);
if (null == mSizeTag)
{
mSizeTag = new XMLTag(SsmlXML.SIZE);
addSubtag(mSizeTag);
}
}
mSizeTag.setAttribute(SsmlXML.VALUE_ATT, inValue);
return this;
}
//---------------------------------------------------------------------------
public int getSize()
{
return (mSizeTag != null ? Integer.parseInt(mSizeTag.getAttributeValue(SsmlXML.VALUE_ATT)) : 0);
}
//---------------------------------------------------------------------------
public SsmlFont setBold(boolean inValue)
{
if (null == mBoldTag)
{
// Check it it has been added via addSubtag()...
mBoldTag = getOptionalSubtagByName(SsmlXML.BOLD);
}
if (inValue)
{
if (null == mBoldTag)
{
mBoldTag = new XMLTag(SsmlXML.BOLD);
addSubtag(mBoldTag);
}
}
else if (mBoldTag != null)
{
removeSubtag(mBoldTag);
mBoldTag = null;
}
return this;
}
//---------------------------------------------------------------------------
public SsmlFont setItalic(boolean inValue)
{
if (null == mItalicTag)
{
// Check it it has been added via addSubtag()...
mItalicTag = getOptionalSubtagByName(SsmlXML.ITALIC);
}
if (inValue)
{
if (null == mItalicTag)
{
mItalicTag = new XMLTag(SsmlXML.ITALIC);
addSubtag(mItalicTag);
}
}
else if (mItalicTag != null)
{
removeSubtag(mItalicTag);
mItalicTag = null;
}
return this;
}
//---------------------------------------------------------------------------
public SsmlFont setStrikeThrough(boolean inValue)
{
if (null == mStrikeThroughTag)
{
// Check it it has been added via addSubtag()...
mStrikeThroughTag = getOptionalSubtagByName(SsmlXML.STRIKE);
}
if (inValue)
{
if (null == mStrikeThroughTag)
{
mStrikeThroughTag = new XMLTag(SsmlXML.STRIKE);
addSubtag(mStrikeThroughTag);
}
}
else if (mStrikeThroughTag != null)
{
removeSubtag(mStrikeThroughTag);
mStrikeThroughTag = null;
}
return this;
}
//---------------------------------------------------------------------------
public SsmlFont setShadow(boolean inValue)
{
if (null == mShadowTag)
{
// Check it it has been added via addSubtag()...
mShadowTag = getOptionalSubtagByName(SsmlXML.SHADOW);
}
if (inValue)
{
if (null == mShadowTag)
{
mShadowTag = new XMLTag(SsmlXML.SHADOW);
addSubtag(mShadowTag);
}
}
else if (mShadowTag != null)
{
removeSubtag(mShadowTag);
mShadowTag = null;
}
return this;
}
//---------------------------------------------------------------------------
public SsmlFont setUnderline(SsmlUnderlineType inValue)
{
if (null == mUnderlineTag)
{
// Check it it has been added via addSubtag()...
mUnderlineTag = getOptionalSubtagByName(SsmlXML.UNDERLINE);
}
if (inValue != null)
{
if (null == mUnderlineTag)
{
mUnderlineTag = new XMLTag(SsmlXML.UNDERLINE);
addSubtag(mUnderlineTag);
}
mUnderlineTag.setAttribute(SsmlXML.VALUE_ATT, inValue.name());
}
else if (mUnderlineTag != null)
{
removeSubtag(mUnderlineTag);
mUnderlineTag = null;
}
return this;
}
//---------------------------------------------------------------------------
public SsmlFont setTextVerticalAlignment(SsmlTextVerticalAlignRunType inValue)
{
if (null == mTextVertAlignTag)
{
// Check it it has been added via addSubtag()...
mTextVertAlignTag = getOptionalSubtagByName(SsmlXML.VERTICAL_ALIGN);
}
if (inValue != null)
{
if (null == mTextVertAlignTag)
{
mTextVertAlignTag = new XMLTag(SsmlXML.VERTICAL_ALIGN);
addSubtag(mTextVertAlignTag);
}
mTextVertAlignTag.setAttribute(SsmlXML.VALUE_ATT, inValue.name());
}
else if (mTextVertAlignTag != null)
{
removeSubtag(mTextVertAlignTag);
mTextVertAlignTag = null;
}
return this;
}
//---------------------------------------------------------------------------
public SsmlFont setColor(Color inValue)
{
if (null == mColorTag)
{
// Check it it has been added via addSubtag()...
mColorTag = getOptionalSubtagByName(SsmlXML.COLOR);
}
if (inValue != null)
{
if (null == mColorTag)
{
mColorTag = new XMLTag(SsmlXML.COLOR);
addSubtag(mColorTag);
}
if (inValue instanceof ExcelIndexedColor)
{
mColorTag.setAttribute(SsmlXML.INDEXED_ATT, ((ExcelIndexedColor)inValue).getIndex());
}
else
{
mColorTag.setAttribute(SsmlXML.RGB_ATT, ColorUtil.colorToHex(inValue));
}
}
else if (mColorTag != null)
{
removeSubtag(mColorTag);
mColorTag = null;
}
return this;
}
//---------------------------------------------------------------------------
public SsmlFont setName(String inValue)
{
if (null == mNameTag)
{
// Check it it has been added via addSubtag()...
mNameTag = getOptionalSubtagByName(SsmlXML.NAME);
if (null == mNameTag)
{
mNameTag = new XMLTag(SsmlXML.NAME);
addSubtag(mNameTag);
}
}
mNameTag.setAttribute(SsmlXML.VALUE_ATT, inValue);
return this;
}
//---------------------------------------------------------------------------
public String getName()
{
return (mNameTag != null ? mNameTag.getAttributeValue(SsmlXML.VALUE_ATT) : null);
}
}