root / tags / v1_0_2_Build_896 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / panels / JPanelFieldDefinition.java @ 10391
History | View | Annotate | Download (11.6 KB)
1 |
package com.iver.cit.gvsig.gui.cad.panels; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Component; |
5 |
import java.sql.Types; |
6 |
import java.util.ArrayList; |
7 |
|
8 |
import javax.swing.DefaultCellEditor; |
9 |
import javax.swing.JButton; |
10 |
import javax.swing.JComboBox; |
11 |
import javax.swing.JLabel; |
12 |
import javax.swing.JOptionPane; |
13 |
import javax.swing.JPanel; |
14 |
import javax.swing.JScrollPane; |
15 |
import javax.swing.JTable; |
16 |
import javax.swing.ListSelectionModel; |
17 |
import javax.swing.event.ListSelectionEvent; |
18 |
import javax.swing.event.ListSelectionListener; |
19 |
import javax.swing.table.DefaultTableModel; |
20 |
import javax.swing.table.TableColumn; |
21 |
|
22 |
import jwizardcomponent.JWizardComponents; |
23 |
import jwizardcomponent.JWizardPanel; |
24 |
|
25 |
import com.iver.andami.PluginServices; |
26 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
27 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
28 |
|
29 |
/**
|
30 |
* @author fjp
|
31 |
*
|
32 |
* Panel para que el usuario seleccione el driver que va a utilizar para
|
33 |
* crear un tema desde cero.
|
34 |
*
|
35 |
*/
|
36 |
public class JPanelFieldDefinition extends JWizardPanel { |
37 |
|
38 |
|
39 |
private JLabel jLabel = null; |
40 |
private JScrollPane jScrollPane = null; |
41 |
private JTable jTable = null; |
42 |
private JPanel jPanelEast = null; |
43 |
private JButton jButtonAddField = null; |
44 |
private JButton jButtonDeleteField = null; |
45 |
private int MAX_FIELD_LENGTH = 254; |
46 |
|
47 |
private IWriter writer = null; |
48 |
|
49 |
|
50 |
public JPanelFieldDefinition(JWizardComponents wizardComponents) {
|
51 |
super(wizardComponents, null); |
52 |
initialize(); |
53 |
// TODO Auto-generated constructor stub
|
54 |
} |
55 |
|
56 |
|
57 |
/* (non-Javadoc)
|
58 |
* @see jwizardcomponent.JWizardPanel#next()
|
59 |
*/
|
60 |
public void next() { |
61 |
DefaultTableModel tm=(DefaultTableModel) jTable.getModel(); |
62 |
boolean valid=true; |
63 |
for (int i = 0;i<tm.getRowCount();i++) { |
64 |
String s=(String)tm.getValueAt(i,0); |
65 |
valid=validate(s); |
66 |
String size=(String) tm.getValueAt(i,2); |
67 |
valid=valid && validateInteger(size); |
68 |
if (!valid){
|
69 |
return;
|
70 |
} |
71 |
String type = (String) tm.getValueAt(i,1); |
72 |
int length = Integer.parseInt((String) tm.getValueAt(i,2)); |
73 |
if (type.equals("String") && length > MAX_FIELD_LENGTH) { |
74 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "max_length_is") + ": " + MAX_FIELD_LENGTH+ |
75 |
"\n"+PluginServices.getText(this, "length_of_field")+ " '"+ s + "' " + PluginServices.getText(this, "will_be_truncated")); |
76 |
tm.setValueAt(String.valueOf(MAX_FIELD_LENGTH),i,2); |
77 |
} |
78 |
|
79 |
} |
80 |
|
81 |
// ensure no field name is used more than once
|
82 |
ArrayList fieldNames = new ArrayList(); |
83 |
for (int i = 0; i < jTable.getRowCount(); i++) { |
84 |
if (fieldNames.contains(tm.getValueAt(i,0))) { |
85 |
valid = false;
|
86 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "two_or_more_fields_with_the_same_name")); |
87 |
break;
|
88 |
} |
89 |
fieldNames.add(tm.getValueAt(i, 0));
|
90 |
} |
91 |
|
92 |
if (valid)
|
93 |
super.next();
|
94 |
if (!((FileBasedPanel)getWizardComponents().getWizardPanel(2)).getPath().equals("")) |
95 |
setFinishButtonEnabled(true);
|
96 |
else
|
97 |
setFinishButtonEnabled(false);
|
98 |
} |
99 |
|
100 |
public void setWriter(IWriter writer) { |
101 |
this.writer = writer;
|
102 |
} |
103 |
|
104 |
public IWriter getWriter() {
|
105 |
return this.writer; |
106 |
} |
107 |
|
108 |
private boolean validateInteger(String size) { |
109 |
boolean valid=true; |
110 |
try{
|
111 |
Integer.parseInt(size);
|
112 |
}catch (NumberFormatException e) { |
113 |
valid=false;
|
114 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
115 |
PluginServices.getText(this,"no_puede_continuar")+"\n"+ |
116 |
PluginServices.getText(this,"size")+" : "+size+"\n"+ |
117 |
PluginServices.getText(this,"incorrect_value")); |
118 |
} |
119 |
return valid;
|
120 |
} |
121 |
|
122 |
|
123 |
private boolean validate(String s) { |
124 |
boolean valid=true; |
125 |
if (s.equals("")) { |
126 |
valid=false;
|
127 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
128 |
PluginServices.getText(this,"no_puede_continuar")+"\n"+ |
129 |
PluginServices.getText(this,"the_field_name_is_required")); |
130 |
} |
131 |
if (s.indexOf(" ")!=-1) { |
132 |
valid=false;
|
133 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
134 |
PluginServices.getText(this,"no_puede_continuar")+"\n"+ |
135 |
PluginServices.getText(this,"field")+" : "+s+"\n"+ |
136 |
PluginServices.getText(this,"contiene_espacios_en_blanco")); |
137 |
} |
138 |
if (this.writer != null && this.writer.getCapability("FieldNameMaxLength") != null) { |
139 |
String value = writer.getCapability("FieldNameMaxLength"); |
140 |
int intValue;
|
141 |
try {
|
142 |
intValue = Integer.parseInt(value);
|
143 |
} catch (NumberFormatException e) { |
144 |
intValue = 0;
|
145 |
} |
146 |
if (intValue > 0 && s.length() > intValue) { |
147 |
valid=false;
|
148 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
149 |
PluginServices.getText(this,"no_puede_continuar")+"\n"+ |
150 |
PluginServices.getText(this,"field")+" : "+s+"\n"+ |
151 |
PluginServices.getText(this,"too_long_name")+"\n"+ |
152 |
PluginServices.getText(this,"maximun_name_size")+" : "+intValue+"\n" |
153 |
); |
154 |
} |
155 |
} |
156 |
return valid;
|
157 |
} |
158 |
|
159 |
|
160 |
/**
|
161 |
* This method initializes this
|
162 |
*
|
163 |
*/
|
164 |
private void initialize() { |
165 |
jLabel = new JLabel(); |
166 |
jLabel.setText(PluginServices.getText(this,"define_fields")); |
167 |
this.setLayout(new BorderLayout(5,5)); |
168 |
this.setSize(new java.awt.Dimension(499,232)); |
169 |
this.add(jLabel, java.awt.BorderLayout.NORTH);
|
170 |
this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
|
171 |
this.add(getJPanelEast(), java.awt.BorderLayout.EAST);
|
172 |
} |
173 |
|
174 |
|
175 |
/**
|
176 |
* This method initializes jScrollPane
|
177 |
*
|
178 |
* @return javax.swing.JScrollPane
|
179 |
*/
|
180 |
private JScrollPane getJScrollPane() { |
181 |
if (jScrollPane == null) { |
182 |
jScrollPane = new JScrollPane(); |
183 |
jScrollPane.setViewportView(getJTable()); |
184 |
} |
185 |
return jScrollPane;
|
186 |
} |
187 |
|
188 |
|
189 |
/**
|
190 |
* This method initializes jTable
|
191 |
*
|
192 |
* @return javax.swing.JTable
|
193 |
*/
|
194 |
private JTable getJTable() { |
195 |
if (jTable == null) { |
196 |
jTable = new JTable(); |
197 |
|
198 |
DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
199 |
tm.addColumn(PluginServices.getText(this,"field")); |
200 |
|
201 |
|
202 |
// TableColumn fieldTypeColumn = new TableColumn(1);
|
203 |
// fieldTypeColumn.setHeaderValue("Type");
|
204 |
// jTable.addColumn(fieldTypeColumn);
|
205 |
tm.addColumn(PluginServices.getText(this,"type")); |
206 |
// MIRAR EL C?DIGO DEL BOT?N DE A?ADIR CAMPO PARA VER EL CellEditor con comboBox
|
207 |
|
208 |
|
209 |
|
210 |
/* TableColumn fieldLengthColumn = new TableColumn(2);
|
211 |
fieldLengthColumn.setHeaderValue("Length");
|
212 |
// fieldLengthColumn.setCellRenderer(new DefaultTableCellRenderer());
|
213 |
jTable.addColumn(fieldLengthColumn); */
|
214 |
tm.addColumn(PluginServices.getText(this,"length")); |
215 |
|
216 |
// Ask to be notified of selection changes.
|
217 |
ListSelectionModel rowSM = jTable.getSelectionModel();
|
218 |
rowSM.addListSelectionListener(new ListSelectionListener() { |
219 |
public void valueChanged(ListSelectionEvent e) { |
220 |
//Ignore extra messages.
|
221 |
if (e.getValueIsAdjusting()) return; |
222 |
|
223 |
ListSelectionModel lsm =
|
224 |
(ListSelectionModel)e.getSource();
|
225 |
if (lsm.isSelectionEmpty()) {
|
226 |
//no rows are selected
|
227 |
jButtonDeleteField.setEnabled(false);
|
228 |
} else {
|
229 |
// int selectedRow = lsm.getMinSelectionIndex();
|
230 |
//selectedRow is selected
|
231 |
jButtonDeleteField.setEnabled(true);
|
232 |
} |
233 |
} |
234 |
}); |
235 |
jTable.getColumn(PluginServices.getText(this,"field")).setWidth(180); |
236 |
|
237 |
|
238 |
} |
239 |
return jTable;
|
240 |
} |
241 |
|
242 |
|
243 |
/**
|
244 |
* This method initializes jPanelWest
|
245 |
*
|
246 |
* @return javax.swing.JPanel
|
247 |
*/
|
248 |
private JPanel getJPanelEast() { |
249 |
if (jPanelEast == null) { |
250 |
jPanelEast = new JPanel(); |
251 |
jPanelEast.setLayout(null);
|
252 |
jPanelEast.setPreferredSize(new java.awt.Dimension(170,100)); |
253 |
jPanelEast.add(getJButtonAddField(), null);
|
254 |
jPanelEast.add(getJButtonDeleteField(), null);
|
255 |
} |
256 |
return jPanelEast;
|
257 |
} |
258 |
|
259 |
|
260 |
/**
|
261 |
* This method initializes jButtonAddField
|
262 |
*
|
263 |
* @return javax.swing.JButton
|
264 |
*/
|
265 |
private JButton getJButtonAddField() { |
266 |
if (jButtonAddField == null) { |
267 |
jButtonAddField = new JButton(); |
268 |
jButtonAddField.setText(PluginServices.getText(this,"add_field")); |
269 |
jButtonAddField.setLocation(new java.awt.Point(7,5)); |
270 |
jButtonAddField.setSize(new java.awt.Dimension(145,23)); |
271 |
jButtonAddField.setPreferredSize(new java.awt.Dimension(100,26)); |
272 |
jButtonAddField.addActionListener(new java.awt.event.ActionListener() {
|
273 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
274 |
DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
275 |
|
276 |
// Figure out a suitable field name
|
277 |
ArrayList fieldNames = new ArrayList(); |
278 |
for (int i = 0; i < jTable.getRowCount(); i++) { |
279 |
fieldNames.add(tm.getValueAt(i, 0));
|
280 |
} |
281 |
String[] currentFieldNames = (String[]) fieldNames.toArray(new String[0]); |
282 |
String newField = PluginServices.getText(this, "field").replaceAll(" +", "_"); |
283 |
int index=0; |
284 |
for (int i = 0; i < currentFieldNames.length; i++) { |
285 |
if (currentFieldNames[i].startsWith(newField)) {
|
286 |
try {
|
287 |
index = Integer.parseInt(currentFieldNames[i].replaceAll(newField,"")); |
288 |
} catch (Exception ex) { /* we don't care */} |
289 |
} |
290 |
} |
291 |
String newFieldName = newField+(++index);
|
292 |
|
293 |
|
294 |
// Add a new row
|
295 |
Object[] newRow = new Object[tm.getColumnCount()]; |
296 |
newRow[0] = newFieldName;
|
297 |
newRow[1] = "String"; |
298 |
newRow[2] = "20"; |
299 |
tm.addRow(newRow); |
300 |
|
301 |
// Esto lo a?ado aqu? porque si no tiene registros, no hace caso. (Por eso no
|
302 |
// lo pongo en getJTable()
|
303 |
TableColumn typeColumn = jTable.getColumnModel().getColumn(1); |
304 |
JComboBox comboBox = new JComboBox(); |
305 |
comboBox.addItem("Boolean");
|
306 |
comboBox.addItem("Date");
|
307 |
comboBox.addItem("Integer");
|
308 |
comboBox.addItem("Double");
|
309 |
comboBox.addItem("String");
|
310 |
typeColumn.setCellEditor(new DefaultCellEditor(comboBox)); |
311 |
|
312 |
TableColumn widthColumn = jTable.getColumnModel().getColumn(2); |
313 |
|
314 |
// tm.setValueAt("NewField", tm.getRowCount()-1, 0);
|
315 |
} |
316 |
}); |
317 |
|
318 |
} |
319 |
return jButtonAddField;
|
320 |
} |
321 |
|
322 |
|
323 |
/**
|
324 |
* This method initializes jButton
|
325 |
*
|
326 |
* @return javax.swing.JButton
|
327 |
*/
|
328 |
private JButton getJButtonDeleteField() { |
329 |
if (jButtonDeleteField == null) { |
330 |
jButtonDeleteField = new JButton(); |
331 |
jButtonDeleteField.setText(PluginServices.getText(this,"delete_field")); |
332 |
jButtonDeleteField.setLocation(new java.awt.Point(7,33)); |
333 |
jButtonDeleteField.setSize(new java.awt.Dimension(145,23)); |
334 |
jButtonDeleteField.setEnabled(false);
|
335 |
jButtonDeleteField.addActionListener(new java.awt.event.ActionListener() {
|
336 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
337 |
int[] selecteds = jTable.getSelectedRows(); |
338 |
DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
339 |
|
340 |
for (int i=selecteds.length-1; i >=0; i--) |
341 |
tm.removeRow(selecteds[i]); |
342 |
} |
343 |
}); |
344 |
} |
345 |
return jButtonDeleteField;
|
346 |
} |
347 |
|
348 |
|
349 |
/**
|
350 |
* Convierte lo que hay en la tabla en una definici?n de campos
|
351 |
* adecuada para crear un LayerDefinition
|
352 |
* @return
|
353 |
*/
|
354 |
public FieldDescription[] getFieldsDescription() { |
355 |
DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
356 |
FieldDescription[] fieldsDesc = new FieldDescription[tm.getRowCount()]; |
357 |
|
358 |
for (int i=0; i < tm.getRowCount(); i++) |
359 |
{ |
360 |
fieldsDesc[i] = new FieldDescription();
|
361 |
fieldsDesc[i].setFieldName((String) tm.getValueAt(i,0)); |
362 |
String strType = (String) tm.getValueAt(i,1); |
363 |
if (strType.equals("String")) |
364 |
fieldsDesc[i].setFieldType(Types.VARCHAR);
|
365 |
if (strType.equals("Double")) |
366 |
fieldsDesc[i].setFieldType(Types.DOUBLE);
|
367 |
if (strType.equals("Integer")) |
368 |
fieldsDesc[i].setFieldType(Types.INTEGER);
|
369 |
if (strType.equals("Boolean")) |
370 |
fieldsDesc[i].setFieldType(Types.BOOLEAN);
|
371 |
if (strType.equals("Date")) |
372 |
fieldsDesc[i].setFieldType(Types.DATE);
|
373 |
int fieldLength = Integer.parseInt((String) tm.getValueAt(i,2)); |
374 |
fieldsDesc[i].setFieldLength(fieldLength); |
375 |
|
376 |
// TODO: HACERLO BIEN
|
377 |
if (strType.equals("Double")) |
378 |
fieldsDesc[i].setFieldDecimalCount(5);
|
379 |
|
380 |
} |
381 |
|
382 |
return fieldsDesc;
|
383 |
} |
384 |
|
385 |
|
386 |
|
387 |
} // @jve:decl-index=0:visual-constraint="10,10"
|