Statistics
| Revision:

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 / gui / SymbolTable.java @ 40558

History | View | Annotate | Download (15.9 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
/*
25
 * Created on 27-abr-2004
26
 *
27
 * To change the template for this generated file go to
28
 * Window>Preferences>Java>Code Generation>Code and Comments
29
 */
30
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
31
 *
32
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
33
 *
34
 * This program is free software; you can redistribute it and/or
35
 * modify it under the terms of the GNU General Public License
36
 * as published by the Free Software Foundation; either version 2
37
 * of the License, or (at your option) any later version.
38
 *
39
 * This program is distributed in the hope that it will be useful,
40
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42
 * GNU General Public License for more details.
43
 *
44
 * You should have received a copy of the GNU General Public License
45
 * along with this program; if not, write to the Free Software
46
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
47
 *
48
 * For more information, contact:
49
 *
50
 *  Generalitat Valenciana
51
 *   Conselleria d'Infraestructures i Transport
52
 *   Av. Blasco Ib??ez, 50
53
 *   46010 VALENCIA
54
 *   SPAIN
55
 *
56
 *      +34 963862235
57
 *   gvsig@gva.es
58
 *      www.gvsig.gva.es
59
 *
60
 *    or
61
 *
62
 *   IVER T.I. S.A
63
 *   Salamanca 50
64
 *   46005 Valencia
65
 *   Spain
66
 *
67
 *   +34 963163400
68
 *   dac@iver.es
69
 */
70
package org.gvsig.app.project.documents.view.legend.gui;
71

    
72
import java.awt.Component;
73
import java.awt.Dimension;
74
import java.awt.GridLayout;
75
import java.util.Hashtable;
76

    
77
import javax.swing.JPanel;
78
import javax.swing.JScrollPane;
79
import javax.swing.table.DefaultTableModel;
80
import javax.swing.table.TableCellEditor;
81
import javax.swing.table.TableColumn;
82

    
83
import org.gvsig.andami.PluginServices;
84
import org.gvsig.app.project.documents.gui.SymbolCellEditor;
85
import org.gvsig.app.project.documents.gui.TableSymbolCellRenderer;
86
import org.gvsig.app.project.documents.view.legend.edition.gui.IntervalCellEditor;
87
import org.gvsig.app.project.documents.view.legend.edition.gui.ValueCellEditor;
88
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
89
import org.gvsig.utils.swing.jtable.JTable;
90
import org.gvsig.utils.swing.jtable.TextFieldCellEditor;
91

    
92

    
93
/**
94
 * JPanel que contiene la tabla con los s?mbolos intervalos o valores y
95
 * etiquetado de estos valores.
96
 *
97
 * @author Vicente Caballero Navarro
98
 */
99
public class SymbolTable extends JPanel {
100
        private static final long serialVersionUID = -8694846716328735113L;
101
        private static Hashtable<String,TableCellEditor> cellEditors = new Hashtable<String,TableCellEditor>();
102

    
103
        public static final String VALUES_TYPE = "values";
104
        public static final String INTERVALS_TYPE = "intervals";
105
        private JTable table;
106
        private String type;
107
        private int shapeType;
108

    
109
        /**
110
         * Crea un nuevo FSymbolTable.
111
         *
112
         * @param type
113
         *            tipo de valor si es intervalo: "intervals" y si es por
114
         *            valores: "values".
115
         */
116
        public SymbolTable(Component ownerComponent, String type, int shapeType) {
117
                super(new GridLayout(1, 0));
118
                this.type = type;
119
                this.shapeType = shapeType;
120

    
121
                table = new JTable();
122
                table.setModel(new MyTableModel());
123
                table.setPreferredScrollableViewportSize(new Dimension(480, 110));
124

    
125
                initializeCellEditors();
126

    
127
                // Create the scroll pane and add the table to it.
128
                JScrollPane scrollPane = new JScrollPane(table);
129

    
130
                // Set up column sizes.
131
                // initColumnSizes(table);
132
                setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
133

    
134
                if(cellEditors.get(type) == null)
135
                        throw new Error("Symbol table type not set!");
136

    
137
                setUpValueColumn(table, table.getColumnModel().getColumn(1),cellEditors.get(this.type));
138
                setUpLabelColumn(table, table.getColumnModel().getColumn(2));
139

    
140
                // Add the scroll pane to this panel.
141
                add(scrollPane);
142
                table.setRowSelectionAllowed(true);
143
        }
144
        /**
145
         * Inicializa los valores de los CellEditors que la SymbolTable poseer? por defecto
146
         */
147
        private void initializeCellEditors() {
148
                this.cellEditors.put(this.INTERVALS_TYPE,new IntervalCellEditor());
149
                this.cellEditors.put(this.VALUES_TYPE, new ValueCellEditor());
150
        }
151
        /**
152
         * A?ade un nuevo CellEditor a la lista de disponibles
153
         *
154
         * @param key String con el nombre identificativo del CellEditor
155
         * @param cellEditor CellEditor que va a ser a?adido
156
         */
157
        public static void addNewCellEditor(String key,TableCellEditor cellEditor ) {
158
                cellEditors.put(key, cellEditor);
159
        }
160
        /**
161
         * Obtiene el valor de los elementos de una fila seleccionada
162
         *
163
         * @return Object[] Array con los objetos de cada una de las columnas de la fila seleccionada
164
         */
165
        public Object[] getSelectedRowElements() {
166
                Object[] values = new Object[3];
167

    
168
                MyTableModel m = (MyTableModel) table.getModel();
169
                int[] selectedRows = table.getSelectedRows();
170

    
171
                if(selectedRows.length != 1)
172
                        return null;
173

    
174
                for (int i = 0; i < 3; i++) {
175
                        values[i] = m.getValueAt(selectedRows[0], i);
176
                }
177

    
178
                return values;
179
        }
180
        /**
181
         * A?ade una fila al modelo.
182
         *
183
         * @param vector
184
         *            Fila en forma de vector de Object para a?adir al modelo.
185
         */
186
        public void addRow(Object[] vector) {
187
                MyTableModel m = (MyTableModel) table.getModel();
188
                m.addRow(vector);
189

    
190
        }
191

    
192
        /**
193
         * Elimina la fila que tiene como clave el objeto que se pasa como
194
         * par?metro.
195
         *
196
         * @param obj
197
         *            clave del objeto a eliminar.
198
         */
199
        public void removeRow(Object obj) {
200
                MyTableModel m = (MyTableModel) table.getModel();
201

    
202
                for (int i = 0; i < m.getRowCount(); i++) {
203
//                        if (m.getValueAt(i, 1) instanceof NullUniqueValue
204
//                                        || m.getValueAt(i, 1) instanceof NullIntervalValue) {
205
//                                m.removeRow(i);
206
//                        }
207
                        if (m.getValueAt(i, 1) == null) {
208
                                m.removeRow(i);
209
                        }
210
                }
211
        }
212

    
213
        /**
214
         * Elimina las filas que est?n seleccionadas.
215
         */
216
        public void removeSelectedRows() {
217
                if (table.getCellEditor() != null) {
218
                        table.getCellEditor().cancelCellEditing();
219
                }
220

    
221
                MyTableModel m = (MyTableModel) table.getModel();
222
                int[] selectedRows = table.getSelectedRows();
223

    
224
                for (int i = selectedRows.length - 1; i >= 0; i--) {
225
                        m.removeRow(selectedRows[i]);
226
                }
227
        }
228

    
229
        /**
230
         * Rellena la tabla con los s?mbolos valores y descripciones que se pasan
231
         * como par?metro.
232
         *
233
         * @param symbols
234
         *            Array de s?mbolos
235
         * @param values
236
         *            Array de valores.
237
         * @param descriptions
238
         *            Array de descripciones.
239
         */
240
        public void fillTableFromSymbolList(ISymbol[] symbols, Object[] values,
241
                        String[] descriptions) {
242
                ISymbol theSymbol;
243

    
244
                for (int i = 0; i < symbols.length; i++) {
245
                        theSymbol = symbols[i];
246
//                        if(!(values[i] instanceof NullIntervalValue) && !(values[i] instanceof NullUniqueValue))
247
//                                addTableRecord(theSymbol, values[i], descriptions[i]);
248
                        if(!(values[i] == null))
249
                                addTableRecord(theSymbol, values[i], descriptions[i]);
250
                }
251
        }
252

    
253
        /**
254
         * A?ade una fila con los objetos que se pasan como par?metros.
255
         *
256
         * @param symbol
257
         *            s?mbolo de la fila.
258
         * @param value
259
         *            Valor de la fila.
260
         * @param description
261
         *            Descripci?n.
262
         */
263
        public void addTableRecord(ISymbol symbol, Object value, String description) {
264
                Object[] theRow = new Object[3];
265
                theRow[0] = symbol;
266
                theRow[1] = value;
267
                theRow[2] = description;
268
                addRow(theRow);
269
        }
270

    
271
        /**
272
         * Devuelve el valor a partie del n?mero de fila y columna.
273
         *
274
         * @param row
275
         *            n?mero de fila.
276
         * @param col
277
         *            n?mero de columna.
278
         *
279
         * @return Objeto.
280
         */
281
        public Object getFieldValue(int row, int col) {
282
                MyTableModel m = (MyTableModel) table.getModel();
283

    
284
                return m.getValueAt(row, col);
285
        }
286

    
287
        /**
288
         * Devuelve el n?mero total de filas que contiene el modelo.
289
         *
290
         * @return N?mero de filas.
291
         */
292
        public int getRowCount() {
293
                MyTableModel m = (MyTableModel) table.getModel();
294

    
295
                return m.getRowCount();
296
        }
297

    
298
        /**
299
         * Elimina todas las filas del modelo.
300
         */
301
        public void removeAllItems() {
302
                table.setModel(new MyTableModel());
303
                setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
304
                setUpValueColumn(table, table.getColumnModel().getColumn(1),cellEditors.get(this.type));
305
                setUpLabelColumn(table, table.getColumnModel().getColumn(2));
306
        }
307

    
308
        /**
309
         * Inicializa el cell editor de tipo descripci?n de la columna que se pasa
310
         * como par?metro.
311
         *
312
         * @param table2
313
         *            Tabla.
314
         * @param column
315
         *            Columna.
316
         */
317
        public void setUpLabelColumn(JTable table2, TableColumn column) {
318
                TextFieldCellEditor labeleditor = new TextFieldCellEditor();
319
                column.setCellEditor(labeleditor);
320
        }
321

    
322
        /**
323
         * Inicializa el cell editor de tipo valor de la columna que se pasa como
324
         * par?metro.
325
         *
326
         * @param table2
327
         *            Tabla.
328
         * @param column
329
         *            Columna.
330
         * @param tableCellEditor
331
         */
332
        public void setUpValueColumn(JTable table2,TableColumn column, TableCellEditor tableCellEditor) {
333
                column.setCellEditor(tableCellEditor);
334
        }
335
        /**
336
         * Inicializa el cell editor de tipo s?mbolo de la columna que se pasa como
337
         * par?metro.
338
         *
339
         * @param table2
340
         *            Tabla.
341
         * @param column
342
         *            Columna.
343
         */
344
        public void setUpSymbolColumn(JTable table2, TableColumn column) {
345
                // Set up the editor
346
                column.setMaxWidth(100);
347
                column.setWidth(60);
348
                column.setPreferredWidth(60);
349
                column.setMinWidth(50);
350

    
351
                // FSymbolCellEditor symboleditor = new FSymbolCellEditor();
352
                SymbolCellEditor symboleditor = new SymbolCellEditor(shapeType);
353
                column.setCellEditor(symboleditor);
354

    
355
                TableSymbolCellRenderer renderer = new TableSymbolCellRenderer(true);
356
                column.setCellRenderer(renderer);
357
        }
358

    
359
        /**
360
         * Modelo que propio que se aplica a la tabla.
361
         *
362
         * @author Vicente Caballero Navarro
363
         */
364
        class MyTableModel extends DefaultTableModel {
365
                private static final long serialVersionUID = 1L;
366

    
367
                // AbstractTableModel {
368
                private String[] columnNames = {
369
                                PluginServices.getText(this, "Simbolo"),
370
                                PluginServices.getText(this, "Valor"),
371
                                PluginServices.getText(this, "Etiqueta") };
372

    
373
                /**
374
                 * Devuelve el n?mero de columnas.
375
                 *
376
                 * @return N?mero de columnas.
377
                 */
378
                public int getColumnCount() {
379
                        return columnNames.length;
380
                }
381

    
382
                /**
383
                 * Devuelve el String del valor de la columna.
384
                 *
385
                 * @param col
386
                 *            N?mero de columna.
387
                 *
388
                 * @return Nombre de la columna.
389
                 */
390
                public String getColumnName(int col) {
391
                        return columnNames[col];
392
                }
393

    
394
                public Class getColumnClass(int c) {
395
            switch (c) {
396
            case 0:
397
                return ISymbol.class;
398
            case 1:
399
                // TODO: take the value from the related FeatureType attribute
400
                Object value = getValueAt(0, c);
401
                return value == null ? Object.class : value.getClass();
402
            case 2:
403
                return String.class;
404
            default:
405
                return Object.class;
406
            }
407
                }
408

    
409
                /*
410
                 * Don't need to implement this method unless your table's editable.
411
                 */
412
                public boolean isCellEditable(int row, int col) {
413
                        // Note that the data/cell address is constant,
414
                        // no matter where the cell appears onscreen.
415
                        // if (col > 0) {
416
                        return true;
417
                }
418

    
419
                @Override
420
                public Object getValueAt(int row, int column) {
421
                        if(column == 2)
422
                                return ((ISymbol)getValueAt(row,0)).getDescription();
423

    
424
                        return super.getValueAt(row, column);
425
                }
426

    
427
                @Override
428
                public void setValueAt(Object aValue, int row, int column) {
429

    
430
                        if(column == 2){
431
                                ISymbol symbol = (ISymbol) getValueAt(row,0);
432
                                symbol.setDescription((String) aValue);
433
                                setValueAt(symbol,row,0);
434
                        }
435

    
436
                        super.setValueAt(aValue, row, column);
437
                }
438

    
439
        }
440
        
441
        
442
        
443
    public void moveDownRows(int startPos, int endPos, int numOfElements) {
444
        if(startPos > endPos)
445
            return;
446
        if(startPos >= getRowCount()-1 )
447
            return;
448
        if(startPos == getRowCount()-1)
449
            return;
450

    
451
        Object[][] values = new Object[getRowCount()][3];
452
        for (int i = 0; i < getRowCount(); i++) {
453
            values[i][0] = table.getModel().getValueAt(i,0);
454
            values[i][1] = table.getModel().getValueAt(i,1);
455
            values[i][2] = table.getModel().getValueAt(i,2);
456
        }
457

    
458
        Object[][]aux = new Object[numOfElements][3];
459
        for (int i = 0; i < numOfElements; i++) {
460

    
461
            aux[numOfElements - i - 1][0] = values[startPos - i][0];
462
            aux[numOfElements - i - 1][1] = values[startPos - i][1];
463
            aux[numOfElements - i - 1][2] = values[startPos - i][2];
464
        }
465

    
466
        Object [][] targetVal = {{values[endPos][0],values[endPos][1],values[endPos][2]}};
467

    
468
        values[startPos - numOfElements + 1][0] = targetVal[0][0];
469
        values[startPos - numOfElements + 1][1] = targetVal[0][1];
470
        values[startPos - numOfElements + 1][2] = targetVal[0][2];
471

    
472
        for (int i = 0; i < numOfElements; i++) {
473
            values[endPos - i][0] = aux[numOfElements - i - 1][0];
474
            values[endPos - i][1] = aux[numOfElements - i - 1][1];
475
            values[endPos - i][2] = aux[numOfElements - i - 1][2];
476
        }
477

    
478
        ISymbol[] symbols = new ISymbol[getRowCount()];
479
        Object[] objects = new Object[getRowCount()];
480
        String[] cads = new String[getRowCount()];
481

    
482
        for (int i = 0; i < getRowCount(); i++) {
483
            symbols[i] = (ISymbol) values[i][0];
484
            objects[i] = values[i][1];
485
            cads[i] = (String) values[i][2];
486
        }
487

    
488
        removeAllItems();
489
        fillTableFromSymbolList(symbols,objects,cads);
490
        table.addRowSelectionInterval(endPos-numOfElements+1,endPos);
491
    }
492

    
493

    
494
    public void moveUpRows(int startPos, int endPos, int numOfElements) {
495

    
496
        if(startPos == 0)
497
            return;
498
        if(endPos > startPos)
499
            return;
500

    
501

    
502
        Object[][] values = new Object[getRowCount()][3];
503
        for (int i = 0; i < getRowCount(); i++) {
504
            values[i][0] = table.getModel().getValueAt(i,0);
505
            values[i][1] = table.getModel().getValueAt(i,1);
506
            values[i][2] = table.getModel().getValueAt(i,2);
507
        }
508

    
509
        Object[][]aux = new Object[numOfElements][3];
510
        for (int i = 0; i < numOfElements; i++) {
511

    
512
            aux[i][0] = values[startPos + i][0];
513
            aux[i][1] = values[startPos + i][1];
514
            aux[i][2] = values[startPos + i][2];
515
        }
516

    
517
        Object [][] targetVal = {{values[endPos][0],values[endPos][1],values[endPos][2]}};
518

    
519
        values[startPos + numOfElements - 1][0] = targetVal[0][0];
520
        values[startPos + numOfElements - 1][1] = targetVal[0][1];
521
        values[startPos + numOfElements - 1][2] = targetVal[0][2];
522

    
523
        for (int i = 0; i < numOfElements; i++) {
524

    
525
            values[endPos + i][0] = aux[i][0];
526
            values[endPos + i][1] = aux[i][1];
527
            values[endPos + i][2] = aux[i][2];
528
        }
529

    
530
        ISymbol[] symbols = new ISymbol[getRowCount()];
531
        Object[] objects = new Object[getRowCount()];
532
        String[] cads = new String[getRowCount()];
533

    
534
        for (int i = 0; i < getRowCount(); i++) {
535
            symbols[i] = (ISymbol) values[i][0];
536
            objects[i] = values[i][1];
537
            cads[i] = (String) values[i][2];
538
        }
539

    
540
        removeAllItems();
541
        fillTableFromSymbolList(symbols,objects,cads);
542
        table.addRowSelectionInterval(endPos,endPos+numOfElements-1);
543
    }
544
    
545
    
546
    public int[] getSelectedRows(){
547
        return table.getSelectedRows();
548
    }
549

    
550
}