Coverage Report - us.daveread.basicquery.gui.ColoredCellRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
ColoredCellRenderer
100%
64/64
100%
10/10
0
 
 1  
 package us.daveread.basicquery.gui;
 2  
 
 3  
 import java.awt.Component;
 4  
 import java.awt.Color;
 5  
 import java.util.Map;
 6  
 import java.util.HashMap;
 7  
 import java.util.List;
 8  
 import java.util.ArrayList;
 9  
 import javax.swing.JLabel;
 10  
 import javax.swing.BorderFactory;
 11  
 import javax.swing.JTable;
 12  
 import javax.swing.table.TableCellRenderer;
 13  
 
 14  
 import org.apache.log4j.Logger;
 15  
 
 16  
 /**
 17  
  * Title: Basic Query Utility
 18  
  * Description: Execute arbitrary SQL against database accessible with any
 19  
  * JDBC-compliant driver.
 20  
  * 
 21  
  * Copyright: Copyright (c) 2003-2014
 22  
  * 
 23  
  * @author David Read
 24  
  */
 25  
 
 26  
 public class ColoredCellRenderer extends JLabel implements TableCellRenderer {
 27  
 
 28  
   /**
 29  
    * Serial UID for this version of class
 30  
    */
 31  
   private static final long serialVersionUID = -2456625376274110406L;
 32  
 
 33  
   /**
 34  
    * Logger
 35  
    */
 36  
   @SuppressWarnings("unused")
 37  1
   private static final Logger LOGGER = Logger
 38  
       .getLogger(ColoredCellRenderer.class);
 39  
 
 40  
   /**
 41  
    * Foreground colors mapped to keys
 42  
    */
 43  
   private Map<String, Color> mapcFGPatterns;
 44  
 
 45  
   /**
 46  
    * Background colors mapped to keys
 47  
    */
 48  
   private Map<String, Color> mapcBGPatterns;
 49  
 
 50  
   /**
 51  
    * The ordered set of foregound colors used for rows when displaying a table
 52  
    */
 53  
   private List<Color> objcAlternatingBGColors;
 54  
 
 55  
   /**
 56  
    * The ordered set of background colors used for rows when displaying a table
 57  
    */
 58  
   private List<Color> objcAlternatingFGColors;
 59  
 
 60  
   /**
 61  
    * A table to display
 62  
    */
 63  
   private JTable objcTable;
 64  
 
 65  
   /**
 66  
    * Constructor a ColoredCellRenderer - when this function is called it
 67  
    * passes default parameters to the function
 68  
    * ColoredCellRenderer(int,int,int,int)
 69  
    */
 70  
   public ColoredCellRenderer() {
 71  6
     this(JLabel.LEFT, JLabel.CENTER);
 72  6
   }
 73  
 
 74  
   /**
 75  
    * Class Constructor - passes the parameter iaHorizontalAlignment to the
 76  
    * function ColoredCellRenderer(int,int,int,int)
 77  
    * 
 78  
    * @param iaHorizontalAlignment
 79  
    *          The Horizontal Alignment attribute
 80  
    */
 81  
   public ColoredCellRenderer(int iaHorizontalAlignment) {
 82  1
     this(iaHorizontalAlignment, JLabel.CENTER);
 83  1
   }
 84  
 
 85  
   /**
 86  
    * Creates and Initializes the class ColoredCellRenderer
 87  
    * 
 88  
    * @param iaHorizAlignment
 89  
    *          The Horizontal Alignment attribute
 90  
    * @param iaVerticalAlignment
 91  
    *          The Vertical Alignment attribute
 92  
    */
 93  8
   public ColoredCellRenderer(int iaHorizAlignment, int iaVerticalAlignment) {
 94  8
     setOpaque(true);
 95  8
     setHorizontalAlignment(iaHorizAlignment);
 96  8
     setVerticalAlignment(iaVerticalAlignment);
 97  
 
 98  8
     mapcFGPatterns = new HashMap<String, Color>();
 99  8
     mapcBGPatterns = new HashMap<String, Color>();
 100  8
   }
 101  
 
 102  
   /**
 103  
    * Add a foreground and background color pair for a row in the displayed
 104  
    * table. The added colors are kept in sequence than then used when displaying
 105  
    * a table of data, cycling through the colors in order when displaying the
 106  
    * rows.
 107  
    * 
 108  
    * @param objaTextColor
 109  
    *          The foreground text color
 110  
    * @param objaBGColor
 111  
    *          The background cell color
 112  
    */
 113  
   public void addAlternatingRowColor(Color objaTextColor, Color objaBGColor) {
 114  3
     if (objcAlternatingBGColors == null) {
 115  2
       objcAlternatingBGColors = new ArrayList<Color>();
 116  
     }
 117  
 
 118  3
     if (objcAlternatingFGColors == null) {
 119  2
       objcAlternatingFGColors = new ArrayList<Color>();
 120  
     }
 121  
 
 122  3
     objcAlternatingBGColors.add(objaBGColor);
 123  3
     objcAlternatingFGColors.add(objaTextColor);
 124  3
   }
 125  
 
 126  
   /**
 127  
    * Sets the default foreground (text) and background colors for the object
 128  
    * 
 129  
    * @param objaTextColor
 130  
    *          The default foreground (text) color is set to this value
 131  
    * @param objaBackgroundColor
 132  
    *          The default background color is set to this value
 133  
    */
 134  
   public void setDefaultColors(Color objaTextColor, Color objaBackgroundColor) {
 135  1
     objcAlternatingBGColors = new ArrayList<Color>();
 136  1
     objcAlternatingBGColors.add(objaBackgroundColor);
 137  
 
 138  1
     objcAlternatingFGColors = new ArrayList<Color>();
 139  1
     objcAlternatingFGColors.add(objaTextColor);
 140  1
   }
 141  
 
 142  
   /**
 143  
    * Reset the renderer, removing all color definitions
 144  
    */
 145  
   public void clearColors() {
 146  2
     objcAlternatingBGColors = null;
 147  2
     objcAlternatingFGColors = null;
 148  2
   }
 149  
 
 150  
   /**
 151  
    * Associates the specified foreground (text) and background colors with the
 152  
    * specified string pattern
 153  
    * 
 154  
    * @param saPattern
 155  
    *          The key in this map is the string
 156  
    * @param objaTextColor
 157  
    *          The value of the foreground (text) Color object associated with
 158  
    *          the string
 159  
    * @param objaBackgroundColor
 160  
    *          The value of the background Color object which
 161  
    *          is associated with the string
 162  
    */
 163  
   public void addPattern(String saPattern, Color objaTextColor,
 164  
       Color objaBackgroundColor) {
 165  1
     mapcFGPatterns.put(saPattern, objaTextColor);
 166  1
     mapcBGPatterns.put(saPattern, objaBackgroundColor);
 167  1
   }
 168  
 
 169  
   /**
 170  
    * Finds the background color object that is associated with the String
 171  
    * pattern
 172  
    * 
 173  
    * @param saText
 174  
    *          The string pattern which is the key for the
 175  
    *          color object
 176  
    * @param row
 177  
    *          The row being displayed
 178  
    * 
 179  
    * @return Color The background Color object
 180  
    */
 181  
   private Color findBGColor(String saText, int row) {
 182  
     Color objlMatchedColor;
 183  
 
 184  13
     objlMatchedColor = (Color) mapcBGPatterns.get(saText);
 185  
 
 186  13
     if (objlMatchedColor == null && objcAlternatingBGColors != null) {
 187  10
       objlMatchedColor = (Color) objcAlternatingBGColors.get(row 
 188  
           % objcAlternatingBGColors.size());
 189  
     }
 190  
 
 191  13
     return objlMatchedColor;
 192  
   }
 193  
 
 194  
   /**
 195  
    * Finds the foreground color object that is associated with the String
 196  
    * pattern
 197  
    * 
 198  
    * @param saText
 199  
    *          The string pattern which is the key for the
 200  
    *          color object
 201  
    * @param row
 202  
    *          The row being displayed
 203  
    * 
 204  
    * @return Color The foreground Color object
 205  
    */
 206  
   private Color findFGColor(String saText, int row) {
 207  
     Color objlMatchedColor;
 208  
 
 209  13
     objlMatchedColor = (Color) mapcFGPatterns.get(saText);
 210  
 
 211  13
     if (objlMatchedColor == null && objcAlternatingFGColors != null) {
 212  10
       objlMatchedColor = (Color) objcAlternatingFGColors.get(row 
 213  
           % objcAlternatingFGColors.size());
 214  
     }
 215  
 
 216  13
     return objlMatchedColor;
 217  
   }
 218  
 
 219  
   /**
 220  
    * Returns the component used for drawing the cell.Used to configure the
 221  
    * renderer appropriately before drawing
 222  
    * 
 223  
    * @param table
 224  
    *          The JTable that is asking the renderer to draw
 225  
    * @param value
 226  
    *          The value of the cell to be rendered
 227  
    * @param isSelected
 228  
    *          A Boolean Value - true if the cell is to be rendered
 229  
    *          with the selection highlighted
 230  
    * @param hasFocus
 231  
    *          A Boolean Value - if true render cell appropriately
 232  
    * @param row
 233  
    *          The row index of the cell to be rendered
 234  
    * @param column
 235  
    *          The column index of the cell to be rendered
 236  
    * 
 237  
    * @return Component Returns the component used for drawing the cell
 238  
    */
 239  
   public Component getTableCellRendererComponent(JTable table, Object value,
 240  
       boolean isSelected,
 241  
       boolean hasFocus, int row,
 242  
       int column) {
 243  
 
 244  13
     if (objcTable == null) {
 245  3
       objcTable = table;
 246  
     }
 247  
 
 248  13
     if (value != null) {
 249  11
       setText(value.toString());
 250  
     } else {
 251  2
       setText("");
 252  
     }
 253  
 
 254  13
     setColor(table, isSelected, hasFocus, row, column);
 255  
 
 256  13
     return this;
 257  
   }
 258  
 
 259  
   /**
 260  
    * Sets the color for the object objlBackgroundColor
 261  
    * 
 262  
    * @param table
 263  
    *          The JTable
 264  
    * @param isSelected
 265  
    *          Boolean value
 266  
    * @param hasFocus
 267  
    *          Whether the component has focus
 268  
    * @param row
 269  
    *          The row index
 270  
    * @param col
 271  
    *          The column index
 272  
    */
 273  
   private void setColor(JTable table, boolean isSelected, boolean hasFocus,
 274  
       int row, int col) {
 275  
 
 276  
     Color objlBackgroundColor;
 277  
     Color objlForegroundColor;
 278  
 
 279  13
     objlBackgroundColor = findBGColor(getText(), row);
 280  13
     objlForegroundColor = findFGColor(getText(), row);
 281  
 
 282  13
     if (objlForegroundColor != null) {
 283  10
       setForeground(objlForegroundColor);
 284  
     } else {
 285  3
       setForeground(table.getForeground());
 286  
     }
 287  
 
 288  13
     if (objlBackgroundColor != null) {
 289  10
       setBorder(BorderFactory.createLineBorder(objlBackgroundColor, 2));
 290  10
       setBackground(objlBackgroundColor);
 291  
     } else {
 292  3
       setBorder(BorderFactory.createLineBorder(table.getBackground(), 2));
 293  3
       setBackground(table.getBackground());
 294  
     }
 295  
 
 296  13
     if (isSelected && !hasFocus) {
 297  2
       setBorder(BorderFactory.createLineBorder(table.getSelectionBackground(),
 298  
           2));
 299  2
       setBackground(table.getSelectionBackground());
 300  2
       setForeground(table.getSelectionForeground());
 301  11
     } else if (isSelected) {
 302  3
       setBorder(BorderFactory.createLineBorder(table.getSelectionBackground(),
 303  
           2));
 304  3
       setBackground(table.getBackground());
 305  3
       setForeground(table.getForeground());
 306  
     }
 307  13
   }
 308  
 }