BarColumn.java

    1/*******************************************************************************
    2 * Copyright (c) 2009 Mountainminds GmbH & Co. KG and others
    3 * All rights reserved. This program and the accompanying materials
    4 * are made available under the terms of the Eclipse Public License v1.0
    5 * which accompanies this distribution, and is available at
    6 * http://www.eclipse.org/legal/epl-v10.html
    7 *
    8 * Contributors:
    9 *    Marc R. Hoffmann - initial API and implementation
   10 *    
   11 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.report.html;
   14
   15import java.io.IOException;
   16import java.text.DecimalFormat;
   17import java.text.NumberFormat;
   18import java.util.List;
   19
   20import org.jacoco.core.analysis.ICounter;
   21import org.jacoco.core.analysis.ICoverageNode;
   22import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
   23import org.jacoco.report.ReportOutputFolder;
   24import org.jacoco.report.html.resources.Resources;
   25
   26/**
   27 * Column with a graphical bar that represents the total amount of items in with
   28 * length, and the coverage ratio with a red/green sections. The implementation
   29 * is stateful, instances must not be used in parallel.
   30 * 
   31 * @author Marc R. Hoffmann
   32 * @version $Revision: $
   33 */
   34public class BarColumn implements ICoverageTableColumn {
   35
   36    private static final int WIDTH = 120;
   37
   38    private final String header;
   39
   40    private final CounterEntity entity;
   41
   42    private final NumberFormat integerFormat = DecimalFormat
   43            .getIntegerInstance();
   44
   45    private int max;
   46
   47    /**
   48     * Creates a new column that is based on the {@link ICounter} for the given
   49     * entity.
   50     * 
   51     * @param header
   52     *            column header caption
   53     * @param entity
   54     *            counter entity for visualization
   55     */
   56    public BarColumn(final String header, final CounterEntity entity) {
   57        this.header = header;
   58        this.entity = entity;
   59    }
   60
   61    public void init(final List<ICoverageTableItem> items,
   62            final ICoverageNode total) {
   63        this.max = 0;
   64        for (final ICoverageTableItem item : items) {
   65            final int count = item.getNode().getCounter(entity).getTotalCount();
   66            if (count > this.max) {
   67                this.max = count;
   68            }
   69        }
   70    }
   71
   72    public void header(final HTMLElement tr, final Resources resources,
   73            final ReportOutputFolder base) throws IOException {
   74        tr.td().text(header);
   75    }
   76
   77    public void footer(final HTMLElement tr, final ICoverageNode total,
   78            final Resources resources, final ReportOutputFolder base)
   79            throws IOException {
   80        tr.td();
   81    }
   82
   83    public void item(final HTMLElement tr, final ICoverageTableItem item,
   84            final Resources resources, final ReportOutputFolder base)
   85            throws IOException {
   86        final HTMLElement td = tr.td();
   87        if (max > 0) {
   88            final ICounter counter = item.getNode().getCounter(entity);
   89            final int notCovered = counter.getNotCoveredCount();
   90            bar(td, notCovered, Resources.REDBAR, resources, base);
   91            final int covered = counter.getCoveredCount();
   92            bar(td, covered, Resources.GREENBAR, resources, base);
   93        }
   94    }
   95
   96    private void bar(final HTMLElement td, final int count, final String image,
   97            final Resources resources, final ReportOutputFolder base)
   98            throws IOException {
   99        final int width = count * WIDTH / max;
  100        if (width > 0) {
  101            td.img(resources.getLink(base, image), width, 10, integerFormat
  102                    .format(count));
  103        }
  104    }
  105
  106}