svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / dialogs / DlgFieldManager.java @ 6399
History | View | Annotate | Download (14.3 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.gui.dialogs; |
42 |
|
43 |
import java.sql.Types; |
44 |
|
45 |
import javax.swing.DefaultCellEditor; |
46 |
import javax.swing.JButton; |
47 |
import javax.swing.JComboBox; |
48 |
import javax.swing.JComponent; |
49 |
import javax.swing.JLabel; |
50 |
import javax.swing.JOptionPane; |
51 |
import javax.swing.JPanel; |
52 |
import javax.swing.JScrollPane; |
53 |
import javax.swing.JTable; |
54 |
import javax.swing.ListSelectionModel; |
55 |
import javax.swing.event.ListSelectionEvent; |
56 |
import javax.swing.event.ListSelectionListener; |
57 |
import javax.swing.table.AbstractTableModel; |
58 |
import javax.swing.table.DefaultTableModel; |
59 |
import javax.swing.table.TableColumn; |
60 |
import javax.swing.table.TableModel; |
61 |
|
62 |
import com.hardcode.driverManager.DriverLoadException; |
63 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
64 |
import com.iver.andami.PluginServices; |
65 |
import com.iver.andami.ui.mdiManager.View; |
66 |
import com.iver.andami.ui.mdiManager.ViewInfo; |
67 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
68 |
import com.iver.cit.gvsig.fmap.edition.EditableAdapter; |
69 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
70 |
import com.iver.cit.gvsig.fmap.edition.IEditableSource; |
71 |
|
72 |
public class DlgFieldManager extends JPanel implements View { |
73 |
|
74 |
/**
|
75 |
*
|
76 |
*/
|
77 |
private static final long serialVersionUID = -4284879326692474318L; |
78 |
|
79 |
ViewInfo viewInfo = null;
|
80 |
|
81 |
private JLabel jLabel = null; |
82 |
|
83 |
private JScrollPane jScrollPane = null; |
84 |
|
85 |
private JTable jTableFields = null; |
86 |
|
87 |
private JButton jBtnNewField = null; |
88 |
|
89 |
private JButton jBtnDeleteField = null; |
90 |
|
91 |
private JButton jBtnRenameField = null; |
92 |
|
93 |
private JButton jBtnOK = null; |
94 |
|
95 |
private JButton jBtnCancel = null; |
96 |
|
97 |
// private IFieldManager fieldManager;;
|
98 |
private EditableAdapter edAdapter = null; |
99 |
|
100 |
private class MyFieldDescription { |
101 |
boolean isOriginal;
|
102 |
|
103 |
FieldDescription fieldDesc; |
104 |
|
105 |
MyFieldDescription(FieldDescription fieldDesc, boolean isOriginal) {
|
106 |
this.fieldDesc = fieldDesc;
|
107 |
this.isOriginal = isOriginal;
|
108 |
} |
109 |
|
110 |
FieldDescription getFieldDescription() { |
111 |
return fieldDesc;
|
112 |
} |
113 |
|
114 |
boolean isOriginal() {
|
115 |
return isOriginal;
|
116 |
} |
117 |
|
118 |
} |
119 |
|
120 |
private class MyTableModel extends AbstractTableModel { |
121 |
IEditableSource myIes; |
122 |
|
123 |
public MyTableModel(IEditableSource ies) {
|
124 |
myIes = ies; |
125 |
} |
126 |
|
127 |
public int getColumnCount() { |
128 |
return 3; |
129 |
} |
130 |
|
131 |
public int getRowCount() { |
132 |
try {
|
133 |
return myIes.getRecordset().getFieldCount();
|
134 |
} catch (DriverLoadException e) {
|
135 |
e.printStackTrace(); |
136 |
} catch (DriverException e) {
|
137 |
e.printStackTrace(); |
138 |
} |
139 |
return 0; |
140 |
} |
141 |
|
142 |
public Object getValueAt(int rowIndex, int columnIndex) { |
143 |
FieldDescription[] myFields;
|
144 |
try {
|
145 |
myFields = myIes.getRecordset().getFieldsDescription(); |
146 |
FieldDescription aux = myFields[rowIndex]; |
147 |
switch (columnIndex) {
|
148 |
case 0: |
149 |
return aux.getFieldAlias();
|
150 |
case 1: |
151 |
String strType = null; |
152 |
int type = aux.getFieldType();
|
153 |
if (type == Types.VARCHAR) |
154 |
strType = "String";
|
155 |
if (type == Types.DOUBLE) |
156 |
strType = "Double";
|
157 |
if (type == Types.INTEGER) |
158 |
strType = "Integer";
|
159 |
if (type == Types.BOOLEAN) |
160 |
strType = "Boolean";
|
161 |
if (type == Types.DATE) |
162 |
strType = "Date";
|
163 |
|
164 |
return strType;
|
165 |
case 2: |
166 |
return new Integer(aux.getFieldLength()); |
167 |
} |
168 |
} catch (DriverLoadException e) {
|
169 |
e.printStackTrace(); |
170 |
} catch (DriverException e) {
|
171 |
e.printStackTrace(); |
172 |
} |
173 |
|
174 |
return null; |
175 |
} |
176 |
|
177 |
public Class getColumnClass(int columnIndex) { |
178 |
// TODO Auto-generated method stub
|
179 |
return super.getColumnClass(columnIndex); |
180 |
} |
181 |
|
182 |
public String getColumnName(int column) { |
183 |
switch (column) {
|
184 |
case 0: |
185 |
return PluginServices.getText(this, "FieldName"); |
186 |
case 1: |
187 |
return PluginServices.getText(this, "FieldType"); |
188 |
case 2: |
189 |
return PluginServices.getText(this, "FieldLength"); |
190 |
} |
191 |
return super.getColumnName(column); |
192 |
} |
193 |
|
194 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
195 |
return false; |
196 |
|
197 |
} |
198 |
|
199 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
200 |
FieldDescription[] myFields;
|
201 |
try {
|
202 |
myFields = myIes.getRecordset().getFieldsDescription(); |
203 |
FieldDescription fDesc = myFields[rowIndex]; |
204 |
|
205 |
switch (columnIndex) {
|
206 |
case 0: |
207 |
fDesc.setFieldAlias((String) aValue);
|
208 |
break;
|
209 |
case 1: |
210 |
String strType = (String) aValue; |
211 |
if (strType.equals("String")) |
212 |
fDesc.setFieldType(Types.VARCHAR);
|
213 |
if (strType.equals("Double")) |
214 |
fDesc.setFieldType(Types.DOUBLE);
|
215 |
if (strType.equals("Integer")) |
216 |
fDesc.setFieldType(Types.INTEGER);
|
217 |
if (strType.equals("Boolean")) |
218 |
fDesc.setFieldType(Types.BOOLEAN);
|
219 |
if (strType.equals("Date")) |
220 |
fDesc.setFieldType(Types.DATE);
|
221 |
break;
|
222 |
case 2: |
223 |
int fieldLength = ((Integer) aValue).intValue(); |
224 |
fDesc.setFieldLength(fieldLength); |
225 |
|
226 |
// TODO: HACERLO BIEN
|
227 |
if (fDesc.getFieldType() == Types.VARCHAR) |
228 |
fDesc.setFieldDecimalCount(5);
|
229 |
|
230 |
} |
231 |
} catch (DriverLoadException e) {
|
232 |
e.printStackTrace(); |
233 |
} catch (DriverException e) {
|
234 |
e.printStackTrace(); |
235 |
} |
236 |
|
237 |
} |
238 |
|
239 |
} |
240 |
|
241 |
/**
|
242 |
* This method initializes
|
243 |
*
|
244 |
*/
|
245 |
public DlgFieldManager(IEditableSource ies) {
|
246 |
super();
|
247 |
this.edAdapter = (EditableAdapter) ies;
|
248 |
initialize(); |
249 |
// Add a new row
|
250 |
TableModel tm;
|
251 |
try {
|
252 |
tm = new MyTableModel(ies);
|
253 |
getJTableFields().setModel(tm); |
254 |
// Esto lo a?ado aqu? porque si no tiene registros, no hace caso.
|
255 |
// (Por eso no
|
256 |
// lo pongo en getJTable()
|
257 |
TableColumn typeColumn = jTableFields.getColumnModel().getColumn(1); |
258 |
JComboBox comboBox = new JComboBox(); |
259 |
comboBox.addItem("Boolean");
|
260 |
comboBox.addItem("Date");
|
261 |
comboBox.addItem("Integer");
|
262 |
comboBox.addItem("Double");
|
263 |
comboBox.addItem("String");
|
264 |
typeColumn.setCellEditor(new DefaultCellEditor(comboBox)); |
265 |
|
266 |
TableColumn widthColumn = jTableFields.getColumnModel()
|
267 |
.getColumn(2);
|
268 |
} catch (DriverLoadException e) {
|
269 |
// TODO Auto-generated catch block
|
270 |
e.printStackTrace(); |
271 |
} |
272 |
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* This method initializes this
|
277 |
*
|
278 |
*/
|
279 |
private void initialize() { |
280 |
jLabel = new JLabel(); |
281 |
jLabel.setBounds(new java.awt.Rectangle(14, 9, 361, 34)); |
282 |
jLabel.setText("Puede a?adir, borrar o renombrar los campos:");
|
283 |
this.setLayout(null); |
284 |
this.setSize(new java.awt.Dimension(387, 327)); |
285 |
this.add(jLabel, null); |
286 |
this.add(getJScrollPane(), null); |
287 |
this.add(getJBtnNewField(), null); |
288 |
this.add(getJButton(), null); |
289 |
this.add(getJBtnRenameField(), null); |
290 |
this.add(getJBtnOK(), null); |
291 |
this.add(getJButton2(), null); |
292 |
|
293 |
} |
294 |
|
295 |
public ViewInfo getViewInfo() {
|
296 |
if (viewInfo == null) { |
297 |
viewInfo = new ViewInfo(ViewInfo.MODALDIALOG | ViewInfo.PALETTE
|
298 |
| ViewInfo.RESIZABLE); |
299 |
} |
300 |
return viewInfo;
|
301 |
} |
302 |
|
303 |
/**
|
304 |
* Convierte lo que hay en la tabla en una definici?n de campos adecuada
|
305 |
* para crear un LayerDefinition
|
306 |
*
|
307 |
* @return
|
308 |
*/
|
309 |
public FieldDescription[] getFieldsDescription() { |
310 |
DefaultTableModel tm = (DefaultTableModel) jTableFields.getModel(); |
311 |
FieldDescription[] fieldsDesc = new FieldDescription[tm.getRowCount()]; |
312 |
|
313 |
for (int i = 0; i < tm.getRowCount(); i++) { |
314 |
fieldsDesc[i] = new FieldDescription();
|
315 |
fieldsDesc[i].setFieldName((String) tm.getValueAt(i, 0)); |
316 |
String strType = (String) tm.getValueAt(i, 1); |
317 |
if (strType.equals("String")) |
318 |
fieldsDesc[i].setFieldType(Types.VARCHAR);
|
319 |
if (strType.equals("Double")) |
320 |
fieldsDesc[i].setFieldType(Types.DOUBLE);
|
321 |
if (strType.equals("Integer")) |
322 |
fieldsDesc[i].setFieldType(Types.INTEGER);
|
323 |
if (strType.equals("Boolean")) |
324 |
fieldsDesc[i].setFieldType(Types.BOOLEAN);
|
325 |
if (strType.equals("Date")) |
326 |
fieldsDesc[i].setFieldType(Types.DATE);
|
327 |
int fieldLength = Integer.parseInt((String) tm.getValueAt(i, 2)); |
328 |
fieldsDesc[i].setFieldLength(fieldLength); |
329 |
|
330 |
// TODO: HACERLO BIEN
|
331 |
if (strType.equals("Double")) |
332 |
fieldsDesc[i].setFieldDecimalCount(5);
|
333 |
|
334 |
} |
335 |
|
336 |
return fieldsDesc;
|
337 |
} |
338 |
|
339 |
/**
|
340 |
* This method initializes jScrollPane
|
341 |
*
|
342 |
* @return javax.swing.JScrollPane
|
343 |
*/
|
344 |
private JScrollPane getJScrollPane() { |
345 |
if (jScrollPane == null) { |
346 |
jScrollPane = new JScrollPane(); |
347 |
jScrollPane.setBounds(new java.awt.Rectangle(13, 53, 236, 203)); |
348 |
jScrollPane.setViewportView(getJTableFields()); |
349 |
} |
350 |
return jScrollPane;
|
351 |
} |
352 |
|
353 |
/**
|
354 |
* This method initializes jTableFields
|
355 |
*
|
356 |
* @return javax.swing.JTable
|
357 |
*/
|
358 |
private JTable getJTableFields() { |
359 |
if (jTableFields == null) { |
360 |
jTableFields = new JTable(); |
361 |
jTableFields |
362 |
.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
363 |
|
364 |
jTableFields.setColumnSelectionAllowed(false);
|
365 |
// Ask to be notified of selection changes.
|
366 |
ListSelectionModel rowSM = jTableFields.getSelectionModel();
|
367 |
rowSM.addListSelectionListener(new ListSelectionListener() { |
368 |
public void valueChanged(ListSelectionEvent e) { |
369 |
// Ignore extra messages.
|
370 |
if (e.getValueIsAdjusting())
|
371 |
return;
|
372 |
|
373 |
ListSelectionModel lsm = (ListSelectionModel) e.getSource(); |
374 |
if (lsm.isSelectionEmpty()) {
|
375 |
// no rows are selected
|
376 |
jBtnDeleteField.setEnabled(false);
|
377 |
} else {
|
378 |
jBtnDeleteField.setEnabled(true);
|
379 |
} |
380 |
if (jTableFields.getSelectedRows().length != 1) |
381 |
{ |
382 |
getJBtnRenameField().setEnabled(false);
|
383 |
} |
384 |
else
|
385 |
{ |
386 |
getJBtnRenameField().setEnabled(true);
|
387 |
} |
388 |
|
389 |
} |
390 |
}); |
391 |
|
392 |
} |
393 |
return jTableFields;
|
394 |
} |
395 |
|
396 |
/**
|
397 |
* This method initializes jBtnNewField
|
398 |
*
|
399 |
* @return javax.swing.JButton
|
400 |
*/
|
401 |
private JButton getJBtnNewField() { |
402 |
if (jBtnNewField == null) { |
403 |
jBtnNewField = new JButton(); |
404 |
jBtnNewField.setBounds(new java.awt.Rectangle(255, 60, 118, 25)); |
405 |
jBtnNewField.setText(PluginServices.getText(this, "New Field")); |
406 |
jBtnNewField.addActionListener(new java.awt.event.ActionListener() {
|
407 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
408 |
// Add a new row
|
409 |
FieldDescription fld = new FieldDescription();
|
410 |
fld.setFieldName("field" + jTableFields.getRowCount());
|
411 |
fld.setFieldType(Types.VARCHAR);
|
412 |
fld.setFieldLength(30);
|
413 |
try {
|
414 |
edAdapter.addField(fld); |
415 |
jTableFields.revalidate(); |
416 |
} catch (EditionException e1) {
|
417 |
// TODO Auto-generated catch block
|
418 |
e1.printStackTrace(); |
419 |
} |
420 |
} |
421 |
}); |
422 |
} |
423 |
return jBtnNewField;
|
424 |
} |
425 |
|
426 |
/**
|
427 |
* This method initializes jButton
|
428 |
*
|
429 |
* @return javax.swing.JButton
|
430 |
*/
|
431 |
private JButton getJButton() { |
432 |
if (jBtnDeleteField == null) { |
433 |
jBtnDeleteField = new JButton(); |
434 |
jBtnDeleteField.setBounds(new java.awt.Rectangle(255, 90, 118, 25)); |
435 |
jBtnDeleteField.setText("Delete Field");
|
436 |
jBtnDeleteField |
437 |
.addActionListener(new java.awt.event.ActionListener() {
|
438 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
439 |
int[] selecteds = jTableFields.getSelectedRows(); |
440 |
TableModel tm = jTableFields.getModel();
|
441 |
|
442 |
|
443 |
for (int i = selecteds.length - 1; i >= 0; i--) |
444 |
{ |
445 |
String fieldName = (String) tm.getValueAt(selecteds[i],0); |
446 |
try {
|
447 |
edAdapter.removeField(fieldName); |
448 |
} catch (EditionException e1) {
|
449 |
// TODO Auto-generated catch block
|
450 |
e1.printStackTrace(); |
451 |
} |
452 |
} |
453 |
jTableFields.revalidate(); |
454 |
|
455 |
} |
456 |
}); |
457 |
} |
458 |
return jBtnDeleteField;
|
459 |
} |
460 |
|
461 |
/**
|
462 |
* This method initializes jBtnRenameField
|
463 |
*
|
464 |
* @return javax.swing.JButton
|
465 |
*/
|
466 |
private JButton getJBtnRenameField() { |
467 |
if (jBtnRenameField == null) { |
468 |
jBtnRenameField = new JButton(); |
469 |
jBtnRenameField |
470 |
.setBounds(new java.awt.Rectangle(255, 120, 118, 25)); |
471 |
jBtnRenameField.setText("Rename Field...");
|
472 |
jBtnRenameField.addActionListener(new java.awt.event.ActionListener() {
|
473 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
474 |
int[] selecteds = jTableFields.getSelectedRows(); |
475 |
TableModel tm = jTableFields.getModel();
|
476 |
|
477 |
|
478 |
for (int i = selecteds.length - 1; i >= 0; i--) |
479 |
{ |
480 |
String fieldName = (String) tm.getValueAt(selecteds[i],0); |
481 |
try {
|
482 |
String newName = JOptionPane.showInputDialog( |
483 |
(JComponent) PluginServices.getMDIManager().getActiveView(),
|
484 |
PluginServices.getText(this, "please_insert_new_field_name"), |
485 |
fieldName |
486 |
); |
487 |
if (newName == null) return; |
488 |
|
489 |
edAdapter.renameField(fieldName, newName); |
490 |
} catch (EditionException e1) {
|
491 |
// TODO Auto-generated catch block
|
492 |
e1.printStackTrace(); |
493 |
} |
494 |
} |
495 |
jTableFields.repaint(); |
496 |
|
497 |
} |
498 |
}); |
499 |
} |
500 |
return jBtnRenameField;
|
501 |
} |
502 |
|
503 |
/**
|
504 |
* This method initializes jBtnOK
|
505 |
*
|
506 |
* @return javax.swing.JButton
|
507 |
*/
|
508 |
private JButton getJBtnOK() { |
509 |
if (jBtnOK == null) { |
510 |
jBtnOK = new JButton(); |
511 |
jBtnOK.setBounds(new java.awt.Rectangle(180, 285, 91, 23)); |
512 |
jBtnOK.setText("Aceptar");
|
513 |
jBtnOK.addActionListener(new java.awt.event.ActionListener() {
|
514 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
515 |
PluginServices.getMDIManager().closeView(DlgFieldManager.this); |
516 |
} |
517 |
}); |
518 |
} |
519 |
return jBtnOK;
|
520 |
} |
521 |
|
522 |
/**
|
523 |
* This method initializes jButton
|
524 |
*
|
525 |
* @return javax.swing.JButton
|
526 |
*/
|
527 |
private JButton getJButton2() { |
528 |
if (jBtnCancel == null) { |
529 |
jBtnCancel = new JButton(); |
530 |
jBtnCancel.setBounds(new java.awt.Rectangle(285, 285, 91, 23)); |
531 |
jBtnCancel.setText("Cancelar");
|
532 |
jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
|
533 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
534 |
PluginServices.getMDIManager().closeView(DlgFieldManager.this); |
535 |
} |
536 |
}); |
537 |
} |
538 |
return jBtnCancel;
|
539 |
} |
540 |
|
541 |
} // @jve:decl-index=0:visual-constraint="12,23"
|
542 |
|