MeasurementTableCellRenderer.java

  1. /*
  2.  *  Copyright (c) 2001-2024, Jean Tessier
  3.  *  All rights reserved.
  4.  *  
  5.  *  Redistribution and use in source and binary forms, with or without
  6.  *  modification, are permitted provided that the following conditions
  7.  *  are met:
  8.  *  
  9.  *      * Redistributions of source code must retain the above copyright
  10.  *        notice, this list of conditions and the following disclaimer.
  11.  *  
  12.  *      * Redistributions in binary form must reproduce the above copyright
  13.  *        notice, this list of conditions and the following disclaimer in the
  14.  *        documentation and/or other materials provided with the distribution.
  15.  *  
  16.  *      * Neither the name of Jean Tessier nor the names of his contributors
  17.  *        may be used to endorse or promote products derived from this software
  18.  *        without specific prior written permission.
  19.  *  
  20.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21.  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22.  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23.  *  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
  24.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26.  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27.  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28.  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29.  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  */

  32. package com.jeantessier.dependencyfinder.gui;

  33. import java.awt.*;

  34. import javax.swing.*;
  35. import javax.swing.table.*;

  36. import com.jeantessier.metrics.*;

  37. public class MeasurementTableCellRenderer extends DefaultTableCellRenderer {
  38.     private static final Color PRIMARY_NORMAL_BACKGROUND        = new Color(247, 247, 247);
  39.     private static final Color SECONDARY_NORMAL_BACKGROUND      = new Color(223, 223, 223);
  40.     private static final Color NORMAL_FOREGROUND                = Color.black;

  41.     private static final Color PRIMARY_HIGHLIGHTED_BACKGROUND   = new Color(255, 223, 223);
  42.     private static final Color SECONDARY_HIGHLIGHTED_BACKGROUND = new Color(255, 207, 207);
  43.     private static final Color HIGHLIGHTED_FOREGROUND           = Color.red;

  44.     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
  45.         JLabel result = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

  46.         if (column == 0) {
  47.             result.setHorizontalAlignment(JLabel.LEFT);
  48.         } else {
  49.             result.setHorizontalAlignment(JLabel.CENTER);
  50.         }

  51.         if (value instanceof Measurement measurement) {
  52.             if (measurement.isInRange()) {
  53.                 formatAsNormalCell(isSelected, row, result);
  54.             } else {
  55.                 formatAsHighlightedCell(isSelected, row, result);
  56.             }

  57.             String text;
  58.             int dispose = ((OOMetricsTableModel) table.getModel()).getRawColumnDispose(column);

  59.             if (measurement instanceof StatisticalMeasurement stats) {
  60.                 text = switch (dispose) {
  61.                     case StatisticalMeasurement.DISPOSE_MINIMUM -> String.valueOf(stats.getMinimum());
  62.                     case StatisticalMeasurement.DISPOSE_MEDIAN -> String.valueOf(stats.getMedian());
  63.                     case StatisticalMeasurement.DISPOSE_AVERAGE -> String.valueOf(stats.getAverage());
  64.                     case StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION -> String.valueOf(stats.getStandardDeviation());
  65.                     case StatisticalMeasurement.DISPOSE_MAXIMUM -> String.valueOf(stats.getMaximum());
  66.                     case StatisticalMeasurement.DISPOSE_SUM -> String.valueOf(stats.getSum());
  67.                     default -> "n/a";
  68.                 };
  69.             } else {
  70.                 text = measurement.getValue().toString();
  71.             }

  72.             setCellContent(result, measurement, dispose, text);
  73.         } else if (value instanceof Metrics metrics) {
  74.             if (metrics.isInRange()) {
  75.                 formatAsNormalCell(isSelected, row, result);
  76.             } else {
  77.                 formatAsHighlightedCell(isSelected, row, result);
  78.             }

  79.             result.setText(metrics.getName());
  80.             result.setToolTipText(metrics.getName());
  81.         } else {
  82.             formatAsNormalCell(isSelected, row, result);
  83.         }

  84.         return result;
  85.     }

  86.     private void formatAsNormalCell(boolean isSelected, int row, JLabel result) {
  87.         result.setForeground(NORMAL_FOREGROUND);

  88.         if (!isSelected) {
  89.             if (((row / 3) % 2) == 0) {
  90.                 result.setBackground(PRIMARY_NORMAL_BACKGROUND);
  91.             } else {
  92.                 result.setBackground(SECONDARY_NORMAL_BACKGROUND);
  93.             }
  94.         }
  95.     }

  96.     private void formatAsHighlightedCell(boolean isSelected, int row, JLabel result) {
  97.         result.setForeground(HIGHLIGHTED_FOREGROUND);

  98.         if (!isSelected) {
  99.             if (((row / 3) % 2) == 0) {
  100.                 result.setBackground(PRIMARY_HIGHLIGHTED_BACKGROUND);
  101.             } else {
  102.                 result.setBackground(SECONDARY_HIGHLIGHTED_BACKGROUND);
  103.             }
  104.         }
  105.     }

  106.     private void setCellContent(JLabel result, Measurement measurement, int dispose, String text) {
  107.         StringBuilder tooltip = new StringBuilder();
  108.         tooltip.append("<html><body><p>");
  109.         tooltip.append("<b>").append(measurement.getContext().getName()).append("</b><br>");
  110.         tooltip.append(measurement.getLongName()).append(" (").append(measurement.getShortName()).append(")");
  111.         if (measurement instanceof StatisticalMeasurement) {
  112.             tooltip.append(", ").append(StatisticalMeasurement.getDisposeLabel(dispose));
  113.         }
  114.         tooltip.append("<br>");
  115.         tooltip.append("valid range: ").append(measurement.getDescriptor().getRangeAsString()).append("<br>");
  116.         tooltip.append("value: ").append(text);
  117.         if (!measurement.isInRange()) {
  118.             tooltip.append(" <b>*** out of range ***</b>");
  119.         }
  120.         if (measurement instanceof StatisticalMeasurement) {
  121.             tooltip.append("<br>").append(measurement);
  122.         }
  123.         tooltip.append("</p></body></html>");

  124.         result.setText(text);
  125.         result.setToolTipText(tooltip.toString());
  126.     }
  127. }