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