1 7 8 package org.jdesktop.swing.decorator; 9 10 import java.awt.Color ; 11 import java.awt.Component ; 12 13 18 public abstract class ConditionalHighlighter extends Highlighter { 19 protected int testColumn = 0; protected int highlightColumn = -1; protected int mask = 255; 22 23 public ConditionalHighlighter() { 24 } 26 27 44 public ConditionalHighlighter(Color cellBackground, Color cellForeground, 45 int testColumn, int highlightColumn) { 46 super(cellBackground, cellForeground); 47 this.testColumn = testColumn; 48 this.highlightColumn = highlightColumn; 49 } 50 51 public void setMask(int alpha) { 52 mask = alpha; 53 } 54 55 public int getMask() { 56 return mask; 57 } 58 59 67 public Component highlight(Component renderer, ComponentAdapter adapter) { 68 if (needsHighlight(adapter)) { 69 return doHighlight(renderer, adapter); 70 } 71 else if (getMask() < 256) { 72 return doMask(renderer, adapter); 73 } 74 return renderer; 75 } 76 77 protected Component doMask(Component renderer, ComponentAdapter adapter) { 78 79 maskBackground(renderer, adapter); 80 maskForeground(renderer, adapter); 81 83 return renderer; 84 } 85 86 protected void maskBackground(Component renderer, ComponentAdapter adapter) { 87 Color seed = renderer.getBackground(); 88 Color color = adapter.isSelected() ? computeSelectedBackground(seed) : seed; 89 renderer.setBackground( 90 new Color ((getMask() << 24) | (color.getRGB() & 0x00FFFFFF), true)); 91 } 92 93 protected void maskForeground(Component renderer, ComponentAdapter adapter) { 94 Color seed = renderer.getForeground(); 95 Color color = adapter.isSelected() ? computeSelectedForeground(seed) : seed; 96 renderer.setForeground( 97 new Color ((getMask() << 24) | (color.getRGB() & 0x00FFFFFF), true)); 98 } 99 100 106 protected Color computeBackground(Component renderer, ComponentAdapter adapter) { 107 return getBackground() == null ? null : 108 super.computeBackground(renderer, adapter); 109 } 110 111 117 protected Color computeForeground(Component renderer, ComponentAdapter adapter) { 118 return getForeground() == null ? null : 119 super.computeForeground(renderer, adapter); 120 } 121 122 protected Color computeSelectedForeground(Color seed) { 123 return getSelectedForeground() == null ? seed.brighter() : getSelectedForeground(); 124 } 125 126 public int getTestColumnIndex() { 127 return testColumn; 128 } 129 130 public void setTestColumnIndex(int columnIndex) { 131 this.testColumn = columnIndex; 132 } 133 134 public int getHighlightColumnIndex() { 135 return highlightColumn; 136 } 137 138 public void setHighlightColumnIndex(int columnIndex) { 139 this.highlightColumn = columnIndex; 140 } 141 142 150 protected boolean needsHighlight(ComponentAdapter adapter) { 151 if ((highlightColumn < 0) || 156 (adapter.modelToView(highlightColumn) == adapter.column)) { 157 return test(adapter); 158 } 159 return false; } 161 162 169 protected abstract boolean test(ComponentAdapter adapter); 170 171 } 172 | Popular Tags |