com.hfg.xml.msofficexml.xlsx.spreadsheetml.style.SsmlFill 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.html.attribute.HTMLColor;
import com.hfg.graphics.ColorUtil;
import com.hfg.util.StringUtil;
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.Color;
//------------------------------------------------------------------------------
/**
Represents an Office Open XML spreadsheetml (<ssml:fill>) 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 SsmlFill extends SsmlXMLTag
{
private Integer mIndex;
private XMLTag mPatternFillTag;
private XMLTag mFgColorTag;
private XMLTag mBgColorTag;
//---------------------------------------------------------------------------
public SsmlFill(SsmlXMLTag inSsmlXMLTag)
{
this(inSsmlXMLTag.getParentDoc());
}
//---------------------------------------------------------------------------
public SsmlFill(Xlsx inXlsx)
{
this(inXlsx.getStylesPart());
}
//---------------------------------------------------------------------------
public SsmlFill(StylesPart inStylesPart)
{
super(SsmlXML.FILL, inStylesPart.getParentDoc());
// Register with the styles part
mIndex = inStylesPart.defineFill(this);
// Default to a solid pattern
setPatternType(SsmlPatternType.solid);
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//---------------------------------------------------------------------------
@Override
public SsmlFill clone()
{
SsmlFill clone = (SsmlFill) super.clone();
// These cached values will be repopulated if needed
clone.mPatternFillTag = null;
clone.mFgColorTag = null;
clone.mBgColorTag = null;
// Register with the styles part
clone.mIndex = getParentDoc().getStylesPart().defineFill(clone);
return clone;
}
//---------------------------------------------------------------------------
public Integer getIndex()
{
return mIndex;
}
//---------------------------------------------------------------------------
public SsmlFill setPatternType(SsmlPatternType inValue)
{
XMLTag patternFillTag = getPatternFillTag();
if (inValue != null)
{
patternFillTag.setAttribute(SsmlXML.PATTERN_TYPE_ATT, inValue);
}
else
{
patternFillTag.removeAttribute(SsmlXML.PATTERN_TYPE_ATT.getLocalName());
}
return this;
}
//---------------------------------------------------------------------------
public SsmlPatternType getPatternType()
{
SsmlPatternType patternType = null;
if (mPatternFillTag != null)
{
String patternTypeString = mPatternFillTag.getAttributeValue(SsmlXML.PATTERN_TYPE_ATT);
if (StringUtil.isSet(patternTypeString))
{
patternType = SsmlPatternType.valueOf(patternTypeString);
}
}
return patternType;
}
//---------------------------------------------------------------------------
public SsmlFill setForegroundColor(Color inValue)
{
if (inValue != null)
{
if (null == mFgColorTag)
{
// Check it it has been added via addSubtag()...
XMLTag patternFillTag = getPatternFillTag();
mFgColorTag = patternFillTag.getOptionalSubtagByName(SsmlXML.FG_COLOR);
if (null == mFgColorTag)
{
mFgColorTag = new XMLTag(SsmlXML.FG_COLOR);
patternFillTag.addSubtag(mFgColorTag);
}
}
if (inValue instanceof ExcelIndexedColor)
{
mFgColorTag.setAttribute(SsmlXML.INDEXED_ATT, ((ExcelIndexedColor)inValue).getIndex());
}
else
{
mFgColorTag.setAttribute(SsmlXML.RGB_ATT, ColorUtil.colorToHex(inValue).toUpperCase());
}
}
else
{
getPatternFillTag().removeSubtagsByName(SsmlXML.FG_COLOR);
}
return this;
}
//---------------------------------------------------------------------------
public Color getForegroundColor()
{
Color color = null;
if (mFgColorTag != null)
{
String rgbString = mFgColorTag.getAttributeValue(SsmlXML.RGB_ATT);
if (StringUtil.isSet(rgbString))
{
color = HTMLColor.valueOf(rgbString);
}
else
{
String index = mFgColorTag.getAttributeValue(SsmlXML.INDEXED_ATT);
if (StringUtil.isSet(index))
{
color = ExcelIndexedColor.valueOf(index);
}
}
}
return color;
}
//---------------------------------------------------------------------------
public SsmlFill setBackgroundColor(Color inValue)
{
if (inValue != null)
{
if (null == mBgColorTag)
{
// Check it it has been added via addSubtag()...
XMLTag patternFillTag = getPatternFillTag();
mBgColorTag = patternFillTag.getOptionalSubtagByName(SsmlXML.BG_COLOR);
if (null == mBgColorTag)
{
mBgColorTag = new XMLTag(SsmlXML.BG_COLOR);
patternFillTag.addSubtag(mBgColorTag);
}
}
if (inValue instanceof ExcelIndexedColor)
{
mBgColorTag.setAttribute(SsmlXML.INDEXED_ATT, ((ExcelIndexedColor)inValue).getIndex());
}
else
{
mBgColorTag.setAttribute(SsmlXML.RGB_ATT, ColorUtil.colorToHex(inValue).toUpperCase());
}
}
else
{
getPatternFillTag().removeSubtagsByName(SsmlXML.BG_COLOR);
}
return this;
}
//---------------------------------------------------------------------------
public Color getBackgroundColor()
{
Color color = null;
if (mBgColorTag != null)
{
String rgbString = mBgColorTag.getAttributeValue(SsmlXML.RGB_ATT);
if (StringUtil.isSet(rgbString))
{
color = HTMLColor.valueOf(rgbString);
}
else
{
String index = mBgColorTag.getAttributeValue(SsmlXML.INDEXED_ATT);
if (StringUtil.isSet(index))
{
color = ExcelIndexedColor.valueOf(index);
}
}
}
return color;
}
//---------------------------------------------------------------------------
private XMLTag getPatternFillTag()
{
if (null == mPatternFillTag)
{
// Check it it has been added via addSubtag()...
mPatternFillTag = getOptionalSubtagByName(SsmlXML.PATTERN_FILL);
if (null == mPatternFillTag)
{
mPatternFillTag = new XMLTag(SsmlXML.PATTERN_FILL);
addSubtag(mPatternFillTag);
}
}
return mPatternFillTag;
}
}