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

ro.nextreports.engine.ReportLayout Maven / Gradle / Ivy

Go to download

NextReports Engine is a lightweight Java platform development library which can be used to run NextReports inside your applications.

There is a newer version: 9.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ro.nextreports.engine;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import ro.nextreports.engine.band.Band;
import ro.nextreports.engine.band.BandElement;
import ro.nextreports.engine.band.ChartBandElement;
import ro.nextreports.engine.band.FunctionBandElement;
import ro.nextreports.engine.band.Padding;
import ro.nextreports.engine.band.PaperSize;
import ro.nextreports.engine.band.ReportBandElement;
import ro.nextreports.engine.exporter.ResultExporter;


/**
 * @author Decebal Suiu
 */
public class ReportLayout implements Serializable {

    public static final int HEADER_GROUP_MASK = 1;
	public static final int FOOTER_GROUP_MASK = 2;
	public static final int ALL_GROUP_MASK = HEADER_GROUP_MASK | FOOTER_GROUP_MASK;

    public static final String HEADER_BAND_NAME = "Header";
    public static final String PAGE_HEADER_BAND_NAME = "PageHeader";
    public static final String DETAIL_BAND_NAME = "Detail";
    public static final String FOOTER_BAND_NAME = "Footer";
    public static final String PAGE_FOOTER_BAND_NAME = "PageFooter";
    public static final String GROUP_HEADER_BAND_NAME_PREFIX = "Group_Header";
    public static final String GROUP_FOOTER_BAND_NAME_PREFIX = "Group_Footer";

    public static final String LETTER = "LETTER";
    public static final String A0 = "A0";
    public static final String A1 = "A1";
    public static final String A2 = "A2";
    public static final String A3 = "A3";
    public static final String A4 = "A4";
    public static final String LEGAL = "LEGAL";
    public static final String LEDGER = "LEDGER";
    public static final String TABLOID = "TABLOID";
    public static final String CUSTOM = "CUSTOM";
	
    private static final long serialVersionUID = -345768096451315236L;

    private List groups;
    private List columnsWidth;
    private boolean useSize;
    
    private Band headerBand;    
    private Band pageHeaderBand;
    private List groupHeaderBands;
    private Band detailBand;
    private List groupFooterBands;
    private Band pageFooterBand;
    private Band footerBand;

    private int orientation;
    private int reportType;
    private String pageFormat;
    private PaperSize paperSize;
    private Padding pagePadding;
    private String backgroundImage;

    private boolean headerOnEveryPage;

    public ReportLayout() {
        groups = new ArrayList();
        
        headerBand = new Band(HEADER_BAND_NAME);
        pageHeaderBand = new Band(PAGE_HEADER_BAND_NAME);
        detailBand = new Band(DETAIL_BAND_NAME);
        pageFooterBand = new Band(PAGE_FOOTER_BAND_NAME);
        footerBand = new Band(FOOTER_BAND_NAME);
        groupHeaderBands = new ArrayList();
        groupFooterBands = new ArrayList();

        columnsWidth = new ArrayList();
    }

    public List getColumnsWidth() {
        return columnsWidth;
    }

    public void setColumnsWidth(List columnsWidth) {
        this.columnsWidth = columnsWidth;
    }

    public boolean isUseSize() {
        return useSize;
    }

    public void setUseSize(boolean useSize) {
        this.useSize = useSize;
    }

    public int getOrientation() {
        return orientation;
    }

    public void setOrientation(int orientation) {
        this.orientation = orientation;
    }
        
    public int getReportType() {
		return reportType;
	}

	public void setReportType(int reportType) {
		this.reportType = reportType;
	}

	public String getPageFormat() {
        if (pageFormat == null) {
            return A4;
        }
        return pageFormat;
    }

    public void setPageFormat(String pageFormat) {
        this.pageFormat = pageFormat;
    }
    
    public PaperSize getPaperSize() {
        if (paperSize == null) {
            return PaperSize.A4;
        }
        return paperSize;
    }

    public void setPaperSize(PaperSize paperSize) {
        this.paperSize = paperSize;
    }
                
    public String getBackgroundImage() {
		return backgroundImage;
	}

	public void setBackgroundImage(String backgroundImage) {
		this.backgroundImage = backgroundImage;
	}

	public Padding getPagePadding() {
    	if (pagePadding == null) {
    		int m = ResultExporter.DEFAULT_PADDING_PIXELS;
    		return new Padding(m, m, m, m);
    	}
		return pagePadding;
	}

	public void setPagePadding(Padding pagePadding) {
		this.pagePadding = pagePadding;
	}

	public boolean isHeaderOnEveryPage() {
        return headerOnEveryPage;
    }

    public void setHeaderOnEveryPage(boolean headerOnEveryPage) {
        this.headerOnEveryPage = headerOnEveryPage;
    }

    public Band getDetailBand() {
        return detailBand;
    }
    
    public Band getFooterBand() {
        return footerBand;
    }
    
    public Band getHeaderBand() {
        return headerBand;
    }
    
    public Band getPageFooterBand() {
    	if (pageFooterBand == null) {
    		pageFooterBand = new Band(PAGE_FOOTER_BAND_NAME);
    	}
        return pageFooterBand;
    }
    
    public Band getPageHeaderBand() {
    	if (pageHeaderBand == null) {
    		pageHeaderBand = new Band(PAGE_HEADER_BAND_NAME);
    	}
        return pageHeaderBand;
    }
    
    public List getGroupHeaderBands() {
		return groupHeaderBands;
	}

	public List getGroupFooterBands() {
		return groupFooterBands;
	}

	public List getGroups() {
		return groups;
	}

	public Band getBand(String name) {
		for (Band band : getBands()) {
			if (name.equals(band.getName())) {
				return band;
			}
		}
		
		return null;
	}
	
	public int getBandIndex(String bandName) {
		List bands = getBands();
		for (int i = 0; i < bands.size(); i++) {
			if (bandName.equals(bands.get(i).getName())) {
				return i;
			}
		}
		
		return -1;
	}

	public List getBandNamesAfter(String bandName) {
		return getBandNamesAfter(getBandIndex(bandName));
	}
	
	public List getBandNamesAfter(int index) {
		List bandAfters = getBandsAfter(index);
		List bandNamesAfter = new ArrayList();
		for (Band band : bandAfters) {
			bandNamesAfter.add(band.getName());
		}
		
		return bandNamesAfter;
	}

	public List getBandsAfter(String bandName) {
		return getBandsAfter(getBandIndex(bandName));
	}
	
	public List getBandsAfter(int index) {
		if (index == -1) {
			return new ArrayList();
		}
		
		List bands = getBands();
		List bandsAfter = new ArrayList();
		for (int i = index + 1; i < bands.size(); i++) {
			bandsAfter.add(bands.get(i));
		}
		
		return bandsAfter;
	}

    public int getGridRow(String bandName, int bandRow) {
        int gridRow = 0;
        for (Band band : getBands()) {
             if (!band.getName().equals(bandName)) {
                 gridRow += band.getRowCount();
             } else {
                 gridRow += bandRow;
                 break;
             }
        }
        return gridRow;
    }

    public void addGroup(ReportGroup group, int groupMask) {
        groupHeaderBands.add(new Band(GROUP_HEADER_BAND_NAME_PREFIX + group.getName()));
        groupFooterBands.add(0, new Band(GROUP_FOOTER_BAND_NAME_PREFIX + group.getName()));        
        if (groups == null) {
            groups = new ArrayList();
        }
        groups.add(group);
    }
	
	public void removeGroup(String groupName) {
        for (Iterator it = groups.iterator(); it.hasNext();) {
            ReportGroup group = (ReportGroup)it.next();
            if (group.getName().equals(groupName)) {
                it.remove();                
            }
        }
        for (Iterator it = groupHeaderBands.iterator(); it.hasNext(); ) {
            Band band =(Band)it.next();
            if  (band.getName().substring(GROUP_HEADER_BAND_NAME_PREFIX.length()).equals(groupName))  {
                it.remove();
                break;
            }
        }
        for (Iterator it = groupFooterBands.iterator(); it.hasNext(); ) {
            Band band =(Band)it.next();
            if  (band.getName().substring(GROUP_FOOTER_BAND_NAME_PREFIX.length()).equals(groupName))  {
                it.remove();
                break;
            }
        }
    }

    public void editGroup(String groupName, String newColumnName, boolean headerOnEveryPage, boolean newPageAfter) {
        for (ReportGroup group : groups) {
            if (group.getName().equals(groupName)) {
                group.setColumn(newColumnName);
                group.setHeaderOnEveryPage(headerOnEveryPage);
                group.setNewPageAfter(newPageAfter);
            }
        }        
    }

    public ReportGroup getGroup(String groupName) {
        for (ReportGroup group : groups) {
            if (group.getName().equals(groupName)) {
                return group;
            }
        }
        return null;
    }


    /**
	 * @return List with 'sorted' bands.
	 */
    public List getBands() {
        List bands = new ArrayList();
        bands.add(getPageHeaderBand());
        bands.add(headerBand);       
        bands.addAll(groupHeaderBands);
        bands.add(detailBand);
        bands.addAll(groupFooterBands);        
        bands.add(footerBand);
        bands.add(getPageFooterBand());
        
        return bands;
    }
    
    /**
     * Get bands that appear in document page (no header page and footer page)
     * @return list of bands that appear in document page 
     */
    public List getDocumentBands() {
        List bands = new ArrayList();
        bands.add(headerBand);        
        bands.addAll(groupHeaderBands);
        bands.add(detailBand);
        bands.addAll(groupFooterBands);        
        bands.add(footerBand);
        
        return bands;
    }

    public List getNotEmptyBands() {
        List bands = new ArrayList();
        List allBands = getBands();
        for (Band band : allBands) {
            if (band.getRowCount() > 0) {
                bands.add(band);
            }
        }

        return bands;
    }

    public int getRowCount() {
    	int rowCount = 0;
    	for (Band band : getBands()) {
    		rowCount += band.getRowCount();
    	}
    	
    	return rowCount;
    }
    
    public int getColumnCount() {
        for (Band band : getBands()) {
            int count = band.getColumnCount();
            if  (count > 0) {
                return count;
            }
        }
        return 0;
    }

    public void clear() {
        headerBand.clear();
        pageHeaderBand.clear();
        detailBand.clear();
        pageFooterBand.clear();
        footerBand.clear();
        
        for (Band band : groupHeaderBands) {
        	band.clear();
        }
        
        for (Band band : groupFooterBands) {
        	band.clear();
        }

        if (groups != null) {
            groups.clear();
        }
    }
    
    public void initBandsListenerList() {
        for (Band band : getBands()) {
            band.initListenerList();
        }
    }

    public static String[] getPageFormats() {
        return new String[] { A4, A3, A2, A1, A0, LETTER, LEGAL, LEDGER, TABLOID, CUSTOM };
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ReportLayout that = (ReportLayout) o;

        if (headerOnEveryPage != that.headerOnEveryPage) return false;
        if (orientation != that.orientation) return false;
        if (reportType != that.reportType) return false;
        if (useSize != that.useSize) return false;
        if (columnsWidth != null ? !columnsWidth.equals(that.columnsWidth) : that.columnsWidth != null) return false;
        if (detailBand != null ? !detailBand.equals(that.detailBand) : that.detailBand != null) return false;
        if (footerBand != null ? !footerBand.equals(that.footerBand) : that.footerBand != null) return false;
        if (groupFooterBands != null ? !groupFooterBands.equals(that.groupFooterBands) : that.groupFooterBands != null)
            return false;
        if (groupHeaderBands != null ? !groupHeaderBands.equals(that.groupHeaderBands) : that.groupHeaderBands != null)
            return false;
        if (groups != null ? !groups.equals(that.groups) : that.groups != null) return false;
        if (headerBand != null ? !headerBand.equals(that.headerBand) : that.headerBand != null) return false;
        if (pageHeaderBand != null ? !pageHeaderBand.equals(that.pageHeaderBand) : that.pageHeaderBand != null) return false;
        if (pageFooterBand != null ? !pageFooterBand.equals(that.pageFooterBand) : that.pageFooterBand != null) return false;
        if (pageFormat != null ? !pageFormat.equals(that.pageFormat) : that.pageFormat != null) return false;
        if (pagePadding != null ? !pagePadding.equals(that.pagePadding) : that.pagePadding != null) return false;
        if (backgroundImage != null ? !backgroundImage.equals(that.backgroundImage) : that.backgroundImage != null) return false;
        if (paperSize != null ? !paperSize.equals(that.paperSize) : that.paperSize != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (groups != null ? groups.hashCode() : 0);
        result = 31 * result + (columnsWidth != null ? columnsWidth.hashCode() : 0);
        result = 31 * result + (useSize ? 1 : 0);
        result = 31 * result + (headerBand != null ? headerBand.hashCode() : 0);
        result = 31 * result + (pageHeaderBand != null ? pageHeaderBand.hashCode() : 0);
        result = 31 * result + (pageFooterBand != null ? pageFooterBand.hashCode() : 0);
        result = 31 * result + (groupHeaderBands != null ? groupHeaderBands.hashCode() : 0);
        result = 31 * result + (detailBand != null ? detailBand.hashCode() : 0);
        result = 31 * result + (groupFooterBands != null ? groupFooterBands.hashCode() : 0);
        result = 31 * result + (footerBand != null ? footerBand.hashCode() : 0);
        result = 31 * result + orientation;
        result = 31 * result + reportType;
        result = 31 * result + (pageFormat != null ? pageFormat.hashCode() : 0);
        result = 31 * result + (headerOnEveryPage ? 1 : 0);
        result = 31 * result + (pagePadding != null ? pagePadding.hashCode() : 0);
        result = 31 * result + (backgroundImage != null ? backgroundImage.hashCode() : 0);
        result = 31 * result + (paperSize != null ? paperSize.hashCode() : 0);
        return result;
    }
    
    public Set getFunctions() {
    	Set functions = new HashSet();
    	List bands = getBands();
    	for (Band band : bands) {
    		for (int i=0, rows = band.getRowCount(); i getFunctions(String bandName) {    	
    	Set functions = new HashSet();
    	List bands = getBands();
    	for (Band band : bands) {    		
			if (band.getName().equals(bandName)) {
				for (int i = 0, rows = band.getRowCount(); i < rows; i++) {
					for (int j = 0, cols = band.getColumnCount(); j < cols; j++) {
						BandElement be = band.getElementAt(i, j);
						if (be instanceof FunctionBandElement) {
							FunctionBandElement fbe = (FunctionBandElement) be;
							functions.add(fbe.getFunction() + "_" + fbe.getColumn());							
						}
					}
				}
			}
    	}
    	return functions;
    }
    
    public ReportBandElement getReportBandElement(String reportName) {
    	 List bands = getDocumentBands();
         for (Band band : bands) {
        	 for (int i=0; i bands = getDocumentBands();
        for (Band band : bands) {
       	 for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy