Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / dialogs / DlgFieldManager.java @ 6356

History | View | Annotate | Download (14.2 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
import java.util.ArrayList;
45

    
46
import javax.swing.DefaultCellEditor;
47
import javax.swing.JButton;
48
import javax.swing.JComboBox;
49
import javax.swing.JLabel;
50
import javax.swing.JPanel;
51
import javax.swing.JScrollPane;
52
import javax.swing.JTable;
53
import javax.swing.ListSelectionModel;
54
import javax.swing.event.ListSelectionEvent;
55
import javax.swing.event.ListSelectionListener;
56
import javax.swing.table.AbstractTableModel;
57
import javax.swing.table.DefaultTableModel;
58
import javax.swing.table.TableColumn;
59
import javax.swing.table.TableModel;
60

    
61
import com.hardcode.driverManager.DriverLoadException;
62
import com.hardcode.gdbms.engine.data.driver.DriverException;
63
import com.iver.andami.PluginServices;
64
import com.iver.andami.ui.mdiManager.View;
65
import com.iver.andami.ui.mdiManager.ViewInfo;
66
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
67
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
68
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
69

    
70
public class DlgFieldManager extends JPanel implements View {
71

    
72
        /**
73
         * 
74
         */
75
        private static final long serialVersionUID = -4284879326692474318L;
76
        ViewInfo viewInfo = null;
77
        private JLabel jLabel = null;
78
        private JScrollPane jScrollPane = null;
79
        private JTable jTableFields = null;
80
        private JButton jBtnNewField = null;
81
        private JButton jBtnDeleteField = null;
82
        private JButton jBtnRenameField = null;
83
        private JButton jBtnOK = null;
84
        private JButton jBtnCancel = null;
85
//        private IFieldManager fieldManager;;
86
        private EditableAdapter edAdapter = null;
87
        
88
        private class MyFieldDescription 
89
        {
90
                boolean isOriginal;
91
                FieldDescription fieldDesc;
92
                MyFieldDescription(FieldDescription fieldDesc, boolean isOriginal)
93
                {
94
                        this.fieldDesc = fieldDesc;                        
95
                        this.isOriginal = isOriginal;
96
                }
97
                
98
                FieldDescription getFieldDescription()
99
                {
100
                        return fieldDesc;
101
                }
102
                
103
                boolean isOriginal()
104
                {
105
                        return isOriginal;
106
                }
107
                
108
        }
109
        private class MyTableModel extends AbstractTableModel
110
        {
111
                ArrayList myFields = new ArrayList();
112
                public MyTableModel(FieldDescription[] originalFields)
113
                {
114
                        for (int i=0; i<originalFields.length; i++)
115
                        {
116
                                MyFieldDescription aux = new MyFieldDescription(originalFields[i], true);
117
                                myFields.add(aux);
118
                        }
119
                }
120

    
121
                public int getColumnCount() {
122
                        return 3;
123
                }
124

    
125
                public int getRowCount() {
126
                        return myFields.size();
127
                }
128

    
129
                public Object getValueAt(int rowIndex, int columnIndex) {
130
                        MyFieldDescription aux = (MyFieldDescription) myFields.get(rowIndex);
131
                        switch (columnIndex)
132
                        {
133
                        case 0:
134
                                return aux.getFieldDescription().getFieldAlias();
135
                        case 1:
136
                                String strType = null;
137
                                int type = aux.getFieldDescription().getFieldType();
138
                                if (type == Types.VARCHAR)
139
                                        strType = "String";
140
                                if (type == Types.DOUBLE)
141
                                        strType = "Double";
142
                                if (type == Types.INTEGER)
143
                                        strType = "Integer";
144
                                if (type == Types.BOOLEAN)
145
                                        strType = "Boolean";
146
                                if (type == Types.DATE)
147
                                        strType = "Date";
148

    
149
                                return strType;
150
                        case 2:
151
                                return new Integer(aux.getFieldDescription().getFieldLength());
152
                        }
153
                        return null;
154
                }
155

    
156
                public Class getColumnClass(int columnIndex) {
157
                        // TODO Auto-generated method stub
158
                        return super.getColumnClass(columnIndex);
159
                }
160

    
161
                public String getColumnName(int column) {
162
                        switch (column)
163
                        {
164
                        case 0:
165
                                return PluginServices.getText(this, "FieldName");
166
                        case 1:
167
                                return PluginServices.getText(this, "FieldType");
168
                        case 2:
169
                                return PluginServices.getText(this, "FieldLength");                                
170
                        }
171
                        return super.getColumnName(column);
172
                }
173

    
174
                public boolean isCellEditable(int rowIndex, int columnIndex) {
175
                        MyFieldDescription aux = (MyFieldDescription) myFields.get(rowIndex);
176
                        switch (columnIndex)
177
                        {
178
                        case 0:
179
                                return true;
180
                        case 1:
181
                        case 2:
182
                                return !aux.isOriginal;
183
                        }
184
                        return false;
185

    
186
                }
187

    
188
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
189
                        MyFieldDescription aux = (MyFieldDescription) myFields.get(rowIndex);
190
                        FieldDescription fDesc = aux.getFieldDescription();
191
                        switch (columnIndex)
192
                        {
193
                        case 0:
194
                                fDesc.setFieldAlias((String) aValue);
195
                                break;
196
                        case 1:
197
                                String strType = (String) aValue;
198
                                if (strType.equals("String"))
199
                                        fDesc.setFieldType(Types.VARCHAR);
200
                                if (strType.equals("Double"))
201
                                        fDesc.setFieldType(Types.DOUBLE);
202
                                if (strType.equals("Integer"))
203
                                        fDesc.setFieldType(Types.INTEGER);
204
                                if (strType.equals("Boolean"))
205
                                        fDesc.setFieldType(Types.BOOLEAN);
206
                                if (strType.equals("Date"))
207
                                        fDesc.setFieldType(Types.DATE);
208
                                break;
209
                        case 2:
210
                                int fieldLength = ((Integer) aValue).intValue();
211
                                fDesc.setFieldLength(fieldLength);
212

    
213
                                // TODO: HACERLO BIEN
214
                                if (fDesc.getFieldType() == Types.VARCHAR)
215
                                        fDesc.setFieldDecimalCount(5);
216

    
217
                        }
218
                }
219
                
220
        }
221
        /**
222
         * This method initializes 
223
         * 
224
         */
225
        public DlgFieldManager(IEditableSource ies) {
226
                super();
227
                this.edAdapter = (EditableAdapter) ies;
228
                initialize();
229
                // Add a new row
230
                TableModel tm;
231
                try {
232
                        tm = new MyTableModel(ies.getRecordset().getFieldsDescription());
233
                        getJTableFields().setModel(tm);
234
                        // Esto lo a?ado aqu? porque si no tiene registros, no hace caso. (Por eso no
235
                        // lo pongo en getJTable()
236
                        TableColumn typeColumn = jTableFields.getColumnModel().getColumn(1);
237
                        JComboBox comboBox = new JComboBox();
238
                        comboBox.addItem("Boolean");
239
                        comboBox.addItem("Date");
240
                        comboBox.addItem("Integer");
241
                        comboBox.addItem("Double");
242
                        comboBox.addItem("String");
243
                        typeColumn.setCellEditor(new DefaultCellEditor(comboBox));
244
        
245
                        TableColumn widthColumn = jTableFields.getColumnModel().getColumn(2);
246
                } catch (DriverLoadException e) {
247
                        // TODO Auto-generated catch block
248
                        e.printStackTrace();
249
                } catch (DriverException e) {
250
                        // TODO Auto-generated catch block
251
                        e.printStackTrace();
252
                }
253

    
254
        }
255

    
256
        /**
257
         * This method initializes this
258
         * 
259
         */
260
        private void initialize() {
261
        jLabel = new JLabel();
262
        jLabel.setBounds(new java.awt.Rectangle(14,9,361,34));
263
        jLabel.setText("Puede a?adir, borrar o renombrar los campos:");
264
        this.setLayout(null);
265
        this.setSize(new java.awt.Dimension(387,327));
266
        this.add(jLabel, null);
267
        this.add(getJScrollPane(), null);
268
        this.add(getJBtnNewField(), null);
269
        this.add(getJButton(), null);
270
        this.add(getJBtnRenameField(), null);
271
        this.add(getJBtnOK(), null);
272
        this.add(getJButton2(), null);
273
                        
274
        }
275

    
276
        public ViewInfo getViewInfo() {
277
                if (viewInfo == null)
278
                {
279
                        viewInfo = new ViewInfo(ViewInfo.MODALDIALOG | ViewInfo.PALETTE | ViewInfo.RESIZABLE);
280
                }
281
                return viewInfo;
282
        }
283
        
284
        /**
285
         * Convierte lo que hay en la tabla en una definici?n de campos
286
         * adecuada para crear un LayerDefinition
287
         * @return
288
         */
289
        public FieldDescription[] getFieldsDescription() {
290
                DefaultTableModel tm = (DefaultTableModel) jTableFields.getModel();
291
                FieldDescription[] fieldsDesc = new FieldDescription[tm.getRowCount()];
292

    
293
                for (int i=0; i < tm.getRowCount(); i++)
294
                {
295
                        fieldsDesc[i] = new FieldDescription();
296
                        fieldsDesc[i].setFieldName((String) tm.getValueAt(i,0));
297
                        String strType = (String) tm.getValueAt(i,1);
298
                        if (strType.equals("String"))
299
                                fieldsDesc[i].setFieldType(Types.VARCHAR);
300
                        if (strType.equals("Double"))
301
                                fieldsDesc[i].setFieldType(Types.DOUBLE);
302
                        if (strType.equals("Integer"))
303
                                fieldsDesc[i].setFieldType(Types.INTEGER);
304
                        if (strType.equals("Boolean"))
305
                                fieldsDesc[i].setFieldType(Types.BOOLEAN);
306
                        if (strType.equals("Date"))
307
                                fieldsDesc[i].setFieldType(Types.DATE);
308
                        int fieldLength = Integer.parseInt((String) tm.getValueAt(i,2));
309
                        fieldsDesc[i].setFieldLength(fieldLength);
310

    
311
                        // TODO: HACERLO BIEN
312
                        if (strType.equals("Double"))
313
                                fieldsDesc[i].setFieldDecimalCount(5);
314

    
315
                }
316

    
317
                return fieldsDesc;
318
        }
319

    
320

    
321
        /**
322
         * This method initializes jScrollPane        
323
         *         
324
         * @return javax.swing.JScrollPane        
325
         */
326
        private JScrollPane getJScrollPane() {
327
                if (jScrollPane == null) {
328
                        jScrollPane = new JScrollPane();
329
                        jScrollPane.setBounds(new java.awt.Rectangle(13,53,236,203));
330
                        jScrollPane.setViewportView(getJTableFields());
331
                }
332
                return jScrollPane;
333
        }
334

    
335
        /**
336
         * This method initializes jTableFields        
337
         *         
338
         * @return javax.swing.JTable        
339
         */
340
        private JTable getJTableFields() {
341
                if (jTableFields == null) {
342
                        jTableFields = new JTable();
343
                        jTableFields.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
344
                        
345
//                        Ask to be notified of selection changes.
346
                        ListSelectionModel rowSM = jTableFields.getSelectionModel();
347
                        rowSM.addListSelectionListener(new ListSelectionListener() {
348
                            public void valueChanged(ListSelectionEvent e) {
349
                                //Ignore extra messages.
350
                                if (e.getValueIsAdjusting()) return;
351

    
352
                                ListSelectionModel lsm =
353
                                    (ListSelectionModel)e.getSource();
354
                                if (lsm.isSelectionEmpty()) {
355
                                    //no rows are selected
356
                                        jBtnDeleteField.setEnabled(false);
357
                                } else {
358
                                        jBtnDeleteField.setEnabled(true);
359
                                }
360
                            }
361
                        });
362
                        
363
                }
364
                return jTableFields;
365
        }
366

    
367
        /**
368
         * This method initializes jBtnNewField        
369
         *         
370
         * @return javax.swing.JButton        
371
         */
372
        private JButton getJBtnNewField() {
373
                if (jBtnNewField == null) {
374
                        jBtnNewField = new JButton();
375
                        jBtnNewField.setBounds(new java.awt.Rectangle(255,60,118,25));
376
                        jBtnNewField.setText(PluginServices.getText(this, "New Field"));
377
                        jBtnNewField.addActionListener(new java.awt.event.ActionListener() {
378
                                public void actionPerformed(java.awt.event.ActionEvent e) {
379
                                        // Add a new row
380
                                        DefaultTableModel tm = (DefaultTableModel) jTableFields.getModel();
381
                                        Object[] newRow = new Object[tm.getColumnCount()];
382
                                        newRow[0] = PluginServices.getText(this,"field");
383
                                        newRow[1] = "String";
384
                                        newRow[2] = "20";
385
                                        tm.addRow(newRow);
386

    
387
                                        // Esto lo a?ado aqu? porque si no tiene registros, no hace caso. (Por eso no
388
                                        // lo pongo en getJTable()
389
                                        TableColumn typeColumn = jTableFields.getColumnModel().getColumn(1);
390
                                        JComboBox comboBox = new JComboBox();
391
                                        comboBox.addItem("Boolean");
392
                                        comboBox.addItem("Date");
393
                                        comboBox.addItem("Integer");
394
                                        comboBox.addItem("Double");
395
                                        comboBox.addItem("String");
396
                                        typeColumn.setCellEditor(new DefaultCellEditor(comboBox));
397

    
398
                                        TableColumn widthColumn = jTableFields.getColumnModel().getColumn(2);
399
                                        
400
                                }
401
                        });
402
                        jBtnNewField.addActionListener(new java.awt.event.ActionListener() {
403
                                public void actionPerformed(java.awt.event.ActionEvent e) {
404
                                        // Add a new row
405
                                        DefaultTableModel tm = (DefaultTableModel) jTableFields.getModel();
406
                                        Object[] newRow = new Object[tm.getColumnCount()];
407
                                        newRow[0] = PluginServices.getText(this,"field");
408
                                        newRow[1] = "String";
409
                                        newRow[2] = "20";
410
                                        tm.addRow(newRow);
411

    
412
                                        // Esto lo a?ado aqu? porque si no tiene registros, no hace caso. (Por eso no
413
                                        // lo pongo en getJTable()
414
                                        TableColumn typeColumn = jTableFields.getColumnModel().getColumn(1);
415
                                        JComboBox comboBox = new JComboBox();
416
                                        comboBox.addItem("Boolean");
417
                                        comboBox.addItem("Date");
418
                                        comboBox.addItem("Integer");
419
                                        comboBox.addItem("Double");
420
                                        comboBox.addItem("String");
421
                                        typeColumn.setCellEditor(new DefaultCellEditor(comboBox));
422

    
423
                                        TableColumn widthColumn = jTableFields.getColumnModel().getColumn(2);
424

    
425
//                                        edAdapter.addField(fld);
426
                                }
427
                        });
428
                }
429
                return jBtnNewField;
430
        }
431

    
432
        /**
433
         * This method initializes jButton        
434
         *         
435
         * @return javax.swing.JButton        
436
         */
437
        private JButton getJButton() {
438
                if (jBtnDeleteField == null) {
439
                        jBtnDeleteField = new JButton();
440
                        jBtnDeleteField.setBounds(new java.awt.Rectangle(255,90,118,25));
441
                        jBtnDeleteField.setText("Delete Field");
442
                        jBtnDeleteField.addActionListener(new java.awt.event.ActionListener() {
443
                                public void actionPerformed(java.awt.event.ActionEvent e) {
444
                                        int[] selecteds = jTableFields.getSelectedRows();
445
                                        DefaultTableModel tm = (DefaultTableModel) jTableFields.getModel();
446

    
447
                                        for (int i=selecteds.length-1; i >=0; i--)
448
                                                tm.removeRow(selecteds[i]);
449

    
450
                                }
451
                        });
452
                }
453
                return jBtnDeleteField;
454
        }
455

    
456
        /**
457
         * This method initializes jBtnRenameField        
458
         *         
459
         * @return javax.swing.JButton        
460
         */
461
        private JButton getJBtnRenameField() {
462
                if (jBtnRenameField == null) {
463
                        jBtnRenameField = new JButton();
464
                        jBtnRenameField.setBounds(new java.awt.Rectangle(255,120,118,25));
465
                        jBtnRenameField.setText("Rename Field...");
466
                }
467
                return jBtnRenameField;
468
        }
469

    
470
        /**
471
         * This method initializes jBtnOK        
472
         *         
473
         * @return javax.swing.JButton        
474
         */
475
        private JButton getJBtnOK() {
476
                if (jBtnOK == null) {
477
                        jBtnOK = new JButton();
478
                        jBtnOK.setBounds(new java.awt.Rectangle(180,285,91,23));
479
                        jBtnOK.setText("Aceptar");
480
                        jBtnOK.addActionListener(new java.awt.event.ActionListener() {
481
                                public void actionPerformed(java.awt.event.ActionEvent e) {
482
                                        System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
483
                                        
484
                                }
485
                        });
486
                }
487
                return jBtnOK;
488
        }
489

    
490
        /**
491
         * This method initializes jButton        
492
         *         
493
         * @return javax.swing.JButton        
494
         */
495
        private JButton getJButton2() {
496
                if (jBtnCancel == null) {
497
                        jBtnCancel = new JButton();
498
                        jBtnCancel.setBounds(new java.awt.Rectangle(285,285,91,23));
499
                        jBtnCancel.setText("Cancelar");
500
                }
501
                return jBtnCancel;
502
        }
503

    
504
}  //  @jve:decl-index=0:visual-constraint="12,23"
505

    
506