All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.dynamicreports.report.builder.tableofcontents.TableOfContentsCustomizer Maven / Gradle / Ivy

Go to download

DynamicReports is an open source Java reporting library based on JasperReports. It allows to create dynamic report designs and it doesn't need a visual report designer. You can very quickly create reports and produce documents that can be displayed, printed or exported into many popular formats such as PDF, Excel, Word and others.

There is a newer version: 6.20.1
Show newest version
/**
 * DynamicReports - Free Java reporting library for creating reports dynamically
 *
 * Copyright (C) 2010 - 2016 Ricardo Mariaca
 * http://www.dynamicreports.org
 *
 * This file is part of DynamicReports.
 *
 * DynamicReports 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 3 of the License, or
 * (at your option) any later version.
 *
 * DynamicReports 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 DynamicReports. If not, see .
 */

package net.sf.dynamicreports.report.builder.tableofcontents;

import static net.sf.dynamicreports.report.builder.DynamicReports.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.dynamicreports.jasper.base.tableofcontents.JasperTocHeading;
import net.sf.dynamicreports.report.base.expression.AbstractSimpleExpression;
import net.sf.dynamicreports.report.builder.FieldBuilder;
import net.sf.dynamicreports.report.builder.HyperLinkBuilder;
import net.sf.dynamicreports.report.builder.ReportBuilder;
import net.sf.dynamicreports.report.builder.component.ComponentBuilder;
import net.sf.dynamicreports.report.builder.component.HorizontalListBuilder;
import net.sf.dynamicreports.report.builder.component.TextFieldBuilder;
import net.sf.dynamicreports.report.builder.component.VerticalListBuilder;
import net.sf.dynamicreports.report.builder.expression.SystemMessageExpression;
import net.sf.dynamicreports.report.builder.style.ReportStyleBuilder;
import net.sf.dynamicreports.report.constant.Constants;
import net.sf.dynamicreports.report.constant.HorizontalTextAlignment;
import net.sf.dynamicreports.report.constant.HyperLinkType;
import net.sf.dynamicreports.report.constant.TableOfContentsPosition;
import net.sf.dynamicreports.report.definition.DRITableOfContentsCustomizer;
import net.sf.dynamicreports.report.definition.ReportParameters;

/**
 * @author Ricardo Mariaca ([email protected])
 */
public class TableOfContentsCustomizer implements DRITableOfContentsCustomizer {
	private static final long serialVersionUID = Constants.SERIAL_VERSION_UID;

	protected static String dots;

	protected ReportBuilder report;
	protected List headingList;
	protected int headings;
	protected int levels;

	protected FieldBuilder levelField;
	protected FieldBuilder textField;
	protected FieldBuilder referenceField;
	protected FieldBuilder pageIndexField;

	protected HyperLinkBuilder referenceHyperLink;
	protected int pageIndexDigits;

	protected ReportStyleBuilder titleStyle;
	protected ReportStyleBuilder headingStyle;
	protected Map headingStyles;
	protected Integer textFixedWidth;
	protected Integer dotsFixedWidth;
	protected Integer pageIndexFixedWidth;
	protected TableOfContentsPosition position;

	static {
		StringBuilder dots = new StringBuilder(200);
		for (int i = 0; i < dots.capacity(); i++) {
			dots.append(".");
		}
		TableOfContentsCustomizer.dots = dots.toString();
	}

	public TableOfContentsCustomizer() {
		headingStyles = new HashMap();
	}

	protected void init() {
		levelField = field("level", type.integerType());
		textField = field("text", type.stringType());
		referenceField = field("reference", type.stringType());
		pageIndexField = field("pageIndex", type.integerType());

		referenceHyperLink = hyperLink();
		referenceHyperLink.setAnchor(new ReferenceExpression());
		referenceHyperLink.setType(HyperLinkType.LOCAL_ANCHOR);

		pageIndexDigits = String.valueOf(headings).length();

		if (titleStyle == null) {
			titleStyle = stl.style()
				.bold()
				.setFontSize(16)
				.setHorizontalTextAlignment(HorizontalTextAlignment.CENTER);
		}
	}

	@Override
	public void setReport(ReportBuilder report) {
		this.report = report;
	}

	@Override
	public void setHeadingList(List headingList) {
		this.headingList = headingList;
	}

	@Override
	public void setHeadings(int headings) {
		this.headings = headings;
	}

	@Override
	public void setLevels(int levels) {
		this.levels = levels;
	}

	@Override
	public void customize() {
		init();

		report
			.title(
				title(),
				cmp.filler().setFixedHeight(20))
			.fields(
				levelField, textField, referenceField, pageIndexField)
			.detail(detailComponent());
	}

	protected ComponentBuilder title() {
		return cmp.text(new SystemMessageExpression("table_of_contents")).setStyle(titleStyle);
	}

	protected ComponentBuilder detailComponent() {
		VerticalListBuilder detailComponent = cmp.verticalList();
		for (int i = 0; i < levels; i++) {
			ComponentBuilder headingComponent = headingComponent(i);
			headingComponent.setPrintWhenExpression(new PrintHeadingExpression(i));
			headingComponent.removeLineWhenBlank();
			detailComponent.add(headingComponent);
		}
		return detailComponent;
	}

	protected ComponentBuilder headingComponent(int level) {
		HorizontalListBuilder headingComponent = cmp.horizontalList();

		if (level > 0) {
			headingComponent.add(cmp.filler().setFixedWidth(level * 10));
		}

		TextFieldBuilder textComponent = cmp.text(textField)
  		.setHyperLink(referenceHyperLink);
		if (textFixedWidth != null) {
			textComponent.setFixedWidth(textFixedWidth);
		}
		headingComponent.add(textComponent);

		if (level > 0) {
			headingComponent.add(cmp.filler().setFixedWidth(level * 10));
		}

		TextFieldBuilder dotsComponent = cmp.text(dots.toString())
  		.setStretchWithOverflow(false)
  		.setHyperLink(referenceHyperLink);
		if (dotsFixedWidth != null) {
			dotsComponent.setFixedWidth(dotsFixedWidth);
		}
		headingComponent.add(dotsComponent);

		TextFieldBuilder pageIndexComponent = cmp.text(pageIndexField)
  		.setHyperLink(referenceHyperLink);
		if (pageIndexFixedWidth != null) {
			pageIndexComponent.setFixedWidth(pageIndexFixedWidth);
		}
		else {
			pageIndexComponent.setFixedColumns(pageIndexDigits);
		}
		headingComponent.add(pageIndexComponent);

		ReportStyleBuilder headingStyle = headingStyles.get(level);
		if (headingStyle == null) {
			headingStyle = this.headingStyle;
		}
		if (headingStyle != null) {
			textComponent.setStyle(headingStyle);
			dotsComponent.setStyle(headingStyle);
			pageIndexComponent.setStyle(headingStyle);
		}

		return headingComponent;
	}

	public void setTitleStyle(ReportStyleBuilder titleStyle) {
		this.titleStyle = titleStyle;
	}

	public void setHeadingStyle(ReportStyleBuilder headingStyle) {
		this.headingStyle = headingStyle;
	}

	public void setHeadingStyle(int level, ReportStyleBuilder headingStyle) {
		this.headingStyles.put(level, headingStyle);
	}

	public void setTextFixedWidth(Integer textFixedWidth) {
		this.textFixedWidth = textFixedWidth;
	}

	public void setDotsFixedWidth(Integer dotsFixedWidth) {
		this.dotsFixedWidth = dotsFixedWidth;
	}

	public void setPageIndexFixedWidth(Integer pageIndexFixedWidth) {
		this.pageIndexFixedWidth = pageIndexFixedWidth;
	}

	@Override
	public TableOfContentsPosition getPosition() {
		return position;
	}

	public void setPosition(TableOfContentsPosition position) {
		this.position = position;
	}

	protected class ReferenceExpression extends AbstractSimpleExpression {
		private static final long serialVersionUID = Constants.SERIAL_VERSION_UID;

		@Override
		public String evaluate(ReportParameters reportParameters) {
			return reportParameters.getValue(referenceField);
		}
	}

	protected class PrintHeadingExpression extends AbstractSimpleExpression {
		private static final long serialVersionUID = Constants.SERIAL_VERSION_UID;

		private int level;

		public PrintHeadingExpression(int level) {
			this.level = level;
		}

		@Override
		public Boolean evaluate(ReportParameters reportParameters) {
			return reportParameters.getValue(levelField) == level;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy