/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package reportgen.ren.report.extendedformat.cell;
import reportgen.ren.report.extendedformat.range.ColRowRange;
import org.jdom.Element;
import reportgen.prototype.context.Context;
import reportgen.utils.ReportException;
import reportgen.math.ExpressionContainer;
import reportgen.math.complex.generic.MathExpressionGeneric;
import reportgen.utils.SupportXMLRootImpl;
import reportgen.math.agregate.agregate.AggregateFunction;
import reportgen.ren.report.extendedformat.StyleAdaptor;
import reportgen.utils.Atom;
import simplesheet.style.Style;
import simplesheet.model.cell.StyleDefault;
/**
*
* @author axe
*/
public class CellValue extends SupportXMLRootImpl implements ExpressionContainer {
protected static final String TAG = "cell";
protected static final String ATTR_COL = "col";
protected static final String ATTR_ROW = "row";
protected static final String ATTR_FUNC = "func";
private final ColRowRange colReference;
private final ColRowRange rowReference;
private MathExpressionGeneric value;
private AggregateFunction function = AggregateFunction.ASIS;
private final ContextSum context;
private Style style;
public CellValue(ColRowRange colReference, ColRowRange rowReference) {
assert colReference != null && rowReference != null;
this.colReference = colReference;
this.rowReference = rowReference;
context = new ContextSum(colReference.getLocalContext(),
new Context[] {colReference.getLocalContext(), rowReference.getLocalContext()});
this.value = new MathExpressionGeneric(context);
this.style = new StyleDefault();
}
/**
*
* @param el
* @param prototype
* @return
* @throws reportgen.exception.ReportException
*/
public CellValue(Element element, Context aContext) throws ReportException {
assert element.getName().equals(TAG);
colReference = aContext.getRange(new Atom(getStringAttribute(element, ATTR_COL)));
rowReference = aContext.getRange(new Atom(getStringAttribute(element, ATTR_ROW)));
context = new ContextSum(colReference.getLocalContext(),
new Context[] {colReference.getLocalContext(), rowReference.getLocalContext()});
restoreState(element);
}
public void restoreState(Element element) throws ReportException {
MathExpressionGeneric aValue;
Element child = element.getChild(MathExpressionGeneric.TAG);
if(child != null) {
aValue = new MathExpressionGeneric(child, context);
} else {
aValue = new MathExpressionGeneric(context);
}
AggregateFunction aFunction = null;
String mnemonic = getStringAttribute(element, ATTR_FUNC, null, false);
if(mnemonic == null) {
aFunction = AggregateFunction.ASIS;
} else {
aFunction = context.getAggregFunction(mnemonic, aValue.getCls());
}
style = new StyleAdaptor(element).getStyle();
value = aValue;
function = aFunction;
}
@Override
protected void toXML(Element root) {
root.setAttribute(ATTR_COL, String.valueOf(colReference.getAtom()));
root.setAttribute(ATTR_ROW, String.valueOf(rowReference.getAtom()));
saveState(root);
}
public void saveState(Element root) {
if(function != AggregateFunction.ASIS) {
root.setAttribute(ATTR_FUNC, function.getMnemonic());
}
Element styleElement = new StyleAdaptor(style).toXML();
if(styleElement != null) {
root.addContent(styleElement);
}
if(!value.isEmpty()) {
root.addContent(value.toXML());
}
}
@Override
protected String getRootTag() {
return TAG;
}
public Style getStyle() {
return style;
}
CellValue cloneCell(ColRowRange colReference, ColRowRange rowReference) {
throw new RuntimeException();
//return new CellValue(colReference, rowReference, value, function);
}
public Context getContext() {
return context;
}
public MathExpressionGeneric getValue() {
return value;
}
public ColRowRange getColReference() {
return colReference;
}
public ColRowRange getRowReference() {
return rowReference;
}
public AggregateFunction getFunction() {
return function;
}
public void setFunction(AggregateFunction function) {
this.function = function;
}
@Override
public boolean isContain(Object obj) {
return value.isContain(obj);
}
@Override
public void validate() throws ReportException {
value.validate();
}
}