Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / celleditors / BooleanTableCellEditor.java @ 40561

History | View | Annotate | Download (3.33 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.celleditors;
25

    
26
import java.awt.Component;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.util.ArrayList;
30
import java.util.EventObject;
31

    
32
import javax.swing.JCheckBox;
33
import javax.swing.JComponent;
34
import javax.swing.JTable;
35
import javax.swing.event.CellEditorListener;
36
import javax.swing.event.ChangeEvent;
37
import javax.swing.table.TableCellEditor;
38

    
39
import org.gvsig.gui.beans.swing.cellrenderers.BooleanTableCellRenderer;
40

    
41
/**
42
 * @author jaume dominguez faus - jaume.dominguez@iver.es
43
 */
44
public class BooleanTableCellEditor implements TableCellEditor {
45
        private BooleanTableCellRenderer renderer;
46
        private ArrayList listeners = new ArrayList();
47

    
48

    
49
        public BooleanTableCellEditor(final JTable ownerTable) {
50
                renderer = new BooleanTableCellRenderer(true);
51
        }
52

    
53
        public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) {
54
                if (value == null) return null;
55
                JComponent aux = (JComponent) renderer.getTableCellRendererComponent(table, value, isSelected, false, row, column);
56
                ((JCheckBox) aux.getComponent(0)).addActionListener(new ActionListener() {
57
                        public void actionPerformed(ActionEvent e) {
58
                                // Store the value to avoid the need of pressing ENTER
59
                                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4139078
60
                                table.editingStopped(new ChangeEvent(table));
61
                        }
62
                });
63
                return aux;
64
        }
65

    
66

    
67
        public void cancelCellEditing() {
68
                for (int i = 0; i < listeners.size(); i++) {
69
                CellEditorListener l = (CellEditorListener) listeners.get(i);
70
                ChangeEvent evt = new ChangeEvent(this);
71
                l.editingCanceled(evt);
72
            }
73
        }
74

    
75
        public boolean stopCellEditing() {
76
                for (int i = 0; i < listeners.size(); i++) {
77
            CellEditorListener l = (CellEditorListener) listeners.get(i);
78
            ChangeEvent evt = new ChangeEvent(this);
79
            l.editingStopped(evt);
80
        }
81
        return true;
82
        }
83

    
84

    
85
        public Object getCellEditorValue() {
86
                if (renderer!=null && renderer.getCheck()!=null)
87
                        return new Boolean(renderer.getCheck().isSelected());
88
                return null;
89
        }
90

    
91
        public boolean isCellEditable(EventObject anEvent) {
92
                return true;
93
        }
94

    
95
        public boolean shouldSelectCell(EventObject anEvent) {
96
                return true;
97
        }
98

    
99
        public void addCellEditorListener(CellEditorListener l) {
100
                listeners.add(l);
101
        }
102

    
103
        public void removeCellEditorListener(CellEditorListener l) {
104
                listeners.remove(l);
105
        }
106

    
107
}