001    /**
002     * www.jcoverage.com
003     * Copyright (C)2003 jcoverage ltd.
004     *
005     * This file is part of jcoverage.
006     *
007     * jcoverage is free software; you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published
009     * by the Free Software Foundation; either version 2 of the License,
010     * or (at your option) any later version.
011     *
012     * jcoverage is distributed in the hope that it will be useful, but
013     * WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with jcoverage; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020     * USA
021     *
022     */
023    package com.jcoverage.reporting.html;
024    
025    
026    import java.io.PrintWriter;
027    
028    import org.apache.log4j.Logger;
029    
030    import com.jcoverage.reporting.Column;
031    import com.jcoverage.reporting.FormattingContext;
032    import com.jcoverage.reporting.Line;
033    import com.jcoverage.reporting.Page;
034    
035    /**
036     *
037     */
038    public class PercentageRedGreenIndicator extends DefaultColumnRenderer implements ColumnRenderer {
039      
040      static Logger logger=Logger.getLogger(PercentageRedGreenIndicator.class);
041      
042      public PercentageRedGreenIndicator(String label) {
043        super(label);
044      }
045    
046      public void renderField(FormattingContext ctx,PrintWriter writer,Column column,Line line,String href) {
047        int greenValue=((Integer)line.getField(column)).intValue();
048        int redValue=100-greenValue;
049    
050        String greenGifPath=ctx.getCollator().getPathToResource(ctx,"images/green.gif",getPage(line));
051        String redGifPath=ctx.getCollator().getPathToResource(ctx,"images/red.gif",getPage(line));
052    
053        writer.print("<td>");
054        writer.print(greenValue);
055        writer.print('%');
056        writer.print(" ");
057        writer.print("<img src=\""+greenGifPath+"\" height=\"10\" width=\""+greenValue+"\"/>");
058        writer.print("<img src=\""+redGifPath+"\" height=\"10\" width=\""+redValue+"\"/>");
059        writer.println("</td>");
060      }
061      
062      public String getWidth() {
063        return "100";
064      }
065    
066      /**
067       * We need to find the location of the page we are generating.  If
068       * our line has an owning page, we can use that, otherwise we must
069       * be the top-level line, which means we should use the detail
070       * page. It's a bit contrived, but should work for most situations.
071       */
072      Page getPage(Line line) {
073        Page page=line.getOwner();
074        if (page==null) {
075          page=line.getDetailPage();
076        }
077        return page;
078      }
079    
080    }