001package jmri.util.table; 002 003import java.awt.Color; 004import java.awt.Component; 005import javax.annotation.CheckForNull; 006import javax.swing.JProgressBar; 007import javax.swing.JTable; 008import javax.swing.table.TableCellRenderer; 009 010/** 011 * Cell Renderer for a progress bar. 012 * 013 * Displays progress bar changing from red ( 0f ) to green ( 1f ) 014 * Percentage value also displayed as text. 015 * @author Steve Young Copyright (C) 2020 016 */ 017public class ProgressCellRender extends JProgressBar implements TableCellRenderer { 018 019 private final Color _oddRowColor; 020 021 /** 022 * Create a new Instance. 023 * @param oddRowColor optional value for alternate row colour, can be null. 024 */ 025 public ProgressCellRender( @CheckForNull Color oddRowColor ){ 026 super(); 027 _oddRowColor = oddRowColor; 028 } 029 030 @Override 031 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 032 boolean hasFocus, int row, int column) { 033 int progress = 0; 034 int fullValprogress = 0; 035 float fp = 0.00f; 036 if (value instanceof Float) { 037 fp = (Float) value; 038 progress = Math.round( fp * 100f); 039 fullValprogress = Math.round( fp * 1000f); 040 if ( progress==100 && fullValprogress<1000 ){ 041 progress = 99; 042 } 043 } 044 045 // progress value from 0 to 100 046 // As progress increases bar changes from red to green via yellow 047 setForeground(new Color(Math.min(0.8f, 2.0f * (1 - fp)),Math.min(0.8f, 2.0f * fp ),0)); 048 setBorderPainted(false); 049 setStringPainted(true); 050 setValue(fullValprogress); 051 if ( progress < 99 ) { 052 setMaximum(1000); 053 } 054 setString(progress + "%"); 055 if (_oddRowColor!=null) { 056 setBackground( isSelected ? table.getSelectionBackground() : 057 ( row % 2 == 0 ) ? table.getBackground() : _oddRowColor); 058 } 059 return this; 060 } 061}