Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 95   Methods: 3
NCLOC: 29   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ShadedTreeCellRenderer.java 0% 0% 0% 0%
coverage
 1    /*BEGIN_COPYRIGHT_BLOCK*
 2   
 3    PLT Utilities BSD License
 4   
 5    Copyright (c) 2007-2010 JavaPLT group at Rice University
 6    All rights reserved.
 7   
 8    Developed by: Java Programming Languages Team
 9    Rice University
 10    http://www.cs.rice.edu/~javaplt/
 11   
 12    Redistribution and use in source and binary forms, with or without modification, are permitted
 13    provided that the following conditions are met:
 14   
 15    - Redistributions of source code must retain the above copyright notice, this list of conditions
 16    and the following disclaimer.
 17    - Redistributions in binary form must reproduce the above copyright notice, this list of
 18    conditions and the following disclaimer in the documentation and/or other materials provided
 19    with the distribution.
 20    - Neither the name of the JavaPLT group, Rice University, nor the names of the library's
 21    contributors may be used to endorse or promote products derived from this software without
 22    specific prior written permission.
 23   
 24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 25    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 26    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND
 27    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 28    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 29    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 30    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 31    OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32   
 33    *END_COPYRIGHT_BLOCK*/
 34   
 35    package edu.rice.cs.plt.swing;
 36   
 37    import java.awt.*;
 38    import javax.swing.*;
 39    import javax.swing.tree.*;
 40   
 41    /**
 42    * A tree cell renderer that sets the backgrounds of cells to alternating colors.
 43    */
 44    public class ShadedTreeCellRenderer implements TreeCellRenderer {
 45   
 46    /** An invisible component to return when the provided row number is incorrect. */
 47    private static final Component DUMMY_CELL = Box.createRigidArea(new Dimension(0, 0));
 48   
 49    private final TreeCellRenderer _renderer;
 50    private final Color _even;
 51    private final Color _odd;
 52   
 53    /**
 54    * @param renderer A renderer to provide the contents of each cell. For the shading
 55    * to be visible, the returned components must honor their backgroud
 56    * color setting, and must not be obscured by opaque child components.
 57    * @param even Color to shade the first (at index 0) row and each subsequent even row.
 58    * @param odd Color to shade the second (at index 1) row and each subsequent odd row.
 59    */
 60  0 public ShadedTreeCellRenderer(TreeCellRenderer renderer, Color even, Color odd) {
 61  0 _renderer = renderer;
 62  0 _even = even;
 63  0 _odd = odd;
 64    }
 65   
 66  0 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
 67    boolean expanded, boolean leaf, int row,
 68    boolean hasFocus) {
 69    /** Observed bug: provided row numbers will occasionally be out of the range of valid rows.
 70    * Sometimes this seems to be a pre-drawing step to get sizing right, and the value
 71    * is -1. Other times, the value is too large, and the cell is drawn on top of the
 72    * correct cell. The result is that the shading will sometimes be incorrect, depending
 73    * on the too-large value of row. As a workaround, we just ignore requests to handle
 74    * cells with too-large row values (by returning an invisible, sizeless component).
 75    */
 76  0 int maxRow = tree.getRowCount() - (tree.isRootVisible() ? 0 : 1);
 77  0 if (row < maxRow) {
 78  0 Component c = _renderer.getTreeCellRendererComponent(tree, value, selected, expanded,
 79    leaf, row, hasFocus);
 80  0 c.setBackground(row % 2 == 0 ? _even : _odd);
 81  0 return c;
 82    }
 83  0 else { return DUMMY_CELL; }
 84    }
 85   
 86    /**
 87    * Wrap the given tree's cell renderer in a {@code ShadedTreeCellRenderer} using the given
 88    * colors.
 89    */
 90  0 public static void shadeTree(JTree tree, Color even, Color odd) {
 91  0 tree.setCellRenderer(new ShadedTreeCellRenderer(tree.getCellRenderer(), even, odd));
 92    }
 93   
 94   
 95    }