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
932 1022  
1616 import com.jbidwatcher.ui.table.TableColumnController;
1717 
1818 import javax.swing.*;
19 import javax.swing.border.Border;
1920 import javax.swing.table.DefaultTableCellRenderer;
2021 import java.awt.*;
2122 import java.util.HashMap;
------
7677     } else {
7778       setHorizontalAlignment(JLabel.LEFT);
7879     }
79     Component returnComponent = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
80     JComponent returnComponent = (JComponent)super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
8081 
8182     AuctionEntry ae = (AuctionEntry)table.getValueAt(row, -1);
83     returnComponent.setOpaque(false);
8284     if(ae == null) return returnComponent;
8385 
8486     Color foreground = chooseForeground(ae, column, table.getForeground());
85     Color background = chooseBackground(ae, column, table.getBackground());
8687 
8788     mRow = row;
8889 
89     if( (row % 2) == 1) {
90       if(darkBG == null) {
91         int r = background.getRed();
92         int g = background.getGreen();
93         int b = background.getBlue();
94         r = Math.max(0, r - 20);
95         g = Math.max(0, g - 20);
96         b = Math.max(0, b - 20);
97         darkBG = new Color(r, g, b);
98       }
90     if(!Platform.isMac()) {
91       Color background = chooseBackground(ae, column, table.getBackground());
92       if ((row % 2) == 1) {
93         if (darkBG == null) darkBG = darken(background);
9994 
100       if (column != 2 || !ae.isMultiSniped()) {
101       if ((column != TableColumnController.SNIPE_OR_MAX && column != TableColumnController.SNIPE_TOTAL) || !ae.isMultiSniped()) {
102           if(JConfig.queryConfiguration("display.alternate", "true").equals("true")) {
95         if (column != 2 || !ae.isMultiSniped()) {
96           if ((column != TableColumnController.SNIPE_OR_MAX && column != TableColumnController.SNIPE_TOTAL) || !ae.isMultiSniped()) {
97             if (JConfig.queryConfiguration("display.alternate", "true").equals("true")) {
98               background = darkBG;
99             }
100           }
103101         }
104           background = darkBG;
105         }
106102       }
103       if (isSelected) {
104         Colors selectionColors = getSelectionColors(column, ae, foreground, background);
105 
106         foreground = selectionColors.getForeground();
107         background = selectionColors.getBackground();
108       }
109       returnComponent.setBackground(background);
107110     }
108111 
109112     mSelected = isSelected;
110     if(isSelected) {
111       Colors selectionColors = getSelectionColors(column, ae, foreground, background);
112113 
113       foreground = selectionColors.getForeground();
114       background = selectionColors.getBackground();
115     }
116 
117114     Font foo = chooseFont(returnComponent.getFont(), ae, column);
118115     returnComponent.setFont(foo);
119116     returnComponent.setForeground(foreground);
120     returnComponent.setBackground(background);
121117 
122118     return(returnComponent);
123119   }
124120 
121   private Color darken(Color background) {
122     int r = background.getRed();
123     int g = background.getGreen();
124     int b = background.getBlue();
125     r = Math.max(0, r - 20);
126     g = Math.max(0, g - 20);
127     b = Math.max(0, b - 20);
128     return new Color(r, g, b);
129   }
130 
131   private Color lighten(Color background) {
132     int r = background.getRed();
133     int g = background.getGreen();
134     int b = background.getBlue();
135     r = Math.min(255, r + 20);
136     g = Math.min(255, g + 20);
137     b = Math.min(255, b + 20);
138     return new Color(r, g, b);
139   }
140 
125141   private Map<Integer, GradientPaint> gradientCache = new HashMap<Integer, GradientPaint>();
126142 
143   private final static String evenList = "List.evenRowBackgroundPainter";
144   private final static String oddList = "List.oddRowBackgroundPainter";
145 
127146   public void paintComponent(Graphics g) {
147     boolean painted = false;
128148     if(g != null) {
129149       if(JConfig.queryDisplayProperty("background.complex", "false").equals("true")) {
130150         Graphics2D g2d = (Graphics2D) g;
------
139159           }
140160           g2d.fillRect((int) bounds.getX(), (int) bounds.getY(), (int) bounds.getWidth(), (int) bounds.getHeight());
141161         }
162       } else {
163         if(Platform.isMac()) {
164           if(mSelected) {
165             Color selected = UIManager.getColor("Table.selectionBackground");
166             renderGradient(g, selected);
167           } else {
168             Border bgPaint = UIManager.getBorder((mRow % 2) == 0 ? evenList : oddList);
169             bgPaint.paintBorder(this, g, 0, 0, getWidth(), getHeight());
170             super.paintComponent(g);
171             painted = true;
172 
173             Graphics2D g2d = (Graphics2D) g;
174             float alpha = .1f;
175             g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
176             g.setColor(Color.BLACK);
177             g.drawLine(0, getHeight()-1, getWidth(), getHeight()-1);
178           }
179         }
142180       }
143       super.paintComponent(g);
181       if(!painted) super.paintComponent(g);
144182     }
145183   }
146184 
185   private void renderGradient(Graphics g, Color selected) {
186     GradientPaint paint;
187     paint = gradientCache.get(cacheMapper());
188     if(paint == null) {
189       paint = new GradientPaint(0, 0, lighten(selected), 0, getHeight(), selected, false);
190       gradientCache.put(cacheMapper(), paint);
191     }
192     Graphics2D g2d = (Graphics2D) g;
193     g2d.setPaint(paint);
194     Rectangle bounds = g2d.getClipBounds();
195     g2d.fillRect((int) bounds.getX(), (int) bounds.getY(), (int) bounds.getWidth(), (int) bounds.getHeight());
196   }
197 
147198   private int cacheMapper() {return 10000 * (mRow % 2) + getHeight();}
148199 
149200   private GradientPaint getGradientPaint() {