com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlRow 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;
import java.util.Collection;
import java.util.List;
import com.hfg.graphics.units.GfxSize;
import com.hfg.graphics.units.GfxUnits;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.xml.XMLAttribute;
import com.hfg.xml.XMLName;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLizable;
import com.hfg.xml.msofficexml.xlsx.CellRef;
//------------------------------------------------------------------------------
/**
Represents an Office Open XML worksheet data row (<ssml: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 SsmlRow extends SsmlXMLTag
{
private SsmlWorksheet mParentWorksheet;
// Cached values
private List mCells;
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//---------------------------------------------------------------------------
public SsmlRow(SsmlWorksheet inParentWorksheet)
{
super(SsmlXML.ROW, inParentWorksheet.getParentDoc());
mParentWorksheet = inParentWorksheet;
}
//---------------------------------------------------------------------------
public SsmlRow(SsmlWorksheet inParentWorksheet, XMLTag inXMLTag)
{
this(inParentWorksheet);
inXMLTag.verifyTagName(SsmlXML.ROW);
setRowIndex(Integer.parseInt(inXMLTag.getAttributeValue(SsmlXML.ROW_IDX_ATT)));
// Copy subtags over to this tag
if (CollectionUtil.hasValues(inXMLTag.getSubtags()))
{
for (XMLTag subtag : (List) (Object) inXMLTag.getSubtags())
{
if (subtag.getTagName().equals(SsmlXML.CELL.getLocalName()))
{
addCell(new SsmlCell(inParentWorksheet, subtag));
}
else
{
addSubtag(subtag);
}
}
}
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//---------------------------------------------------------------------------
public SsmlRow setRowIndex(int inValue)
{
setAttribute(SsmlXML.ROW_IDX_ATT, inValue);
return this;
}
//---------------------------------------------------------------------------
public Integer getRowIndex()
{
XMLAttribute attr = getAttribute(SsmlXML.ROW_IDX_ATT);
return attr != null ? Integer.parseInt(attr.getValue()) : null;
}
//---------------------------------------------------------------------------
public SsmlCell addCell()
{
String column = "A";
// How many cells do we already have?
CellRef maxCell = getMaxCellRef();
if (maxCell != null)
{
column = maxCell.nextCol();
}
// They might set the column later but we want to be sure that it has a value.
SsmlCell cell = new SsmlCell(mParentWorksheet).setRef(new CellRef(column + getRowIndex()));
addSubtag(cell);
clearCachedValues();
return cell;
}
//---------------------------------------------------------------------------
public SsmlCell setCell(int inColIndex, String inValue)
{
return addCellByColIndex(inColIndex).setValue(inValue);
}
//---------------------------------------------------------------------------
public SsmlCell addCellByColIndex(int inColIndex)
{
CellRef cellRef = new CellRef().setColIndex(inColIndex).setRowIndex(getRowIndex());
SsmlCell cell = new SsmlCell(mParentWorksheet).setRef(cellRef);
addCell(cell);
return cell;
}
//---------------------------------------------------------------------------
public void addCell(SsmlCell inValue)
{
int maxColIndex = 0;
CellRef maxCellRef = getMaxCellRef();
if (maxCellRef != null)
{
maxColIndex = maxCellRef.getColIndex();
}
if (null == inValue.getRef())
{
// No column specified. Add to the end of the row.
inValue.setRef(new CellRef().setColIndex(maxColIndex + 1).setRowIndex(getRowIndex()));
}
if (inValue.getRef().getColIndex() > maxColIndex)
{
// It's the highest value. Just add it to the end.
addSubtag(inValue);
}
else
{
List cells = getCells();
for (int i = 0; i < cells.size(); i++)
{
SsmlCell cell = cells.get(i);
if (cell.getRef().equals(inValue.getRef()))
{
// Replace this cell.
addSubtag(getContentPlusSubtagList().indexOf(cells.get(i)), inValue);
removeSubtag(cell);
break;
}
else if (cell.getRef().getColIndex() > inValue.getRef().getColIndex())
{
// Insert before this cell.
addSubtag(getContentPlusSubtagList().indexOf(cells.get(i)), inValue);
break;
}
}
}
clearCachedValues();
}
//---------------------------------------------------------------------------
public SsmlCell addCell(Object inValue)
{
return addCell().setValue(inValue);
}
//---------------------------------------------------------------------------
public SsmlCell addCell(SsmlTextRun inValue)
{
return addCell().setValue(inValue);
}
//---------------------------------------------------------------------------
public SsmlCell addCell(List inValue)
{
return addCell().setValue(inValue);
}
//---------------------------------------------------------------------------
public SsmlCell addCell(String inValue)
{
return addCell().setValue(inValue);
}
//---------------------------------------------------------------------------
public SsmlCell addCell(int inValue)
{
return addCell().setValue(inValue);
}
//---------------------------------------------------------------------------
public SsmlCell addCell(long inValue)
{
return addCell().setValue(inValue);
}
//---------------------------------------------------------------------------
public SsmlCell addCell(float inValue)
{
return addCell().setValue(inValue);
}
//---------------------------------------------------------------------------
public SsmlCell addCell(double inValue)
{
return addCell().setValue(inValue);
}
//---------------------------------------------------------------------------
public List getCells()
{
if (null == mCells)
{
mCells = (List) (Object) getSubtagsByName(SsmlXML.CELL);
}
return mCells;
}
//---------------------------------------------------------------------------
/**
* Returns whether or not any cell in this row contains content.
* @return whether or not any cell in this row contains content
*/
public boolean isEmpty()
{
return null == getFirstCellWithContent();
}
//---------------------------------------------------------------------------
/**
* Returns the first cell in this row that contains content.
* @return the first cell in this row that contains content
*/
public SsmlCell getFirstCellWithContent()
{
SsmlCell firstCellWithContent = null;
List cells = getCells();
if (CollectionUtil.hasValues(cells))
{
for (SsmlCell cell : cells)
{
Object value = cell.getValue();
if (value != null
&& StringUtil.isSet(value.toString()))
{
firstCellWithContent = cell;
break;
}
}
}
return firstCellWithContent;
}
//---------------------------------------------------------------------------
public SsmlCell getCellByColIndex(int inColIndex)
{
SsmlCell requestedCell = null;
if (CollectionUtil.hasValues(getCells()))
{
for (SsmlCell cell : getCells())
{
if (cell.getRef().getColIndex() == inColIndex)
{
requestedCell = cell;
break;
}
}
}
return requestedCell;
}
//---------------------------------------------------------------------------
public CellRef getMinCellRef()
{
// How many cells do we already have?
CellRef cellRef = null;
List cellTags = getSubtagsByName(SsmlXML.CELL);
if (CollectionUtil.hasValues(cellTags))
{
XMLTag firstCell = cellTags.get(0);
XMLAttribute attr = firstCell.getAttribute(SsmlXML.REF_ATT);
if (attr != null)
{
cellRef = new CellRef(attr.getValue());
}
}
return cellRef;
}
//---------------------------------------------------------------------------
public CellRef getMaxCellRef()
{
// How many cells do we already have?
CellRef cellRef = null;
List cellTags = getSubtagsByName(SsmlXML.CELL);
if (CollectionUtil.hasValues(cellTags))
{
XMLTag lastCell = cellTags.get(cellTags.size() - 1);
XMLAttribute attr = lastCell.getAttribute(SsmlXML.REF_ATT);
if (attr != null)
{
cellRef = new CellRef(attr.getValue());
}
}
return cellRef;
}
//---------------------------------------------------------------------------
public SsmlRow setHeight(GfxSize inValue)
{
setAttribute(SsmlXML.HEIGHT_ATT, inValue.to(GfxUnits.points));
setAttribute(SsmlXML.CUSTOM_HEIGHT_ATT, "1");
return this;
}
//---------------------------------------------------------------------------
@Override
public synchronized void addSubtags(Collection extends XMLizable> inSubtags)
{
clearCachedValues();
super.addSubtags(inSubtags);
}
//---------------------------------------------------------------------------
@Override
public void addSubtag(XMLizable inSubtag)
{
clearCachedValues();
super.addSubtag(inSubtag);
}
//---------------------------------------------------------------------------
@Override
public void addSubtag(int inIndex, XMLizable inSubtag)
{
clearCachedValues();
super.addSubtag(inIndex, inSubtag);
}
//--------------------------------------------------------------------------
@Override
public XMLTag addSubtag(String inTagName)
{
clearCachedValues();
return super.addSubtag(inTagName);
}
//--------------------------------------------------------------------------
@Override
public XMLTag addSubtag(XMLName inTagName)
{
clearCachedValues();
return super.addSubtag(inTagName);
}
//---------------------------------------------------------------------------
private void clearCachedValues()
{
mCells = null;
}
}