| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
package org.dishevelled.identify; |
| 25 | |
|
| 26 | |
import java.awt.Component; |
| 27 | |
import java.awt.Color; |
| 28 | |
|
| 29 | |
import java.util.List; |
| 30 | |
|
| 31 | |
import javax.swing.JLabel; |
| 32 | |
import javax.swing.JTable; |
| 33 | |
import javax.swing.UIManager; |
| 34 | |
|
| 35 | |
import javax.swing.table.DefaultTableCellRenderer; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | 12 | public class StripeTableCellRenderer extends DefaultTableCellRenderer |
| 44 | |
{ |
| 45 | |
|
| 46 | 12 | private Color oddRowBackgroundColor = DEFAULT_ODD_ROW_BACKGROUND_COLOR; |
| 47 | |
|
| 48 | |
|
| 49 | 2 | public static final Color DEFAULT_ODD_ROW_BACKGROUND_COLOR = new Color(42, 87, 3, 12); |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public final Color getOddRowBackgroundColor() |
| 57 | |
{ |
| 58 | 0 | return oddRowBackgroundColor; |
| 59 | |
} |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
public final void setOddRowBackgroundColor(final Color oddRowBackgroundColor) |
| 69 | |
{ |
| 70 | 0 | if (oddRowBackgroundColor == null) |
| 71 | |
{ |
| 72 | 0 | throw new IllegalArgumentException("oddRowBackgroundColor must not be null"); |
| 73 | |
} |
| 74 | 0 | Color oldOddRowBackgroundColor = this.oddRowBackgroundColor; |
| 75 | 0 | this.oddRowBackgroundColor = oddRowBackgroundColor; |
| 76 | 0 | firePropertyChange("oddRowBackgroundColor", oldOddRowBackgroundColor, this.oddRowBackgroundColor); |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
@Override |
| 80 | |
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) |
| 81 | |
{ |
| 82 | 40 | JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
| 83 | |
|
| 84 | 40 | if (isSelected) |
| 85 | |
{ |
| 86 | 20 | label.setForeground(UIManager.getColor("Table.selectionForeground")); |
| 87 | 20 | label.setBackground(UIManager.getColor("Table.selectionBackground")); |
| 88 | |
} |
| 89 | |
else |
| 90 | |
{ |
| 91 | 20 | if (hasFocus) |
| 92 | |
{ |
| 93 | 10 | label.setForeground(UIManager.getColor("Table.focusCellForeground")); |
| 94 | |
} |
| 95 | |
else |
| 96 | |
{ |
| 97 | 10 | label.setForeground(UIManager.getColor("Table.foreground")); |
| 98 | |
} |
| 99 | 20 | if (row % 2 == 1) |
| 100 | |
{ |
| 101 | 0 | label.setBackground(oddRowBackgroundColor); |
| 102 | |
} |
| 103 | |
else |
| 104 | |
{ |
| 105 | 20 | if (hasFocus) |
| 106 | |
{ |
| 107 | 10 | label.setBackground(UIManager.getColor("Table.focusCellBackground")); |
| 108 | |
} |
| 109 | |
else |
| 110 | |
{ |
| 111 | 10 | label.setBackground(UIManager.getColor("Table.background")); |
| 112 | |
} |
| 113 | |
} |
| 114 | |
} |
| 115 | 40 | return label; |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public static void install(final JTable table) |
| 124 | |
{ |
| 125 | 0 | if (table == null) |
| 126 | |
{ |
| 127 | 0 | throw new IllegalArgumentException("table must not be null"); |
| 128 | |
} |
| 129 | 0 | StripeTableCellRenderer renderer = new StripeTableCellRenderer(); |
| 130 | 0 | table.setDefaultRenderer(Boolean.class, renderer); |
| 131 | 0 | table.setDefaultRenderer(Double.class, renderer); |
| 132 | 0 | table.setDefaultRenderer(Float.class, renderer); |
| 133 | 0 | table.setDefaultRenderer(Integer.class, renderer); |
| 134 | 0 | table.setDefaultRenderer(List.class, renderer); |
| 135 | 0 | table.setDefaultRenderer(Long.class, renderer); |
| 136 | 0 | table.setDefaultRenderer(String.class, renderer); |
| 137 | 0 | } |
| 138 | |
} |