/*
* :tabSize=2:indentSize=2:noTabs=true:
* :folding=explicit:collapseFolds=1:
*
* Copyright (C) 2009 Chaniel AB, Thomas Dilts This program is free
* software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation;
* either version 2 of the License, or (at your option) any later version. This
* program 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 General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA For more
* information, surf to www.lazy8.nu or email support@lazy8.nu
*/
package nu.lazy8.util.help;
//{{{ imports
import java.util.List;
import nu.lazy8.util.gen.SetupInfo;
import org.jfree.report.Element;
import org.jfree.report.Group;
import org.jfree.report.GroupList;
import org.jfree.report.TextElement;
import org.jfree.report.event.ReportEvent;
import org.jfree.report.function.AbstractFunction;
import org.jfree.report.function.FunctionInitializeException;
import org.jfree.report.style.FontDefinition;
//}}}
/**
* Description of the Class
*
* @author thomas
* @created den 2 juli 2004
*/
public class FontChangeFunction extends AbstractFunction {
private boolean isDoneOnce = false;
private String nameOfFont;
//{{{ +FontChangeFunction() : <init>
/**
*Constructor for the FontChangeFunction object
*/
public FontChangeFunction() {
nameOfFont = SetupInfo.getProperty(SetupInfo.NAME_OF_REPORT_FONT);
if (nameOfFont == null || nameOfFont.length() == 0) {
nameOfFont = "SansSerif";
}
}//}}}
//{{{ -changeFontDef(FontDefinition) : FontDefinition
/**
* Description of the Method
*
* @param fd Description of the Parameter
* @return Description of the Return Value
*/
private FontDefinition changeFontDef(FontDefinition fd) {
if (fd.getFontName().equals(nameOfFont)) {
return fd;
} else {
return new FontDefinition(nameOfFont, fd.getFontSize(), fd.isBold(), fd.isItalic(),
fd.isUnderline(), fd.isStrikeThrough(), fd.getFontEncoding(null), fd.isEmbeddedFont());
}
}//}}}
//{{{ -changeFontInElement(List) : void
/**
* Description of the Method
*
* @param eList Description of the Parameter
*/
private void changeFontInElement(List eList) {
for (int i = 0; i < eList.size(); i++) {
Element e = (Element) eList.get(i);
if (e != null && (e instanceof TextElement)) {
TextElement tx = (TextElement) e;
tx.getStyle().setFontDefinitionProperty(changeFontDef(tx.getStyle().getFontDefinitionProperty()));
}
}
}//}}}
//{{{ +getValue() : Object
/**
* Returns the value calculated by this function. As this function does not calculate values,
* this method does always return null.
*
* @return always null, as this function does not calculate something.
*/
public Object getValue() {
return null;
}//}}}
//{{{ +initialize() : void
/**
* Performs the functions initialisation. The initialisation will fail, if the
* function has no valid name or the required parameter "element" is missing.
* <p>
*
* @see #setElement(String)
* @throws FunctionInitializeException if the element name has not been specified.
*/
public void initialize() throws FunctionInitializeException {
super.initialize();
}//}}}
//{{{ +itemsAdvanced(ReportEvent) : void
/**
* Description of the Method
*
* @param event Description of the Parameter
*/
public void itemsAdvanced(ReportEvent event) {
// if this is a preparerun, nothing gets printed and so no font change is required.
if (!isDoneOnce) {
isDoneOnce = true;
// Lookup the element by name. If there no element found, the getElement function
// returns null, so we have to check this case.
changeFontInElement(event.getReport().getItemBand().getElements());
changeFontInElement(event.getReport().getReportHeader().getElements());
changeFontInElement(event.getReport().getReportFooter().getElements());
changeFontInElement(event.getReport().getPageHeader().getElements());
changeFontInElement(event.getReport().getPageFooter().getElements());
GroupList list = event.getReport().getGroups();
for (int i = 0; i < list.size(); i++) {
Group g = list.get(i);
changeFontInElement(g.getHeader().getElements());
changeFontInElement(g.getFooter().getElements());
}
}
}//}}}
}