On the Mac, the table renderer needs to be transparent, so I can render the background. On Windows (and probably Linux), it needs to be opaque, so I can set the background color.
- ~
- jbidwatcher
- trunk
- src
- com
- jbidwatcher
- ui
- myTableCellRenderer.java
| myTableCellRenderer.java |
|---|
cyberfox 1 package com.jbidwatcher.ui;// -*- Java -*- cyberfox 2 /* cyberfox 3 * Copyright (c) 2000-2007, CyberFOX Software, Inc. All Rights Reserved. cyberfox 4 * cyberfox 5 * Developed by mrs (Morgan Schweers) cyberfox 6 */ cyberfox 7 cyberfox 8 // cyberfox 9 // History: cyberfox 10 // mrs: 23-July-1999 09:29 - This exists to eliminate cell-based selection in the table cell renderer. (It looks ugly.) cyberfox 11 cyberfox 12 import com.jbidwatcher.auction.AuctionEntry; cyberfox 13 import com.jbidwatcher.auction.MultiSnipe; mrs 14 import com.jbidwatcher.util.config.JConfig; mrs 15 import com.jbidwatcher.platform.Platform; mrs 16 import com.jbidwatcher.ui.table.TableColumnController; cyberfox 17 mrs 18 import javax.swing.*; mrs 19 import javax.swing.border.Border; mrs 20 import javax.swing.table.DefaultTableCellRenderer; cyberfox 21 import java.awt.*; mrs 22 import java.util.HashMap; mrs 23 import java.util.Map; cyberfox 24 cyberfox 25 public class myTableCellRenderer extends DefaultTableCellRenderer { cyberfox 26 private static Color darkBG = null; cyberfox 27 private static Font boldFont = null; cyberfox 28 private static Font fixedFont = null; cyberfox 29 cyberfox 30 private static String selectionColorString = null; cyberfox 31 private static Color selectionColor = null; cyberfox 32 cyberfox 33 private static final Color darkGreen = new Color(0, 127, 0); cyberfox 34 // private static final Color darkBlue = new Color(0, 0, 127); cyberfox 35 private static final Color darkRed = new Color(127, 0, 0); cyberfox 36 private static final Color medBlue = new Color(0, 0, 191); cyberfox 37 private static final Color linuxSelection = new Color(204,204,255); mrs 38 private int mRow = 0; mrs 39 private boolean mSelected = false; cyberfox 40 mrs 41 private static class Colors { mrs 42 Color mForeground; mrs 43 Color mBackground; cyberfox 44 mrs 45 private Colors(Color foreground, Color background) { mrs 46 mForeground = foreground; mrs 47 mBackground = background; mrs 48 } mrs 49 mrs 50 public Color getForeground() { mrs 51 return mForeground; mrs 52 } mrs 53 mrs 54 public Color getBackground() { mrs 55 return mBackground; mrs 56 } mrs 57 } mrs 58 cyberfox 59 public static void resetBehavior() { darkBG = null; boldFont = null; fixedFont = null; } cyberfox 60 mrs 61 public void setValue(Object o) { mrs 62 if(o instanceof Icon) { mrs 63 super.setIcon((Icon) o); mrs 64 super.setValue(null); mrs 65 } else { mrs 66 super.setIcon(null); mrs 67 super.setValue(o); mrs 68 } mrs 69 } mrs 70 cyberfox 71 public Component getTableCellRendererComponent(JTable table, Object value, cyberfox 72 boolean isSelected, boolean hasFocus, cyberfox 73 int row, int column) { cyberfox 74 column = table.convertColumnIndexToModel(column); mrs 75 if(value instanceof Icon) { mrs 76 setHorizontalAlignment(SwingConstants.CENTER); mrs 77 setVerticalAlignment(SwingConstants.CENTER); cyberfox 78 } else { mrs 79 setHorizontalAlignment(JLabel.LEFT); cyberfox 80 } mrs 81 JComponent returnComponent = (JComponent)super.getTableCellRendererComponent(table, value, isSelected, false, row, column); cyberfox 82 cyberfox 83 AuctionEntry ae = (AuctionEntry)table.getValueAt(row, -1); mrs 84 returnComponent.setOpaque(!Platform.isMac()); cyberfox 85 if(ae == null) return returnComponent; cyberfox 86 mrs 87 Color foreground = chooseForeground(ae, column, table.getForeground()); cyberfox 88 mrs 89 mRow = row; mrs 90 mrs 91 if(!Platform.isMac()) { mrs 92 Color background = chooseBackground(ae, column, table.getBackground()); mrs 93 if ((row % 2) == 1) { mrs 94 if (darkBG == null) darkBG = darken(background); cyberfox 95 mrs 96 if (column != 2 || !ae.isMultiSniped()) { mrs 97 if ((column != TableColumnController.SNIPE_OR_MAX && column != TableColumnController.SNIPE_TOTAL) || !ae.isMultiSniped()) { mrs 98 if (JConfig.queryConfiguration("display.alternate", "true").equals("true")) { mrs 99 background = darkBG; mrs 100 } mrs 101 } cyberfox 102 } cyberfox 103 } mrs 104 if (isSelected) { mrs 105 Colors selectionColors = getSelectionColors(column, ae, foreground, background); mrs 106 mrs 107 foreground = selectionColors.getForeground(); mrs 108 background = selectionColors.getBackground(); mrs 109 } mrs 110 returnComponent.setBackground(background); cyberfox 111 } cyberfox 112 mrs 113 mSelected = isSelected; mrs 114 cyberfox 115 Font foo = chooseFont(returnComponent.getFont(), ae, column); cyberfox 116 returnComponent.setFont(foo); cyberfox 117 returnComponent.setForeground(foreground); cyberfox 118 cyberfox 119 return(returnComponent); cyberfox 120 } cyberfox 121 mrs 122 private Color darken(Color background) { mrs 123 int r = background.getRed(); mrs 124 int g = background.getGreen(); mrs 125 int b = background.getBlue(); mrs 126 r = Math.max(0, r - 20); mrs 127 g = Math.max(0, g - 20); mrs 128 b = Math.max(0, b - 20); mrs 129 return new Color(r, g, b); mrs 130 } mrs 131 mrs 132 private Color lighten(Color background) { mrs 133 int r = background.getRed(); mrs 134 int g = background.getGreen(); mrs 135 int b = background.getBlue(); mrs 136 r = Math.min(255, r + 20); mrs 137 g = Math.min(255, g + 20); mrs 138 b = Math.min(255, b + 20); mrs 139 return new Color(r, g, b); mrs 140 } mrs 141 mrs 142 private Map<Integer, GradientPaint> gradientCache = new HashMap<Integer, GradientPaint>(); mrs 143 mrs 144 private final static String evenList = "List.evenRowBackgroundPainter"; mrs 145 private final static String oddList = "List.oddRowBackgroundPainter"; mrs 146 mrs 147 public void paintComponent(Graphics g) { mrs 148 boolean painted = false; mrs 149 if(g != null) { mrs 150 if(JConfig.queryDisplayProperty("background.complex", "false").equals("true")) { mrs 151 Graphics2D g2d = (Graphics2D) g; mrs 152 Rectangle bounds = g2d.getClipBounds(); mrs 153 if (bounds != null) { mrs 154 if (!mSelected) { mrs 155 setOpaque(false); mrs 156 GradientPaint paint = getGradientPaint(); mrs 157 g2d.setPaint(paint); mrs 158 } else { mrs 159 g.setColor(getBackground()); mrs 160 } mrs 161 g2d.fillRect((int) bounds.getX(), (int) bounds.getY(), (int) bounds.getWidth(), (int) bounds.getHeight()); mrs 162 } mrs 163 } else { mrs 164 if(Platform.isMac()) { mrs 165 if(mSelected) { mrs 166 Color selected = UIManager.getColor("Table.selectionBackground"); mrs 167 renderGradient(g, selected); mrs 168 } else { mrs 169 Border bgPaint = UIManager.getBorder((mRow % 2) == 0 ? evenList : oddList); mrs 170 bgPaint.paintBorder(this, g, 0, 0, getWidth(), getHeight()); mrs 171 super.paintComponent(g); mrs 172 painted = true; mrs 173 mrs 174 Graphics2D g2d = (Graphics2D) g; mrs 175 float alpha = .1f; mrs 176 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); mrs 177 g.setColor(Color.BLACK); mrs 178 g.drawLine(0, getHeight()-1, getWidth(), getHeight()-1); mrs 179 } mrs 180 } mrs 181 } mrs 182 if(!painted) super.paintComponent(g); mrs 183 } mrs 184 } mrs 185 mrs 186 private void renderGradient(Graphics g, Color selected) { mrs 187 GradientPaint paint; mrs 188 paint = gradientCache.get(cacheMapper()); mrs 189 if(paint == null) { mrs 190 paint = new GradientPaint(0, 0, lighten(selected), 0, getHeight(), selected, false); mrs 191 gradientCache.put(cacheMapper(), paint); mrs 192 } mrs 193 Graphics2D g2d = (Graphics2D) g; mrs 194 g2d.setPaint(paint); mrs 195 Rectangle bounds = g2d.getClipBounds(); mrs 196 g2d.fillRect((int) bounds.getX(), (int) bounds.getY(), (int) bounds.getWidth(), (int) bounds.getHeight()); mrs 197 } mrs 198 mrs 199 private int cacheMapper() {return 10000 * (mRow % 2) + getHeight();} mrs 200 mrs 201 private GradientPaint getGradientPaint() { mrs 202 GradientPaint paint = gradientCache.get(cacheMapper()); mrs 203 if(paint == null) { mrs 204 if ((mRow % 2) == 0) { mrs 205 paint = new GradientPaint(0, 0, Color.WHITE, 0, getHeight(), Color.LIGHT_GRAY, false); mrs 206 } else { mrs 207 paint = new GradientPaint(0, 0, Color.LIGHT_GRAY, 0, getHeight(), Color.WHITE, false); mrs 208 } mrs 209 } mrs 210 gradientCache.put(cacheMapper(), paint); mrs 211 return paint; mrs 212 } mrs 213 mrs 214 private Colors getSelectionColors(int column, AuctionEntry ae, Color foreground, Color background) { mrs 215 if (JConfig.queryConfiguration("selection.invert", "false").equals("true")) { mrs 216 Color tmp = foreground; mrs 217 foreground = background; mrs 218 background = tmp; mrs 219 } else { mrs 220 if (JConfig.queryConfiguration("selection.color") != null) { mrs 221 if (selectionColorString == null || !selectionColorString.equals(JConfig.queryConfiguration("selection.color"))) { mrs 222 selectionColorString = JConfig.queryConfiguration("selection.color"); mrs 223 selectionColor = MultiSnipe.reverseColor(selectionColorString); mrs 224 } mrs 225 background = selectionColor; mrs 226 } else { mrs 227 if (Platform.isMac() || Platform.isLinux()) { mrs 228 if (column == 2 && ae.isMultiSniped()) foreground = background; mrs 229 background = linuxSelection; mrs 230 } else { mrs 231 if (column == 2 && ae.isMultiSniped()) { mrs 232 foreground = background; mrs 233 } else if (foreground.equals(Color.BLACK)) { mrs 234 foreground = SystemColor.textHighlightText; mrs 235 } mrs 236 background = SystemColor.textHighlight; mrs 237 } mrs 238 } mrs 239 } mrs 240 return new Colors(foreground, background); mrs 241 } mrs 242 mrs 243 private Color chooseForeground(AuctionEntry ae, int col, Color foreground) { cyberfox 244 switch(col) { cyberfox 245 case TableColumnController.ID: cyberfox 246 return chooseIDColor(ae); cyberfox 247 case TableColumnController.SNIPE_OR_MAX: cyberfox 248 case TableColumnController.SNIPE_TOTAL: cyberfox 249 return snipeBidColor(ae); cyberfox 250 case TableColumnController.TITLE: cyberfox 251 return titleColor(ae); cyberfox 252 case TableColumnController.CUR_BID: cyberfox 253 default: mrs 254 return (foreground == null) ? Color.BLACK : foreground; cyberfox 255 } cyberfox 256 } cyberfox 257 cyberfox 258 private Color chooseBackground(AuctionEntry ae, int col, Color default_color) { cyberfox 259 Color ret = null; cyberfox 260 cyberfox 261 if(ae != null) { cyberfox 262 if ((col == TableColumnController.SNIPE_OR_MAX || col == TableColumnController.SNIPE_TOTAL) && ae.isSniped()) { cyberfox 263 ret = snipeBidBackground(ae); cyberfox 264 } cyberfox 265 } cyberfox 266 cyberfox 267 if(ret != null) return ret; cyberfox 268 return default_color; cyberfox 269 } cyberfox 270 mrs 271 private static Font sDefaultFont = null; mrs 272 public static Font getDefaultFont() { mrs 273 if(sDefaultFont == null) { mrs 274 String cfgDefault = JConfig.queryConfiguration("default.font"); mrs 275 if(cfgDefault != null) { mrs 276 sDefaultFont = Font.decode(cfgDefault); mrs 277 } mrs 278 } mrs 279 return sDefaultFont; mrs 280 } mrs 281 mrs 282 private static String getStyleName(int style) { mrs 283 switch(style) { mrs 284 case 1: return "bold"; mrs 285 case 2: return "italic"; mrs 286 case 3: return "bolditalic"; mrs 287 case 0: mrs 288 default: return "plain"; mrs 289 } mrs 290 } mrs 291 mrs 292 public static void setDefaultFont(Font defaultFont) { mrs 293 String formattedFontName = defaultFont.getFamily() + "-" + getStyleName(defaultFont.getStyle()) + "-" + defaultFont.getSize(); mrs 294 JConfig.setConfiguration("default.font", formattedFontName); mrs 295 sDefaultFont = defaultFont; mrs 296 fixedFont = null; mrs 297 boldFont = null; mrs 298 } mrs 299 cyberfox 300 private Font chooseFont(Font base, AuctionEntry ae, int col) { cyberfox 301 boolean hasComment = ae.getComment() != null; mrs 302 if(sDefaultFont != null) base = sDefaultFont; else sDefaultFont = base; cyberfox 303 cyberfox 304 if(fixedFont == null) fixedFont = new Font("Monospaced", base.getStyle(), base.getSize()); cyberfox 305 if(boldFont == null) boldFont = base.deriveFont(Font.BOLD); cyberfox 306 if(col == TableColumnController.TIME_LEFT) return fixedFont; mrs 307 if(hasComment && col == TableColumnController.ID) return boldFont; cyberfox 308 if(ae.isShippingOverridden() && col == TableColumnController.SHIPPING_INSURANCE) return boldFont; cyberfox 309 return base; cyberfox 310 } cyberfox 311 cyberfox 312 private Color snipeBidBackground(AuctionEntry ae) { cyberfox 313 if (ae.isMultiSniped()) { cyberfox 314 return ae.getMultiSnipe().getColor(); cyberfox 315 } cyberfox 316 return null; cyberfox 317 } cyberfox 318 mrs 319 private Color titleColor(AuctionEntry ae) { cyberfox 320 if (ae != null && ae.getHighBidder() != null) { cyberfox 321 if (ae.isHighBidder()) { cyberfox 322 if (!ae.isReserve() || ae.isReserveMet()) { cyberfox 323 return medBlue; cyberfox 324 } else { cyberfox 325 return darkRed; cyberfox 326 } cyberfox 327 } else { cyberfox 328 if (ae.getNumBidders() > 0 && (!ae.isReserve() || ae.isReserveMet())) { mrs 329 if (!ae.isSeller()) { cyberfox 330 return darkRed; cyberfox 331 } else { cyberfox 332 return darkGreen; cyberfox 333 } cyberfox 334 } cyberfox 335 } cyberfox 336 } cyberfox 337 cyberfox 338 return Color.BLACK; cyberfox 339 } cyberfox 340 cyberfox 341 private Color snipeBidColor(AuctionEntry ae) { cyberfox 342 if(ae != null) { cyberfox 343 if(ae.isSniped()) { cyberfox 344 if (!ae.isMultiSniped()) { cyberfox 345 if (ae.isSnipeValid() || ae.isDutch()) { cyberfox 346 return darkGreen; cyberfox 347 } cyberfox 348 return darkRed; cyberfox 349 } cyberfox 350 if (ae.snipeCancelled()) { cyberfox 351 return darkRed; cyberfox 352 } cyberfox 353 } else if (ae.isBidOn()) { cyberfox 354 if(ae.isHighBidder()) return medBlue; cyberfox 355 return darkRed; cyberfox 356 } else if (ae.snipeCancelled()) { cyberfox 357 return darkRed; cyberfox 358 } cyberfox 359 } cyberfox 360 return Color.BLACK; cyberfox 361 } cyberfox 362 cyberfox 363 private Color chooseIDColor(AuctionEntry ae) { cyberfox 364 if(ae != null) { cyberfox 365 boolean recent = ae.getJustAdded() != 0; cyberfox 366 boolean isUpdating = ae.isUpdating(); cyberfox 367 cyberfox 368 if (recent) { cyberfox 369 return darkGreen; cyberfox 370 } else if (isUpdating) { cyberfox 371 return darkRed; cyberfox 372 } cyberfox 373 } cyberfox 374 return Color.BLACK; cyberfox 375 } cyberfox 376 }
Check out the code: svn co jbidwatcher/trunk/src/com/jbidwatcher/ui/myTableCellRenderer.java
