Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / cellrenderers / NumberTableCellRenderer.java @ 13191

History | View | Annotate | Download (3.77 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.JPanel;
48
import javax.swing.JTable;
49
import javax.swing.border.MatteBorder;
50
import javax.swing.table.DefaultTableCellRenderer;
51

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

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

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

    
66
        private boolean acceptsDoubles;
67

    
68

    
69

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

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

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

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

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

    
103
                        JPanel content = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
104
                        content.setBackground(table.getBackground());
105
                        Validator valid = acceptsDoubles ? ValidatingTextField.DOUBLE_VALIDATOR : ValidatingTextField.INTEGER_VALIDATOR;
106
                        txt = new JIncrementalNumberField("", 6,valid, ValidatingTextField.NUMBER_CLEANER, 0, 80, 1);
107
                        if (acceptsDoubles) {
108
                                txt.setDouble(((Double) value).doubleValue());
109
                        } else {
110
                                txt.setInteger(((Integer) value).intValue());
111
                        }
112

    
113
                        txt.setBackground(table.getBackground());
114
                        // TODO figure out a way to know the cell's width to fit in the editor
115
//                        txt.setPreferredSize(new Dimension(
116
//                                        table.getColumnModel().getColumn(column).getPreferredWidth(),
117
//                                        table.getRowHeight()));
118
                        content.add(txt);
119
                        return content;
120
                } catch (ClassCastException ccEx) {
121
                        throw new RuntimeException("Trying to use a numeric cell renderer with a non-numeric datatype");
122
                }
123

    
124
        }
125
}