svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / cellrenderers / BooleanTableCellRenderer.java @ 40561
History | View | Annotate | Download (2.92 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.gui.beans.swing.cellrenderers; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.Component; |
28 |
import java.awt.Dimension; |
29 |
import java.awt.LayoutManager; |
30 |
|
31 |
import javax.swing.BorderFactory; |
32 |
import javax.swing.JCheckBox; |
33 |
import javax.swing.JPanel; |
34 |
import javax.swing.JTable; |
35 |
import javax.swing.border.MatteBorder; |
36 |
import javax.swing.table.DefaultTableCellRenderer; |
37 |
|
38 |
import org.jfree.layout.CenterLayout; |
39 |
|
40 |
/**
|
41 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
42 |
*/
|
43 |
public class BooleanTableCellRenderer extends DefaultTableCellRenderer { |
44 |
private static final long serialVersionUID = 2121615214282741840L; |
45 |
|
46 |
private JCheckBox chk; |
47 |
|
48 |
private boolean isBordered; |
49 |
private MatteBorder selectedBorder; |
50 |
private MatteBorder unselectedBorder; |
51 |
|
52 |
|
53 |
public BooleanTableCellRenderer(boolean bordered) { |
54 |
this.isBordered = bordered;
|
55 |
setOpaque(true);
|
56 |
} |
57 |
|
58 |
public JCheckBox getCheck() { |
59 |
return chk;
|
60 |
} |
61 |
|
62 |
public Component getTableCellRendererComponent(JTable table, Object value, |
63 |
boolean isSelected, boolean hasFocus, int row, int column) { |
64 |
if (value == null) |
65 |
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
66 |
|
67 |
if (isBordered) {
|
68 |
if (isSelected) {
|
69 |
if (selectedBorder == null) { |
70 |
selectedBorder = BorderFactory.createMatteBorder(2, 5, 2, |
71 |
5, table.getSelectionBackground());
|
72 |
} |
73 |
|
74 |
setBorder(selectedBorder); |
75 |
} else {
|
76 |
if (unselectedBorder == null) { |
77 |
unselectedBorder = BorderFactory.createMatteBorder(2, 5, 2, |
78 |
5, table.getBackground());
|
79 |
} |
80 |
|
81 |
setBorder(unselectedBorder); |
82 |
} |
83 |
} |
84 |
try {
|
85 |
Boolean v = (Boolean) value; |
86 |
JPanel content = new JPanel(new CenterLayout()); |
87 |
content.setBackground(table.getBackground()); |
88 |
chk = new JCheckBox("", v.booleanValue()); |
89 |
chk.setBackground(table.getBackground()); |
90 |
content.add(chk, BorderLayout.CENTER);
|
91 |
return content;
|
92 |
} catch (ClassCastException ccEx) { |
93 |
throw new RuntimeException("Trying to use a Boolean cell renderer with a non-Boolean datatype"); |
94 |
} |
95 |
|
96 |
} |
97 |
|
98 |
} |