Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.raster.legend / org.gvsig.raster.legend.swing / org.gvsig.raster.legend.swing.impl / src / main / java / org / gvsig / raster / swing / legend / impl / colortable / selector / PreviewerColorTableCellRenderer.java @ 44831

History | View | Annotate | Download (2.94 KB)

1
package org.gvsig.raster.swing.legend.impl.colortable.selector;
2

    
3
import java.awt.Color;
4
import java.awt.Component;
5
import java.awt.Graphics2D;
6
import java.awt.Rectangle;
7
import java.awt.image.BufferedImage;
8

    
9
import javax.swing.ImageIcon;
10
import javax.swing.JComponent;
11
import javax.swing.JLabel;
12
import javax.swing.JPanel;
13
import javax.swing.JTable;
14
import javax.swing.table.TableCellRenderer;
15

    
16
import org.gvsig.raster.lib.legend.api.colortable.ColorTable;
17

    
18

    
19
/**
20
 * @author fdiaz
21
 *
22
 */
23
public class PreviewerColorTableCellRenderer extends JLabel implements TableCellRenderer {
24

    
25
    /**
26
     *
27
     */
28
    private static final long serialVersionUID = 9027225839371919399L;
29
    private ColorTable colorTable;
30

    
31
    @Override
32
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
33
        int row, int column) {
34
        if (value instanceof ColorTable) {
35
            this.colorTable = (ColorTable)value;
36
            Rectangle cellRect = table.getCellRect(row, column, false);
37
            BufferedImage image = new BufferedImage((int)cellRect.getWidth(), (int)cellRect.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
38
            this.paintImage(image, isSelected);
39
            ImageIcon icon = new ImageIcon(image, this.colorTable.getName());
40
            this.setIcon(icon);
41
        }
42
        return this;
43
    }
44

    
45
    private void paintImage(BufferedImage image, boolean isSelected) {
46
        Graphics2D g = (Graphics2D)image.getGraphics();
47

    
48
        Rectangle area = new Rectangle(image.getWidth(), image.getHeight());
49

    
50
        int x1 = area.x;
51
        int x2 = area.x + area.width - 1;
52

    
53
        Color bgColor = new Color(224, 224, 224);
54
        for (int i = 0; (i * 4) <= area.width; i++) {
55
            for (int j = 0; (j * 4) <= area.height; j++) {
56
                if ((i + j) % 2 == 0)
57
                    g.setColor(Color.white);
58
                else
59
                    g.setColor(bgColor);
60
                g.fillRect(area.x + 1 + i * 4, area.y + 1 + j * 4, 4, 4);
61
            }
62
        }
63

    
64
        if (colorTable.getClasses().size() >= 1) {
65
            double min = colorTable.getClasses().get(0).getValue();
66
            double max = colorTable.getClasses().get(colorTable.getClasses().size() - 1).getValue();
67
            for (int i = area.x; i < (area.x + area.width); i++) {
68
                double pos = min + (((max - min) * (i - area.x)) / (area.width - 2));
69

    
70
                byte[] col3 = colorTable.getRGBA(pos);
71
                g.setColor(new Color(col3[0] & 0xff, col3[1] & 0xff, col3[2] & 0xff, col3[3] & 0xff));
72
                g.drawLine(i, area.y, i, area.y + area.height);
73
            }
74
        } else {
75
            g.setColor(new Color(224, 224, 224));
76
            g.fillRect(x1, area.y, x2 - x1, area.height); // - 1);
77
        }
78
        if (isSelected) { //?????
79
            g.setColor(Color.black);
80
        } else {
81
            g.setColor(new Color(96, 96, 96));
82
        }
83
        g.drawRect(x1, area.y, x2 - x1, area.height - 1);
84
    }
85

    
86
}