01: package com.salmonllc.swing.table;
02:
03: import com.salmonllc.sql.DataStoreEvaluator;
04: import com.salmonllc.sql.DataStoreException;
05: import com.salmonllc.swing.STable;
06:
07: import javax.swing.*;
08: import javax.swing.table.TableCellRenderer;
09: import java.awt.*;
10: import java.net.URL;
11: import java.net.MalformedURLException;
12:
13: public class ImageCellRenderer implements TableCellRenderer {
14:
15: DataStoreEvaluator _mainEval;
16:
17: public ImageCellRenderer(DataStoreEvaluator mainEval) {
18: _mainEval = mainEval;
19: }
20:
21: public Component getTableCellRendererComponent(JTable table,
22: Object value, boolean isSelected, boolean hasFocus,
23: int row, int column) {
24: Image img = evalExpression(row);
25: JLabel lab = new JLabel(new ImageIcon(img));
26: STable tab = (STable) table;
27:
28: if (!isSelected) {
29: lab.setBackground(tab.getBackground(row));
30: lab.setForeground(tab.getForeground(row));
31: } else {
32: lab.setBackground(tab.getSelectionBackground(row));
33: lab.setForeground(tab.getSelectionForeground(row));
34: lab.setOpaque(true);
35: }
36: return lab;
37: }
38:
39: private Image evalExpression(int row) {
40: Object o = null;
41: try {
42: if (_mainEval != null)
43: o = _mainEval.evaluateRow(row);
44:
45: Image newImage = null;
46: if (o instanceof String) {
47: URL location = new URL((String) o);
48: newImage = Toolkit.getDefaultToolkit().getImage(
49: location);
50: } else if (o instanceof byte[]) {
51: newImage = Toolkit.getDefaultToolkit().createImage(
52: (byte[]) o);
53: }
54: return newImage;
55: } catch (DataStoreException ex) {
56: ex.printStackTrace();
57: } catch (MalformedURLException ex) {
58: ex.printStackTrace();
59: }
60: return null;
61: }
62:
63: }
|