A first pass at making the main table look a little more Mac like when on a Mac. It's definitely pretty; who knows if users'll like it, though. by mrs, 19 Oct, 2009 09:35 AM
Diff this changeset:
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(JLabel.CENTER);
cyberfox 77      } else {
mrs      78        setHorizontalAlignment(JLabel.LEFT);
cyberfox 79      }
mrs      80      JComponent returnComponent = (JComponent)super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
cyberfox 81  
cyberfox 82      AuctionEntry ae = (AuctionEntry)table.getValueAt(row, -1);
mrs      83      returnComponent.setOpaque(false);
cyberfox 84      if(ae == null) return returnComponent;
cyberfox 85  
mrs      86      Color foreground = chooseForeground(ae, column, table.getForeground());
cyberfox 87  
mrs      88      mRow = row;
mrs      89  
mrs      90      if(!Platform.isMac()) {
mrs      91        Color background = chooseBackground(ae, column, table.getBackground());
mrs      92        if ((row % 2) == 1) {
mrs      93          if (darkBG == null) darkBG = darken(background);
cyberfox 94  
mrs      95          if (column != 2 || !ae.isMultiSniped()) {
mrs      96            if ((column != TableColumnController.SNIPE_OR_MAX && column != TableColumnController.SNIPE_TOTAL) || !ae.isMultiSniped()) {
mrs      97              if (JConfig.queryConfiguration("display.alternate", "true").equals("true")) {
mrs      98                background = darkBG;
mrs      99              }
mrs      100           }
cyberfox 101         }
cyberfox 102       }
mrs      103       if (isSelected) {
mrs      104         Colors selectionColors = getSelectionColors(column, ae, foreground, background);
mrs      105 
mrs      106         foreground = selectionColors.getForeground();
mrs      107         background = selectionColors.getBackground();
mrs      108       }
mrs      109       returnComponent.setBackground(background);
cyberfox 110     }
cyberfox 111 
mrs      112     mSelected = isSelected;
mrs      113 
cyberfox 114     Font foo = chooseFont(returnComponent.getFont(), ae, column);
cyberfox 115     returnComponent.setFont(foo);
cyberfox 116     returnComponent.setForeground(foreground);
cyberfox 117 
cyberfox 118     return(returnComponent);
cyberfox 119   }
cyberfox 120 
mrs      121   private Color darken(Color background) {
mrs      122     int r = background.getRed();
mrs      123     int g = background.getGreen();
mrs      124     int b = background.getBlue();
mrs      125     r = Math.max(0, r - 20);
mrs      126     g = Math.max(0, g - 20);
mrs      127     b = Math.max(0, b - 20);
mrs      128     return new Color(r, g, b);
mrs      129   }
mrs      130 
mrs      131   private Color lighten(Color background) {
mrs      132     int r = background.getRed();
mrs      133     int g = background.getGreen();
mrs      134     int b = background.getBlue();
mrs      135     r = Math.min(255, r + 20);
mrs      136     g = Math.min(255, g + 20);
mrs      137     b = Math.min(255, b + 20);
mrs      138     return new Color(r, g, b);
mrs      139   }
mrs      140 
mrs      141   private Map<Integer, GradientPaint> gradientCache = new HashMap<Integer, GradientPaint>();
mrs      142 
mrs      143   private final static String evenList = "List.evenRowBackgroundPainter";
mrs      144   private final static String oddList = "List.oddRowBackgroundPainter";
mrs      145 
mrs      146   public void paintComponent(Graphics g) {
mrs      147     boolean painted = false;
mrs      148     if(g != null) {
mrs      149       if(JConfig.queryDisplayProperty("background.complex", "false").equals("true")) {
mrs      150         Graphics2D g2d = (Graphics2D) g;
mrs      151         Rectangle bounds = g2d.getClipBounds();
mrs      152         if (bounds != null) {
mrs      153           if (!mSelected) {
mrs      154             setOpaque(false);
mrs      155             GradientPaint paint = getGradientPaint();
mrs      156             g2d.setPaint(paint);
mrs      157           } else {
mrs      158             g.setColor(getBackground());
mrs      159           }
mrs      160           g2d.fillRect((int) bounds.getX(), (int) bounds.getY(), (int) bounds.getWidth(), (int) bounds.getHeight());
mrs      161         }
mrs      162       } else {
mrs      163         if(Platform.isMac()) {
mrs      164           if(mSelected) {
mrs      165             Color selected = UIManager.getColor("Table.selectionBackground");
mrs      166             renderGradient(g, selected);
mrs      167           } else {
mrs      168             Border bgPaint = UIManager.getBorder((mRow % 2) == 0 ? evenList : oddList);
mrs      169             bgPaint.paintBorder(this, g, 0, 0, getWidth(), getHeight());
mrs      170             super.paintComponent(g);
mrs      171             painted = true;
mrs      172 
mrs      173             Graphics2D g2d = (Graphics2D) g;
mrs      174             float alpha = .1f;
mrs      175             g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
mrs      176             g.setColor(Color.BLACK);
mrs      177             g.drawLine(0, getHeight()-1, getWidth(), getHeight()-1);
mrs      178           }
mrs      179         }
mrs      180       }
mrs      181       if(!painted) super.paintComponent(g);
mrs      182     }
mrs      183   }
mrs      184 
mrs      185   private void renderGradient(Graphics g, Color selected) {
mrs      186     GradientPaint paint;
mrs      187     paint = gradientCache.get(cacheMapper());
mrs      188     if(paint == null) {
mrs      189       paint = new GradientPaint(0, 0, lighten(selected), 0, getHeight(), selected, false);
mrs      190       gradientCache.put(cacheMapper(), paint);
mrs      191     }
mrs      192     Graphics2D g2d = (Graphics2D) g;
mrs      193     g2d.setPaint(paint);
mrs      194     Rectangle bounds = g2d.getClipBounds();
mrs      195     g2d.fillRect((int) bounds.getX(), (int) bounds.getY(), (int) bounds.getWidth(), (int) bounds.getHeight());
mrs      196   }
mrs      197 
mrs      198   private int cacheMapper() {return 10000 * (mRow % 2) + getHeight();}
mrs      199 
mrs      200   private GradientPaint getGradientPaint() {
mrs      201     GradientPaint paint = gradientCache.get(cacheMapper());
mrs      202     if(paint == null) {
mrs      203       if ((mRow % 2) == 0) {
mrs      204         paint = new GradientPaint(0, 0, Color.WHITE, 0, getHeight(), Color.LIGHT_GRAY, false);
mrs      205       } else {
mrs      206         paint = new GradientPaint(0, 0, Color.LIGHT_GRAY, 0, getHeight(), Color.WHITE, false);
mrs      207       }
mrs      208     }
mrs      209     gradientCache.put(cacheMapper(), paint);
mrs      210     return paint;
mrs      211   }
mrs      212 
mrs      213   private Colors getSelectionColors(int column, AuctionEntry ae, Color foreground, Color background) {
mrs      214     if (JConfig.queryConfiguration("selection.invert", "false").equals("true")) {
mrs      215       Color tmp = foreground;
mrs      216       foreground = background;
mrs      217       background = tmp;
mrs      218     } else {
mrs      219       if (JConfig.queryConfiguration("selection.color") != null) {
mrs      220         if (selectionColorString == null || !selectionColorString.equals(JConfig.queryConfiguration("selection.color"))) {
mrs      221           selectionColorString = JConfig.queryConfiguration("selection.color");
mrs      222           selectionColor = MultiSnipe.reverseColor(selectionColorString);
mrs      223         }
mrs      224         background = selectionColor;
mrs      225       } else {
mrs      226         if (Platform.isMac() || Platform.isLinux()) {
mrs      227           if (column == 2 && ae.isMultiSniped()) foreground = background;
mrs      228           background = linuxSelection;
mrs      229         } else {
mrs      230           if (column == 2 && ae.isMultiSniped()) {
mrs      231             foreground = background;
mrs      232           } else if (foreground.equals(Color.BLACK)) {
mrs      233             foreground = SystemColor.textHighlightText;
mrs      234           }
mrs      235           background = SystemColor.textHighlight;
mrs      236         }
mrs      237       }
mrs      238     }
mrs      239     return new Colors(foreground, background);
mrs      240   }
mrs      241 
mrs      242   private Color chooseForeground(AuctionEntry ae, int col, Color foreground) {
cyberfox 243     switch(col) {
cyberfox 244       case TableColumnController.ID:
cyberfox 245         return chooseIDColor(ae);
cyberfox 246       case TableColumnController.SNIPE_OR_MAX:
cyberfox 247       case TableColumnController.SNIPE_TOTAL:
cyberfox 248         return snipeBidColor(ae);
cyberfox 249       case TableColumnController.TITLE:
cyberfox 250         return titleColor(ae);
cyberfox 251       case TableColumnController.CUR_BID:
cyberfox 252       default:
mrs      253         return (foreground == null) ? Color.BLACK : foreground;
cyberfox 254     }
cyberfox 255   }
cyberfox 256 
cyberfox 257   private Color chooseBackground(AuctionEntry ae, int col, Color default_color) {
cyberfox 258     Color ret = null;
cyberfox 259 
cyberfox 260     if(ae != null) {
cyberfox 261       if ((col == TableColumnController.SNIPE_OR_MAX || col == TableColumnController.SNIPE_TOTAL) && ae.isSniped()) {
cyberfox 262         ret = snipeBidBackground(ae);
cyberfox 263       }
cyberfox 264     }
cyberfox 265 
cyberfox 266     if(ret != null) return ret;
cyberfox 267     return default_color;
cyberfox 268   }
cyberfox 269 
mrs      270   private static Font sDefaultFont = null;
mrs      271   public static Font getDefaultFont() {
mrs      272     if(sDefaultFont == null) {
mrs      273       String cfgDefault = JConfig.queryConfiguration("default.font");
mrs      274       if(cfgDefault != null) {
mrs      275         sDefaultFont = Font.decode(cfgDefault);
mrs      276       }
mrs      277     }
mrs      278     return sDefaultFont;
mrs      279   }
mrs      280 
mrs      281   private static String getStyleName(int style) {
mrs      282     switch(style) {
mrs      283       case 1: return "bold";
mrs      284       case 2: return "italic";
mrs      285       case 3: return "bolditalic";
mrs      286       case 0:
mrs      287       default: return "plain";
mrs      288     }
mrs      289   }
mrs      290 
mrs      291   public static void setDefaultFont(Font defaultFont) {
mrs      292     String formattedFontName = defaultFont.getFamily() + "-" + getStyleName(defaultFont.getStyle()) + "-" + defaultFont.getSize();
mrs      293     JConfig.setConfiguration("default.font", formattedFontName);
mrs      294     sDefaultFont = defaultFont;
mrs      295     fixedFont = null;
mrs      296     boldFont = null;
mrs      297   }
mrs      298 
cyberfox 299   private Font chooseFont(Font base, AuctionEntry ae, int col) {
cyberfox 300     boolean hasComment = ae.getComment() != null;
mrs      301     if(sDefaultFont != null) base = sDefaultFont; else sDefaultFont = base;
cyberfox 302 
cyberfox 303     if(fixedFont == null) fixedFont = new Font("Monospaced", base.getStyle(), base.getSize());
cyberfox 304     if(boldFont == null) boldFont = base.deriveFont(Font.BOLD);
cyberfox 305     if(col == TableColumnController.TIME_LEFT) return fixedFont;
mrs      306     if(hasComment && col == TableColumnController.ID) return boldFont;
cyberfox 307     if(ae.isShippingOverridden() && col == TableColumnController.SHIPPING_INSURANCE) return boldFont;
cyberfox 308     return base;
cyberfox 309   }
cyberfox 310 
cyberfox 311   private Color snipeBidBackground(AuctionEntry ae) {
cyberfox 312     if (ae.isMultiSniped()) {
cyberfox 313       return ae.getMultiSnipe().getColor();
cyberfox 314     }
cyberfox 315     return null;
cyberfox 316   }
cyberfox 317 
mrs      318   private Color titleColor(AuctionEntry ae) {
cyberfox 319     if (ae != null && ae.getHighBidder() != null) {
cyberfox 320       if (ae.isHighBidder()) {
cyberfox 321         if (!ae.isReserve() || ae.isReserveMet()) {
cyberfox 322           return medBlue;
cyberfox 323         } else {
cyberfox 324           return darkRed;
cyberfox 325         }
cyberfox 326       } else {
cyberfox 327         if (ae.getNumBidders() > 0 && (!ae.isReserve() || ae.isReserveMet())) {
mrs      328           if (!ae.isSeller()) {
cyberfox 329             return darkRed;
cyberfox 330           } else {
cyberfox 331             return darkGreen;
cyberfox 332           }
cyberfox 333         }
cyberfox 334       }
cyberfox 335     }
cyberfox 336 
cyberfox 337     return Color.BLACK;
cyberfox 338   }
cyberfox 339 
cyberfox 340   private Color snipeBidColor(AuctionEntry ae) {
cyberfox 341     if(ae != null) {
cyberfox 342       if(ae.isSniped()) {
cyberfox 343         if (!ae.isMultiSniped()) {
cyberfox 344           if (ae.isSnipeValid() || ae.isDutch()) {
cyberfox 345             return darkGreen;
cyberfox 346           }
cyberfox 347           return darkRed;
cyberfox 348         }
cyberfox 349         if (ae.snipeCancelled()) {
cyberfox 350           return darkRed;
cyberfox 351         }
cyberfox 352       } else if (ae.isBidOn()) {
cyberfox 353         if(ae.isHighBidder()) return medBlue;
cyberfox 354         return darkRed;
cyberfox 355       } else if (ae.snipeCancelled()) {
cyberfox 356         return darkRed;
cyberfox 357       }
cyberfox 358     }
cyberfox 359     return Color.BLACK;
cyberfox 360   }
cyberfox 361 
cyberfox 362   private Color chooseIDColor(AuctionEntry ae) {
cyberfox 363     if(ae != null) {
cyberfox 364       boolean recent = ae.getJustAdded() != 0;
cyberfox 365       boolean isUpdating = ae.isUpdating();
cyberfox 366 
cyberfox 367       if (recent) {
cyberfox 368         return darkGreen;
cyberfox 369       } else if (isUpdating) {
cyberfox 370         return darkRed;
cyberfox 371       }
cyberfox 372     }
cyberfox 373     return Color.BLACK;
cyberfox 374   }
cyberfox 375 }

Check out the code: svn co jbidwatcher/trunk/src/com/jbidwatcher/ui/myTableCellRenderer.java