Statistics
| Revision:

root / trunk / libraries / libUI / src / org / gvsig / gui / beans / swing / cellrenderers / NumberTableCellRenderer.java @ 11734

History | View | Annotate | Download (3.79 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.Dimension;
45
import java.awt.FlowLayout;
46

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

    
53
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
54
import org.gvsig.gui.beans.swing.ValidatingTextField;
55
import org.gvsig.gui.beans.swing.ValidatingTextField.Validator;
56

    
57
/**
58
 * @author jaume dominguez faus - jaume.dominguez@iver.es
59
 */
60
public class NumberTableCellRenderer extends DefaultTableCellRenderer {
61
        private JIncrementalNumberField txt;
62

    
63
        private boolean isBordered;
64
        private MatteBorder selectedBorder;
65
        private MatteBorder unselectedBorder;
66

    
67
        private boolean acceptsDoubles;
68

    
69

    
70

    
71
        public NumberTableCellRenderer(boolean bordered, boolean acceptsDoubles) {
72
                this.isBordered = bordered;
73
                this.acceptsDoubles = acceptsDoubles;
74
                setOpaque(true);
75
        }
76

    
77
        public JIncrementalNumberField getIncrementalNumberField() {
78
                return txt;
79
        }
80

    
81
        public Component getTableCellRendererComponent(JTable table, Object value,
82
                        boolean isSelected, boolean hasFocus, int row, int column) {
83
                if (value == null)
84
                        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
85

    
86
                if (isBordered) {
87
                        if (isSelected) {
88
                                if (selectedBorder == null) {
89
                                        selectedBorder = BorderFactory.createMatteBorder(2, 5, 2,
90
                                                        5, table.getSelectionBackground());
91
                                }
92

    
93
                                setBorder(selectedBorder);
94
                        } else {
95
                                if (unselectedBorder == null) {
96
                                        unselectedBorder = BorderFactory.createMatteBorder(2, 5, 2,
97
                                                        5, table.getBackground());
98
                                }
99

    
100
                                setBorder(unselectedBorder);
101
                        }
102
                }
103
                try {
104
                        String v;
105
                        if (acceptsDoubles) {
106
                                v = String.valueOf((Double) value);
107
                        } else {
108
                                v = String.valueOf((Integer) value);
109
                        }
110

    
111
                        JPanel content = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
112
                        content.setBackground(table.getBackground());
113
                        Validator valid = acceptsDoubles ? ValidatingTextField.DOUBLE_VALIDATOR : ValidatingTextField.INTEGER_VALIDATOR;
114
                        txt = new JIncrementalNumberField(v, 6,valid, ValidatingTextField.NUMBER_CLEANER, 0, 80, 1);
115
                        txt.setBackground(table.getBackground());
116
                        // TODO figure out a way to know the cell's width to fit in the editor
117
//                        txt.setPreferredSize(new Dimension(
118
//                                        table.getColumnModel().getColumn(column).getPreferredWidth(),
119
//                                        table.getRowHeight()));
120
                        content.add(txt);
121
                        return content;
122
                } catch (ClassCastException ccEx) {
123
                        throw new RuntimeException("Trying to use a Boolean cell renderer with a non-Boolean datatype");
124
                }
125

    
126
        }
127
}