Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / cellrenderers / BooleanTableCellRenderer.java @ 13136

History | View | Annotate | Download (3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.gui.beans.swing.cellrenderers;
42

    
43
import java.awt.Component;
44
import java.awt.FlowLayout;
45

    
46
import javax.swing.BorderFactory;
47
import javax.swing.JCheckBox;
48
import javax.swing.JPanel;
49
import javax.swing.JTable;
50
import javax.swing.border.MatteBorder;
51
import javax.swing.table.DefaultTableCellRenderer;
52

    
53
/**
54
 * @author jaume dominguez faus - jaume.dominguez@iver.es
55
 */
56
public class BooleanTableCellRenderer extends DefaultTableCellRenderer {
57
        private JCheckBox chk;
58

    
59
        private boolean isBordered;
60
        private MatteBorder selectedBorder;
61
        private MatteBorder unselectedBorder;
62

    
63

    
64
        public BooleanTableCellRenderer(boolean bordered) {
65
                this.isBordered = bordered;
66
                setOpaque(true);
67
        }
68

    
69
        public JCheckBox getCheck() {
70
                return chk;
71
        }
72

    
73
        public Component getTableCellRendererComponent(JTable table, Object value,
74
                        boolean isSelected, boolean hasFocus, int row, int column) {
75
                if (value == null)
76
                        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
77

    
78
                if (isBordered) {
79
                        if (isSelected) {
80
                                if (selectedBorder == null) {
81
                                        selectedBorder = BorderFactory.createMatteBorder(2, 5, 2,
82
                                                        5, table.getSelectionBackground());
83
                                }
84

    
85
                                setBorder(selectedBorder);
86
                        } else {
87
                                if (unselectedBorder == null) {
88
                                        unselectedBorder = BorderFactory.createMatteBorder(2, 5, 2,
89
                                                        5, table.getBackground());
90
                                }
91

    
92
                                setBorder(unselectedBorder);
93
                        }
94
                }
95
                try {
96
                        Boolean v = (Boolean) value;
97
                        JPanel content = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
98
                        content.setBackground(table.getBackground());
99
                        chk = new JCheckBox("", v.booleanValue());
100
                        chk.setBackground(table.getBackground());
101
                        content.add(chk);
102
                        return content;
103
                } catch (ClassCastException ccEx) {
104
                        throw new RuntimeException("Trying to use a Boolean cell renderer with a non-Boolean datatype");
105
                }
106

    
107
        }
108

    
109
}