01: package org.swingml.tablebrowser.ext;
02:
03: import java.awt.*;
04: import java.net.*;
05: import java.util.*;
06:
07: import javax.swing.*;
08: import javax.swing.table.*;
09:
10: import org.swingml.*;
11: import org.swingml.component.*;
12: import org.swingml.model.*;
13:
14: /**
15: * @author Crosslogic
16: */
17: public class TableBrowserCellLabelRenderer implements TableCellRenderer {
18:
19: static WeakHashMap iconCache;
20: TableCellRenderer originalRenderer;
21:
22: public TableBrowserCellLabelRenderer(TableCellRenderer aRenderer) {
23: this .originalRenderer = aRenderer;
24: }
25:
26: protected Icon getIconForUrl(String url) {
27: Icon result = null;
28: if (iconCache == null) {
29: iconCache = new WeakHashMap();
30: }
31: if (iconCache.containsKey(url)) {
32: result = (Icon) iconCache.get(url);
33: } else {
34: try {
35: result = new ImageIcon(new URL(url));
36: iconCache.put(url, result);
37: } catch (MalformedURLException e) {
38: }
39: }
40: return result;
41: }
42:
43: public Component getTableCellRendererComponent(JTable table,
44: Object value, boolean isSelected, boolean hasFocus,
45: int row, int column) {
46: JLabel component = (JLabel) originalRenderer
47: .getTableCellRendererComponent(table, value,
48: isSelected, hasFocus, row, column);
49: TableBrowserComponent tbc = (TableBrowserComponent) table;
50: JTableModel model = (JTableModel) table.getModel();
51: TableDataModel cellModel = model.getDataModel(tbc
52: .convertRowIndexToModel(row), tbc
53: .convertColumnIndexToModel(column));
54: LabelDecorator.decorateLabel(component, cellModel, table
55: .getFont());
56: if (isSelected) {
57: component.setBackground(table.getSelectionBackground());
58: component.setForeground(table.getSelectionForeground());
59: }
60: if (hasFocus
61: && component.getBackground() != null
62: && table.getBackground() != null
63: && !component.getBackground().equals(
64: table.getBackground())) {
65: component.setBackground(new Color(component.getBackground()
66: .getRed(), component.getBackground().getGreen(),
67: component.getBackground().getBlue(), 125));
68: }
69:
70: // Set alignment
71: int alignment = JLabel.LEFT;
72: if (cellModel.getAlignment().equalsIgnoreCase(Constants.RIGHT)) {
73: alignment = JLabel.RIGHT;
74: } else if (cellModel.getAlignment().equalsIgnoreCase(
75: Constants.CENTER)) {
76: alignment = JLabel.CENTER;
77: }
78: component.setHorizontalAlignment(alignment);
79:
80: if (!table.isEnabled()) {
81: component.setForeground(Color.LIGHT_GRAY);
82: }
83:
84: return component;
85: }
86: }
|