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 java.awt.Color;
import java.awt.Font;
import com.hfg.exception.UnmodifyableObjectException;
import com.hfg.graphics.ColorUtil;
import com.hfg.html.attribute.HTMLColor;
import com.hfg.util.CompareUtil;
import com.hfg.util.StringBuilderPlus;
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.spreadsheetml.SsmlXML;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXMLTag;
//------------------------------------------------------------------------------
/**
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 implements Comparable
{
private int mIndex = -1;
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
protected boolean mLocked;
//##########################################################################
// 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
//##########################################################################
//---------------------------------------------------------------------------
public String toString()
{
StringBuilderPlus buffer = new StringBuilderPlus(getName()).setDelimiter("-");
if (isBold())
{
buffer.delimitedAppend("BOLD");
if (isItalic())
{
buffer.append("ITALIC");
}
}
else if (isItalic())
{
buffer.delimitedAppend("ITALIC");
}
else
{
buffer.delimitedAppend("PLAIN");
}
buffer.delimitedAppend(getSize());
return buffer.toString();
}
//--------------------------------------------------------------------------
@Override
public boolean equals(Object inObj2)
{
return (0 == compareTo(inObj2));
}
//--------------------------------------------------------------------------
@Override
public int compareTo(Object inObj2)
{
int result = -1;
if (inObj2 instanceof SsmlFont)
{
SsmlFont font2 = (SsmlFont) inObj2;
result = CompareUtil.compare(getName(), font2.getName());
if (0 == result)
{
result = CompareUtil.compare(getSize(), font2.getSize());
if (0 == result)
{
result = CompareUtil.compare(getColor(), font2.getColor());
if (0 == result)
{
result = CompareUtil.compare(isBold(), font2.isBold());
if (0 == result)
{
result = CompareUtil.compare(isItalic(), font2.isItalic());
if (0 == result)
{
result = CompareUtil.compare(hasShadow(), font2.hasShadow());
if (0 == result)
{
result = CompareUtil.compare(hasStrikeThrough(), font2.hasStrikeThrough());
if (0 == result)
{
result = CompareUtil.compare(getTextVerticalAlignment(), font2.getTextVerticalAlignment());
if (0 == result)
{
result = CompareUtil.compare(getUnderline(), font2.getUnderline());
}
}
}
}
}
}
}
}
}
return result;
}
//--------------------------------------------------------------------------
public boolean isLocked()
{
return mLocked;
}
//--------------------------------------------------------------------------
public SsmlFont lock()
{
mLocked = true;
return this;
}
//---------------------------------------------------------------------------
@Override
public SsmlFont clone()
{
SsmlFont clone = (SsmlFont) super.clone();
// The clone should be unlocked (modifiable)
clone.mLocked = false;
// These cached values will be repopulated if needed
clone.clearSubtags();
clone.mNameTag = null;
clone.mSizeTag = null;
clone.mColorTag = null;
clone.mBoldTag = null;
clone.mItalicTag = null;
clone.mShadowTag = null;
clone.mUnderlineTag = null;
clone.mTextVertAlignTag = null;
if (getName() != null)
{
clone.setName(getName());
}
if (mSizeTag != null)
{
clone.setSize(getSize());
}
if (getColor() != null)
{
clone.setColor(getColor());
}
if (isBold())
{
clone.setBold(true);
}
if (isItalic())
{
clone.setItalic(true);
}
if (hasShadow())
{
clone.setShadow(true);
}
if (hasStrikeThrough())
{
clone.setStrikeThrough(true);
}
if (getUnderline() != null)
{
clone.setUnderline(getUnderline());
}
if (getTextVerticalAlignment() != null)
{
clone.setTextVerticalAlignment(getTextVerticalAlignment());
}
// 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());
}
//---------------------------------------------------------------------------
/**
Only for use by the StylesPart when setting a new value for the default font.
* @param inValue the new index value
* @return this object to enable chaining
*/
protected SsmlFont setIndex(int inValue)
{
mIndex = inValue;
return this;
}
//---------------------------------------------------------------------------
public int getIndex()
{
return mIndex;
}
//---------------------------------------------------------------------------
public SsmlFont setSize(int inValue)
{
checkLock();
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 boolean isBold()
{
return mBoldTag != null;
}
//---------------------------------------------------------------------------
public SsmlFont setBold(boolean inValue)
{
checkLock();
if (null == mBoldTag)
{
// Check if 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 boolean isItalic()
{
return mItalicTag != null;
}
//---------------------------------------------------------------------------
public SsmlFont setItalic(boolean inValue)
{
checkLock();
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 boolean hasStrikeThrough()
{
if (null == mStrikeThroughTag)
{
// Check if it has been added via addSubtag()...
mStrikeThroughTag = getOptionalSubtagByName(SsmlXML.STRIKE);
}
return (mStrikeThroughTag != null);
}
//---------------------------------------------------------------------------
public SsmlFont setStrikeThrough(boolean inValue)
{
checkLock();
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 boolean hasShadow()
{
if (null == mShadowTag)
{
// Check if it has been added via addSubtag()...
mShadowTag = getOptionalSubtagByName(SsmlXML.SHADOW);
}
return (mShadowTag != null);
}
//---------------------------------------------------------------------------
public SsmlFont setShadow(boolean inValue)
{
checkLock();
if (null == mShadowTag)
{
// Check if 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 SsmlUnderlineType getUnderline()
{
if (null == mUnderlineTag)
{
// Check if it has been added via addSubtag()...
mUnderlineTag = getOptionalSubtagByName(SsmlXML.UNDERLINE);
}
return (mUnderlineTag != null ? SsmlUnderlineType.valueOf(mUnderlineTag.getAttributeValue(SsmlXML.VALUE_ATT)) : null);
}
//---------------------------------------------------------------------------
public SsmlFont setUnderline(SsmlUnderlineType inValue)
{
checkLock();
if (null == mUnderlineTag)
{
// Check if 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)
{
checkLock();
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 SsmlTextVerticalAlignRunType getTextVerticalAlignment()
{
SsmlTextVerticalAlignRunType value = null;
if (null == mTextVertAlignTag)
{
// Check if it has been added via addSubtag()...
mTextVertAlignTag = getOptionalSubtagByName(SsmlXML.VERTICAL_ALIGN);
}
if (mTextVertAlignTag != null)
{
value = SsmlTextVerticalAlignRunType.valueOf(mTextVertAlignTag.getAttributeValue(SsmlXML.VALUE_ATT));
}
return value;
}
//---------------------------------------------------------------------------
public SsmlFont setColor(Color inValue)
{
checkLock();
if (null == mColorTag)
{
// Check if 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 Color getColor()
{
Color value = null;
if (null == mColorTag)
{
// Check if it has been added via addSubtag()...
mColorTag = getOptionalSubtagByName(SsmlXML.COLOR);
}
if (mColorTag != null)
{
if (mColorTag.hasAttribute(SsmlXML.RGB_ATT))
{
value = HTMLColor.valueOf("#" + mColorTag.getAttributeValue(SsmlXML.RGB_ATT));
}
else if (mColorTag.hasAttribute(SsmlXML.INDEXED_ATT))
{
value = ExcelIndexedColor.valueOf(Integer.parseInt(mColorTag.getAttributeValue(SsmlXML.INDEXED_ATT)));
}
}
return value;
}
//---------------------------------------------------------------------------
public SsmlFont setName(String inValue)
{
checkLock();
if (null == mNameTag)
{
// Check if 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);
}
//##########################################################################
// PRIVATE METHODS
//##########################################################################
//---------------------------------------------------------------------------
private void checkLock()
{
if (isLocked())
{
throw new UnmodifyableObjectException("This " + getClass().getSimpleName() + " object is locked and cannot be modified!");
}
}
}