svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / legend / edition / gui / ValueCellEditor.java @ 40558
History | View | Annotate | Download (6.04 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.app.project.documents.view.legend.edition.gui; |
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 |
* Cell Editor sobre los valores ?nicos.
|
40 |
*
|
41 |
* @author Vicente Caballero Navarro
|
42 |
*/
|
43 |
public class ValueCellEditor extends JTextField implements TableCellEditor { |
44 |
private ArrayList listeners = new ArrayList(); |
45 |
//private Value initialValue;
|
46 |
|
47 |
/**
|
48 |
* Crea un nuevo FLabelCellEditor.
|
49 |
*/
|
50 |
public ValueCellEditor() {
|
51 |
addKeyListener(new KeyAdapter() { |
52 |
public void keyReleased(KeyEvent e) { |
53 |
if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
54 |
stopCellEditing(); |
55 |
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { |
56 |
cancelCellEditing(); |
57 |
} |
58 |
} |
59 |
}); |
60 |
} |
61 |
|
62 |
//Implement the one CellEditor method that AbstractCellEditor doesn't.
|
63 |
public Object getCellEditorValue() { |
64 |
return getValue(getText());
|
65 |
} |
66 |
|
67 |
//Implement the one method defined by TableCellEditor.
|
68 |
public Component getTableCellEditorComponent(JTable table, Object value, |
69 |
boolean isSelected, int row, int column) { |
70 |
//initialValue=(Value)value;
|
71 |
if (value == null) { |
72 |
setText("");
|
73 |
} else {
|
74 |
setText(value.toString()); |
75 |
} |
76 |
|
77 |
return this; |
78 |
} |
79 |
|
80 |
/**
|
81 |
* @see javax.swing.CellEditor#cancelCellEditing()
|
82 |
*/
|
83 |
public void cancelCellEditing() { |
84 |
//if (initialValue != null) {
|
85 |
// setText(initialValue.toString());
|
86 |
//}
|
87 |
|
88 |
for (int i = 0; i < listeners.size(); i++) { |
89 |
CellEditorListener l = (CellEditorListener) listeners.get(i); |
90 |
ChangeEvent evt = new ChangeEvent(this); |
91 |
l.editingCanceled(evt); |
92 |
} |
93 |
} |
94 |
|
95 |
/**
|
96 |
* @see javax.swing.CellEditor#stopCellEditing()
|
97 |
*/
|
98 |
public boolean stopCellEditing() { |
99 |
for (int i = 0; i < listeners.size(); i++) { |
100 |
CellEditorListener l = (CellEditorListener) listeners.get(i); |
101 |
ChangeEvent evt = new ChangeEvent(this); |
102 |
l.editingStopped(evt); |
103 |
} |
104 |
|
105 |
return true; |
106 |
} |
107 |
|
108 |
/**
|
109 |
* @see javax.swing.CellEditor#isCellEditable(java.util.EventObject)
|
110 |
*/
|
111 |
public boolean isCellEditable(EventObject anEvent) { |
112 |
return true; |
113 |
} |
114 |
|
115 |
/**
|
116 |
* @see javax.swing.CellEditor#shouldSelectCell(java.util.EventObject)
|
117 |
*/
|
118 |
public boolean shouldSelectCell(EventObject anEvent) { |
119 |
return true; |
120 |
} |
121 |
|
122 |
/**
|
123 |
* @see javax.swing.CellEditor#addCellEditorListener(javax.swing.event.CellEditorListener)
|
124 |
*/
|
125 |
public void addCellEditorListener(CellEditorListener l) { |
126 |
listeners.add(l); |
127 |
} |
128 |
|
129 |
/**
|
130 |
* @see javax.swing.CellEditor#removeCellEditorListener(javax.swing.event.CellEditorListener)
|
131 |
*/
|
132 |
public void removeCellEditorListener(CellEditorListener l) { |
133 |
listeners.remove(l); |
134 |
} |
135 |
|
136 |
/**
|
137 |
* DOCUMENT ME!
|
138 |
*
|
139 |
* @param s DOCUMENT ME!
|
140 |
*
|
141 |
* @return DOCUMENT ME!
|
142 |
*/
|
143 |
private Object getValue(String s) { |
144 |
Object val = null; |
145 |
//Value cero = null;
|
146 |
|
147 |
// try {
|
148 |
try {
|
149 |
val = new Integer(s);//ValueFactory.createValueByType(s, Types.INTEGER); |
150 |
|
151 |
return val;
|
152 |
} catch (NumberFormatException e) { |
153 |
} |
154 |
|
155 |
try {
|
156 |
val = new Long(s);//ValueFactory.createValueByType(s, Types.BIGINT); |
157 |
|
158 |
return val;
|
159 |
} catch (NumberFormatException e) { |
160 |
} |
161 |
|
162 |
try {
|
163 |
val = new Float(s);//ValueFactory.createValueByType(s, Types.FLOAT); |
164 |
|
165 |
return val;
|
166 |
} catch (NumberFormatException e) { |
167 |
} |
168 |
|
169 |
try {
|
170 |
val = new Double(s);//ValueFactory.createValueByType(s, Types.DOUBLE); |
171 |
|
172 |
return val;
|
173 |
} catch (NumberFormatException e) { |
174 |
} |
175 |
|
176 |
val = s;//ValueFactory.createValueByType(s, Types.LONGVARCHAR);
|
177 |
// } catch (ParseException e) {
|
178 |
// e.printStackTrace();
|
179 |
// }
|
180 |
|
181 |
/*try {
|
182 |
if (v instanceof DoubleValue) {
|
183 |
val = ValueFactory.createValue(Double.parseDouble(s));
|
184 |
cero = ValueFactory.createValue((double) 0);
|
185 |
} else if (v instanceof StringValue) {
|
186 |
val = ValueFactory.createValue(s);
|
187 |
cero = ValueFactory.createValue((String) "");
|
188 |
} else if (v instanceof LongValue) {
|
189 |
val = ValueFactory.createValue(Long.parseLong(s));
|
190 |
cero = ValueFactory.createValue((long) 0);
|
191 |
} else if (v instanceof IntValue) {
|
192 |
val = ValueFactory.createValue(Integer.parseInt(s));
|
193 |
cero = ValueFactory.createValue((int) 0);
|
194 |
} else if (v instanceof FloatValue) {
|
195 |
val = ValueFactory.createValue(Float.parseFloat(s));
|
196 |
cero = ValueFactory.createValue((float) 0);
|
197 |
} else if (v instanceof ShortValue) {
|
198 |
val = ValueFactory.createValue(Short.parseShort(s));
|
199 |
cero = ValueFactory.createValue((short) 0);
|
200 |
} else if (v instanceof BooleanValue) {
|
201 |
val = ValueFactory.createValue(Boolean.getBoolean(s));
|
202 |
cero = ValueFactory.createValue((boolean) false);
|
203 |
} else if (v instanceof DateValue) {
|
204 |
val = ValueFactory.createValue(Date.parse(s));
|
205 |
cero = ValueFactory.createValue((Date) new Date());
|
206 |
}
|
207 |
} catch (NumberFormatException e) {
|
208 |
///JOptionPane.showMessageDialog(null, PluginServices...getText(this,"Formato de n?mero erroneo")+".");
|
209 |
return cero;
|
210 |
}
|
211 |
*/
|
212 |
return val;
|
213 |
} |
214 |
} |