svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / swing / jtable / TextFieldCellEditor.java @ 40561
History | View | Annotate | Download (3.87 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.utils.swing.jtable; |
25 |
|
26 |
import java.awt.Component; |
27 |
import java.awt.event.KeyAdapter; |
28 |
import java.awt.event.KeyEvent; |
29 |
import java.util.ArrayList; |
30 |
import java.util.EventObject; |
31 |
|
32 |
import javax.swing.JTable; |
33 |
import javax.swing.JTextField; |
34 |
import javax.swing.event.CellEditorListener; |
35 |
import javax.swing.event.ChangeEvent; |
36 |
import javax.swing.table.TableCellEditor; |
37 |
|
38 |
/**
|
39 |
* @author Fernando Gonz?lez Cort?s
|
40 |
*/
|
41 |
public class TextFieldCellEditor extends JTextField implements TableCellEditor{ |
42 |
|
43 |
private ArrayList listeners = new ArrayList(); |
44 |
private String initialValue; |
45 |
|
46 |
public TextFieldCellEditor() {
|
47 |
addKeyListener(new KeyAdapter() { |
48 |
public void keyReleased(KeyEvent e) { |
49 |
if (e.getKeyCode() == KeyEvent.VK_ENTER){ |
50 |
stopCellEditing(); |
51 |
}else if (e.getKeyCode() == KeyEvent.VK_ESCAPE){ |
52 |
cancelCellEditing(); |
53 |
} |
54 |
} |
55 |
}); |
56 |
} |
57 |
|
58 |
/**
|
59 |
* @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
|
60 |
*/
|
61 |
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { |
62 |
this.setText(value == null ? "" : value.toString()); |
63 |
return this; |
64 |
} |
65 |
|
66 |
/**
|
67 |
* @see javax.swing.CellEditor#cancelCellEditing()
|
68 |
*/
|
69 |
public void cancelCellEditing() { |
70 |
setText(initialValue); |
71 |
for (int i = 0; i < listeners.size(); i++) { |
72 |
CellEditorListener l = (CellEditorListener) listeners.get(i); |
73 |
ChangeEvent evt = new ChangeEvent(this); |
74 |
l.editingCanceled(evt); |
75 |
} |
76 |
} |
77 |
|
78 |
/**
|
79 |
* @see javax.swing.CellEditor#stopCellEditing()
|
80 |
*/
|
81 |
public boolean stopCellEditing() { |
82 |
for (int i = 0; i < listeners.size(); i++) { |
83 |
CellEditorListener l = (CellEditorListener) listeners.get(i); |
84 |
ChangeEvent evt = new ChangeEvent(this); |
85 |
l.editingStopped(evt); |
86 |
} |
87 |
|
88 |
return true; |
89 |
} |
90 |
|
91 |
/**
|
92 |
* @see javax.swing.CellEditor#getCellEditorValue()
|
93 |
*/
|
94 |
public Object getCellEditorValue() { |
95 |
return getText();
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* @see javax.swing.CellEditor#isCellEditable(java.util.EventObject)
|
100 |
*/
|
101 |
public boolean isCellEditable(EventObject anEvent) { |
102 |
return true; |
103 |
} |
104 |
|
105 |
/**
|
106 |
* @see javax.swing.CellEditor#shouldSelectCell(java.util.EventObject)
|
107 |
*/
|
108 |
public boolean shouldSelectCell(EventObject anEvent) { |
109 |
return false; |
110 |
} |
111 |
|
112 |
/**
|
113 |
* @see javax.swing.CellEditor#addCellEditorListener(javax.swing.event.CellEditorListener)
|
114 |
*/
|
115 |
public void addCellEditorListener(CellEditorListener l) { |
116 |
listeners.add(l); |
117 |
} |
118 |
|
119 |
/**
|
120 |
* @see javax.swing.CellEditor#removeCellEditorListener(javax.swing.event.CellEditorListener)
|
121 |
*/
|
122 |
public void removeCellEditorListener(CellEditorListener l) { |
123 |
listeners.remove(l); |
124 |
} |
125 |
|
126 |
|
127 |
} |