svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / cellrenderers / NumberTableCellRenderer.java @ 42079
History | View | Annotate | Download (3.71 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.Component; |
27 |
import java.awt.FlowLayout; |
28 |
|
29 |
import javax.swing.BorderFactory; |
30 |
import javax.swing.JPanel; |
31 |
import javax.swing.JTable; |
32 |
import javax.swing.border.MatteBorder; |
33 |
import javax.swing.table.DefaultTableCellRenderer; |
34 |
|
35 |
import org.gvsig.gui.beans.swing.JNumberSpinner; |
36 |
|
37 |
|
38 |
/**
|
39 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
40 |
*/
|
41 |
public class NumberTableCellRenderer extends DefaultTableCellRenderer { |
42 |
private static final long serialVersionUID = -4551232953341602636L; |
43 |
|
44 |
private JNumberSpinner txt;
|
45 |
|
46 |
private boolean isBordered; |
47 |
private MatteBorder selectedBorder; |
48 |
private MatteBorder unselectedBorder; |
49 |
|
50 |
private boolean acceptsDoubles; |
51 |
|
52 |
|
53 |
|
54 |
public NumberTableCellRenderer(boolean bordered, boolean acceptsDoubles) { |
55 |
this.isBordered = bordered;
|
56 |
this.acceptsDoubles = acceptsDoubles;
|
57 |
setOpaque(true);
|
58 |
} |
59 |
|
60 |
public JNumberSpinner getIncrementalNumberField() {
|
61 |
return txt;
|
62 |
} |
63 |
|
64 |
public Component getTableCellRendererComponent(JTable table, Object value, |
65 |
boolean isSelected, boolean hasFocus, int row, int column) { |
66 |
if (value == null) |
67 |
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
68 |
|
69 |
if (isBordered) {
|
70 |
if (isSelected) {
|
71 |
if (selectedBorder == null) { |
72 |
selectedBorder = BorderFactory.createMatteBorder(2, 5, 2, |
73 |
5, table.getSelectionBackground());
|
74 |
} |
75 |
|
76 |
setBorder(selectedBorder); |
77 |
} else {
|
78 |
if (unselectedBorder == null) { |
79 |
unselectedBorder = BorderFactory.createMatteBorder(2, 5, 2, |
80 |
5, table.getBackground());
|
81 |
} |
82 |
setBorder(unselectedBorder); |
83 |
} |
84 |
} |
85 |
try {
|
86 |
|
87 |
JPanel content = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); |
88 |
content.setBackground(table.getBackground()); |
89 |
|
90 |
int decimals = 0; |
91 |
if( this.acceptsDoubles ) { |
92 |
decimals = 3;
|
93 |
} |
94 |
txt = new JNumberSpinner(0.0,6, 0.0,80.0,1.0,decimals); |
95 |
// Validator valid = acceptsDoubles ? ValidatingTextField.DOUBLE_VALIDATOR : ValidatingTextField.INTEGER_VALIDATOR;
|
96 |
// txt = new JIncrementalNumberField("", 6,valid, ValidatingTextField.NUMBER_CLEANER, 0, 80, 1);
|
97 |
// if (acceptsDoubles) {
|
98 |
// txt.setDouble(((Double) value).doubleValue());
|
99 |
// } else {
|
100 |
// txt.setInteger(((Integer) value).intValue());
|
101 |
// }
|
102 |
|
103 |
txt.setBackground(table.getBackground()); |
104 |
// TODO figure out a way to know the cell's width to fit in the editor
|
105 |
// txt.setPreferredSize(new Dimension(
|
106 |
// table.getColumnModel().getColumn(column).getPreferredWidth(),
|
107 |
// table.getRowHeight()));
|
108 |
content.add(txt); |
109 |
return content;
|
110 |
} catch (ClassCastException ccEx) { |
111 |
throw new RuntimeException("Trying to use a numeric cell renderer with a non-numeric datatype"); |
112 |
} |
113 |
|
114 |
} |
115 |
} |