Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / dialogs / DlgFieldManager.java @ 6212

History | View | Annotate | Download (12.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.iver.andami.PluginServices;
62
import com.iver.andami.ui.mdiManager.View;
63
import com.iver.andami.ui.mdiManager.ViewInfo;
64
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
65
import com.iver.cit.gvsig.fmap.edition.IFieldManager;
66

    
67
public class DlgFieldManager extends JPanel implements View {
68

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

    
117
                public int getColumnCount() {
118
                        return 3;
119
                }
120

    
121
                public int getRowCount() {
122
                        return myFields.size();
123
                }
124

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

    
145
                                return strType;
146
                        case 2:
147
                                return new Integer(aux.getFieldDescription().getFieldLength());
148
                        }
149
                        return null;
150
                }
151

    
152
                public Class getColumnClass(int columnIndex) {
153
                        // TODO Auto-generated method stub
154
                        return super.getColumnClass(columnIndex);
155
                }
156

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

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

    
182
                }
183

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

    
209
                                // TODO: HACERLO BIEN
210
                                if (fDesc.getFieldType() == Types.VARCHAR)
211
                                        fDesc.setFieldDecimalCount(5);
212

    
213
                        }
214
                }
215
                
216
        }
217
        /**
218
         * This method initializes 
219
         * 
220
         */
221
        public DlgFieldManager(IFieldManager fieldManager) {
222
                super();
223
                this.fieldManager = fieldManager;
224
                initialize();
225
                // Add a new row
226
                TableModel tm = new MyTableModel(fieldManager.getOriginalFields());
227
                getJTableFields().setModel(tm);
228
                // Esto lo a?ado aqu? porque si no tiene registros, no hace caso. (Por eso no
229
                // lo pongo en getJTable()
230
                TableColumn typeColumn = jTableFields.getColumnModel().getColumn(1);
231
                JComboBox comboBox = new JComboBox();
232
                comboBox.addItem("Boolean");
233
                comboBox.addItem("Date");
234
                comboBox.addItem("Integer");
235
                comboBox.addItem("Double");
236
                comboBox.addItem("String");
237
                typeColumn.setCellEditor(new DefaultCellEditor(comboBox));
238

    
239
                TableColumn widthColumn = jTableFields.getColumnModel().getColumn(2);
240

    
241
        }
242

    
243
        /**
244
         * This method initializes this
245
         * 
246
         */
247
        private void initialize() {
248
        jLabel = new JLabel();
249
        jLabel.setBounds(new java.awt.Rectangle(14,9,361,34));
250
        jLabel.setText("Puede a?adir, borrar o renombrar los campos:");
251
        this.setLayout(null);
252
        this.setSize(new java.awt.Dimension(387,327));
253
        this.add(jLabel, null);
254
        this.add(getJScrollPane(), null);
255
        this.add(getJBtnNewField(), null);
256
        this.add(getJButton(), null);
257
        this.add(getJBtnRenameField(), null);
258
        this.add(getJBtnOK(), null);
259
        this.add(getJButton2(), null);
260
                        
261
        }
262

    
263
        public ViewInfo getViewInfo() {
264
                if (viewInfo == null)
265
                {
266
                        viewInfo = new ViewInfo(ViewInfo.MODALDIALOG | ViewInfo.PALETTE | ViewInfo.RESIZABLE);
267
                }
268
                return viewInfo;
269
        }
270
        
271
        /**
272
         * Convierte lo que hay en la tabla en una definici?n de campos
273
         * adecuada para crear un LayerDefinition
274
         * @return
275
         */
276
        public FieldDescription[] getFieldsDescription() {
277
                DefaultTableModel tm = (DefaultTableModel) jTableFields.getModel();
278
                FieldDescription[] fieldsDesc = new FieldDescription[tm.getRowCount()];
279

    
280
                for (int i=0; i < tm.getRowCount(); i++)
281
                {
282
                        fieldsDesc[i] = new FieldDescription();
283
                        fieldsDesc[i].setFieldName((String) tm.getValueAt(i,0));
284
                        String strType = (String) tm.getValueAt(i,1);
285
                        if (strType.equals("String"))
286
                                fieldsDesc[i].setFieldType(Types.VARCHAR);
287
                        if (strType.equals("Double"))
288
                                fieldsDesc[i].setFieldType(Types.DOUBLE);
289
                        if (strType.equals("Integer"))
290
                                fieldsDesc[i].setFieldType(Types.INTEGER);
291
                        if (strType.equals("Boolean"))
292
                                fieldsDesc[i].setFieldType(Types.BOOLEAN);
293
                        if (strType.equals("Date"))
294
                                fieldsDesc[i].setFieldType(Types.DATE);
295
                        int fieldLength = Integer.parseInt((String) tm.getValueAt(i,2));
296
                        fieldsDesc[i].setFieldLength(fieldLength);
297

    
298
                        // TODO: HACERLO BIEN
299
                        if (strType.equals("Double"))
300
                                fieldsDesc[i].setFieldDecimalCount(5);
301

    
302
                }
303

    
304
                return fieldsDesc;
305
        }
306

    
307

    
308
        /**
309
         * This method initializes jScrollPane        
310
         *         
311
         * @return javax.swing.JScrollPane        
312
         */
313
        private JScrollPane getJScrollPane() {
314
                if (jScrollPane == null) {
315
                        jScrollPane = new JScrollPane();
316
                        jScrollPane.setBounds(new java.awt.Rectangle(13,53,236,203));
317
                        jScrollPane.setViewportView(getJTableFields());
318
                }
319
                return jScrollPane;
320
        }
321

    
322
        /**
323
         * This method initializes jTableFields        
324
         *         
325
         * @return javax.swing.JTable        
326
         */
327
        private JTable getJTableFields() {
328
                if (jTableFields == null) {
329
                        jTableFields = new JTable();
330
                        jTableFields.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
331
                        
332
//                        Ask to be notified of selection changes.
333
                        ListSelectionModel rowSM = jTableFields.getSelectionModel();
334
                        rowSM.addListSelectionListener(new ListSelectionListener() {
335
                            public void valueChanged(ListSelectionEvent e) {
336
                                //Ignore extra messages.
337
                                if (e.getValueIsAdjusting()) return;
338

    
339
                                ListSelectionModel lsm =
340
                                    (ListSelectionModel)e.getSource();
341
                                if (lsm.isSelectionEmpty()) {
342
                                    //no rows are selected
343
                                        jBtnDeleteField.setEnabled(false);
344
                                } else {
345
                                        jBtnDeleteField.setEnabled(true);
346
                                }
347
                            }
348
                        });
349
                        
350
                }
351
                return jTableFields;
352
        }
353

    
354
        /**
355
         * This method initializes jBtnNewField        
356
         *         
357
         * @return javax.swing.JButton        
358
         */
359
        private JButton getJBtnNewField() {
360
                if (jBtnNewField == null) {
361
                        jBtnNewField = new JButton();
362
                        jBtnNewField.setBounds(new java.awt.Rectangle(255,60,118,25));
363
                        jBtnNewField.setText(PluginServices.getText(this, "New Field"));
364
                        jBtnNewField.addActionListener(new java.awt.event.ActionListener() {
365
                                public void actionPerformed(java.awt.event.ActionEvent e) {
366
                                        // Add a new row
367
                                        DefaultTableModel tm = (DefaultTableModel) jTableFields.getModel();
368
                                        Object[] newRow = new Object[tm.getColumnCount()];
369
                                        newRow[0] = PluginServices.getText(this,"field");
370
                                        newRow[1] = "String";
371
                                        newRow[2] = "20";
372
                                        tm.addRow(newRow);
373

    
374
                                        // Esto lo a?ado aqu? porque si no tiene registros, no hace caso. (Por eso no
375
                                        // lo pongo en getJTable()
376
                                        TableColumn typeColumn = jTableFields.getColumnModel().getColumn(1);
377
                                        JComboBox comboBox = new JComboBox();
378
                                        comboBox.addItem("Boolean");
379
                                        comboBox.addItem("Date");
380
                                        comboBox.addItem("Integer");
381
                                        comboBox.addItem("Double");
382
                                        comboBox.addItem("String");
383
                                        typeColumn.setCellEditor(new DefaultCellEditor(comboBox));
384

    
385
                                        TableColumn widthColumn = jTableFields.getColumnModel().getColumn(2);
386
                                        
387
                                }
388
                        });
389
                }
390
                return jBtnNewField;
391
        }
392

    
393
        /**
394
         * This method initializes jButton        
395
         *         
396
         * @return javax.swing.JButton        
397
         */
398
        private JButton getJButton() {
399
                if (jBtnDeleteField == null) {
400
                        jBtnDeleteField = new JButton();
401
                        jBtnDeleteField.setBounds(new java.awt.Rectangle(255,90,118,25));
402
                        jBtnDeleteField.setText("Delete Field");
403
                }
404
                return jBtnDeleteField;
405
        }
406

    
407
        /**
408
         * This method initializes jBtnRenameField        
409
         *         
410
         * @return javax.swing.JButton        
411
         */
412
        private JButton getJBtnRenameField() {
413
                if (jBtnRenameField == null) {
414
                        jBtnRenameField = new JButton();
415
                        jBtnRenameField.setBounds(new java.awt.Rectangle(255,120,118,25));
416
                        jBtnRenameField.setText("Rename Field...");
417
                }
418
                return jBtnRenameField;
419
        }
420

    
421
        /**
422
         * This method initializes jBtnOK        
423
         *         
424
         * @return javax.swing.JButton        
425
         */
426
        private JButton getJBtnOK() {
427
                if (jBtnOK == null) {
428
                        jBtnOK = new JButton();
429
                        jBtnOK.setBounds(new java.awt.Rectangle(180,285,91,23));
430
                        jBtnOK.setText("Aceptar");
431
                }
432
                return jBtnOK;
433
        }
434

    
435
        /**
436
         * This method initializes jButton        
437
         *         
438
         * @return javax.swing.JButton        
439
         */
440
        private JButton getJButton2() {
441
                if (jBtnCancel == null) {
442
                        jBtnCancel = new JButton();
443
                        jBtnCancel.setBounds(new java.awt.Rectangle(285,285,91,23));
444
                        jBtnCancel.setText("Cancelar");
445
                }
446
                return jBtnCancel;
447
        }
448

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

    
451