Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / cellrenderers / NumberTableCellRenderer.java @ 13655

History | View | Annotate | Download (3.84 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 static final long serialVersionUID = -4551232953341602636L;
61

    
62
        private JIncrementalNumberField txt;
63

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

    
68
        private boolean acceptsDoubles;
69

    
70

    
71

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

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

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

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

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

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

    
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 numeric cell renderer with a non-numeric datatype");
124
                }
125

    
126
        }
127
}