BarColumn.java

    1/*******************************************************************************
    2 * Copyright (c) 2009, 2010 Mountainminds GmbH & Co. KG and Contributors
    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 *******************************************************************************/
   12package org.jacoco.report.html.table;
   13
   14import java.io.IOException;
   15import java.text.DecimalFormat;
   16import java.text.NumberFormat;
   17import java.util.Comparator;
   18import java.util.List;
   19import java.util.Locale;
   20
   21import org.jacoco.core.analysis.CounterComparator;
   22import org.jacoco.core.analysis.ICounter;
   23import org.jacoco.core.analysis.ICoverageNode;
   24import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
   25import org.jacoco.report.ReportOutputFolder;
   26import org.jacoco.report.html.HTMLElement;
   27import org.jacoco.report.html.resources.Resources;
   28
   29/**
   30 * Column with a graphical bar that represents the total amount of items in with
   31 * length, and the coverage ratio with a red/green sections. The implementation
   32 * is stateful, instances must not be used in parallel.
   33 * 
   34 * @author Marc R. Hoffmann
   35 * @version 0.4.1.20101007204400
   36 */
   37public class BarColumn implements IColumnRenderer {
   38
   39    private static final int WIDTH = 120;
   40
   41    private final CounterEntity entity;
   42
   43    private final NumberFormat integerFormat;
   44
   45    private int max;
   46
   47    private final Comparator<ITableItem> comparator;
   48
   49    /**
   50     * Creates a new column that is based on the {@link ICounter} for the given
   51     * entity.
   52     * 
   53     * @param entity
   54     *            counter entity for visualization
   55     * @param locale
   56     *            locale for rendering numbers
   57     */
   58    public BarColumn(final CounterEntity entity, final Locale locale) {
   59        this.entity = entity;
   60        this.integerFormat = DecimalFormat.getIntegerInstance(locale);
   61        this.comparator = new TableItemComparator(CounterComparator.MISSEDITEMS
   62                .reverse().on(entity)
   63                .second(CounterComparator.TOTALITEMS.reverse().on(entity)));
   64    }
   65
   66    public boolean init(final List<? extends ITableItem> items,
   67            final ICoverageNode total) {
   68        this.max = 0;
   69        for (final ITableItem item : items) {
   70            final int count = item.getNode().getCounter(entity).getTotalCount();
   71            if (count > this.max) {
   72                this.max = count;
   73            }
   74        }
   75        return true;
   76    }
   77
   78    public void footer(final HTMLElement td, final ICoverageNode total,
   79            final Resources resources, final ReportOutputFolder base)
   80            throws IOException {
   81    }
   82
   83    public void item(final HTMLElement td, final ITableItem item,
   84            final Resources resources, final ReportOutputFolder base)
   85            throws IOException {
   86        if (max > 0) {
   87            final ICounter counter = item.getNode().getCounter(entity);
   88            final int missed = counter.getMissedCount();
   89            bar(td, missed, Resources.REDBAR, resources, base);
   90            final int covered = counter.getCoveredCount();
   91            bar(td, covered, Resources.GREENBAR, resources, base);
   92        }
   93    }
   94
   95    private void bar(final HTMLElement td, final int count, final String image,
   96            final Resources resources, final ReportOutputFolder base)
   97            throws IOException {
   98        final int width = count * WIDTH / max;
   99        if (width > 0) {
  100            td.img(resources.getLink(base, image), width, 10,
  101                    integerFormat.format(count));
  102        }
  103    }
  104
  105    public Comparator<ITableItem> getComparator() {
  106        return comparator;
  107    }
  108
  109}