svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / celleditors / StringTableCellEditor.java @ 40561
History | View | Annotate | Download (3.23 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.JComponent; |
33 |
import javax.swing.JTable; |
34 |
import javax.swing.JTextField; |
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.StringTableCellRenderer; |
40 |
|
41 |
public class StringTableCellEditor implements TableCellEditor { |
42 |
private StringTableCellRenderer renderer;
|
43 |
private ArrayList listeners = new ArrayList(); |
44 |
|
45 |
|
46 |
public StringTableCellEditor(final JTable ownerTable) { |
47 |
renderer = new StringTableCellRenderer();
|
48 |
} |
49 |
|
50 |
public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) { |
51 |
if (value == null) return null; |
52 |
JComponent aux = (JComponent) renderer.getTableCellRendererComponent(table, value, isSelected, false, row, column); |
53 |
((JTextField) aux).addActionListener(new ActionListener() { |
54 |
public void actionPerformed(ActionEvent e) { |
55 |
// Store the value to avoid the need of pressing ENTER
|
56 |
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4139078
|
57 |
table.editingStopped(new ChangeEvent(table)); |
58 |
} |
59 |
}); |
60 |
return aux;
|
61 |
} |
62 |
|
63 |
|
64 |
public void cancelCellEditing() { |
65 |
for (int i = 0; i < listeners.size(); i++) { |
66 |
CellEditorListener l = (CellEditorListener) listeners.get(i); |
67 |
ChangeEvent evt = new ChangeEvent(this); |
68 |
l.editingCanceled(evt); |
69 |
} |
70 |
} |
71 |
|
72 |
public boolean stopCellEditing() { |
73 |
for (int i = 0; i < listeners.size(); i++) { |
74 |
CellEditorListener l = (CellEditorListener) listeners.get(i); |
75 |
ChangeEvent evt = new ChangeEvent(this); |
76 |
l.editingStopped(evt); |
77 |
} |
78 |
return true; |
79 |
} |
80 |
|
81 |
|
82 |
public Object getCellEditorValue() { |
83 |
if (renderer!=null && renderer.getText()!=null) |
84 |
return new Boolean(renderer.getText()); |
85 |
return null; |
86 |
} |
87 |
|
88 |
public boolean isCellEditable(EventObject anEvent) { |
89 |
return true; |
90 |
} |
91 |
|
92 |
public boolean shouldSelectCell(EventObject anEvent) { |
93 |
return true; |
94 |
} |
95 |
|
96 |
public void addCellEditorListener(CellEditorListener l) { |
97 |
listeners.add(l); |
98 |
} |
99 |
|
100 |
public void removeCellEditorListener(CellEditorListener l) { |
101 |
listeners.remove(l); |
102 |
} |
103 |
|
104 |
} |