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 @ 41190

History | View | Annotate | Download (14.6 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
package org.gvsig.app.project.documents.view.legend.gui;
26

    
27
import java.awt.Component;
28
import java.awt.Dimension;
29
import java.awt.GridLayout;
30
import java.util.Hashtable;
31

    
32
import javax.swing.JPanel;
33
import javax.swing.JScrollPane;
34
import javax.swing.table.DefaultTableModel;
35
import javax.swing.table.TableCellEditor;
36
import javax.swing.table.TableColumn;
37

    
38
import org.gvsig.andami.PluginServices;
39
import org.gvsig.app.project.documents.gui.SymbolCellEditor;
40
import org.gvsig.app.project.documents.gui.TableSymbolCellRenderer;
41
import org.gvsig.app.project.documents.view.legend.edition.gui.IntervalCellEditor;
42
import org.gvsig.app.project.documents.view.legend.edition.gui.ValueCellEditor;
43
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
44
import org.gvsig.utils.swing.jtable.JTable;
45
import org.gvsig.utils.swing.jtable.TextFieldCellEditor;
46

    
47

    
48
/**
49
 * JPanel que contiene la tabla con los s?mbolos intervalos o valores y
50
 * etiquetado de estos valores.
51
 *
52
 * @author Vicente Caballero Navarro
53
 */
54
public class SymbolTable extends JPanel {
55
        private static final long serialVersionUID = -8694846716328735113L;
56
        private static Hashtable<String,TableCellEditor> cellEditors = new Hashtable<String,TableCellEditor>();
57

    
58
        public static final String VALUES_TYPE = "values";
59
        public static final String INTERVALS_TYPE = "intervals";
60
        private JTable table;
61
        private String type;
62
        private int shapeType;
63

    
64
        /**
65
         * Crea un nuevo FSymbolTable.
66
         *
67
         * @param type
68
         *            tipo de valor si es intervalo: "intervals" y si es por
69
         *            valores: "values".
70
         */
71
        public SymbolTable(Component ownerComponent, String type, int shapeType) {
72
                super(new GridLayout(1, 0));
73
                this.type = type;
74
                this.shapeType = shapeType;
75

    
76
                table = new JTable();
77
                table.setModel(new MyTableModel());
78
                table.setPreferredScrollableViewportSize(new Dimension(480, 110));
79

    
80
                initializeCellEditors();
81

    
82
                // Create the scroll pane and add the table to it.
83
                JScrollPane scrollPane = new JScrollPane(table);
84

    
85
                // Set up column sizes.
86
                // initColumnSizes(table);
87
                setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
88

    
89
                if(cellEditors.get(type) == null)
90
                        throw new Error("Symbol table type not set!");
91

    
92
                setUpValueColumn(table, table.getColumnModel().getColumn(1),cellEditors.get(this.type));
93
                setUpLabelColumn(table, table.getColumnModel().getColumn(2));
94

    
95
                // Add the scroll pane to this panel.
96
                add(scrollPane);
97
                table.setRowSelectionAllowed(true);
98
                // default is 16
99
                table.setRowHeight(24);
100
        }
101
        /**
102
         * Inicializa los valores de los CellEditors que la SymbolTable poseer? por defecto
103
         */
104
        private void initializeCellEditors() {
105
                this.cellEditors.put(this.INTERVALS_TYPE,new IntervalCellEditor());
106
                this.cellEditors.put(this.VALUES_TYPE, new ValueCellEditor());
107
        }
108
        /**
109
         * A?ade un nuevo CellEditor a la lista de disponibles
110
         *
111
         * @param key String con el nombre identificativo del CellEditor
112
         * @param cellEditor CellEditor que va a ser a?adido
113
         */
114
        public static void addNewCellEditor(String key,TableCellEditor cellEditor ) {
115
                cellEditors.put(key, cellEditor);
116
        }
117
        /**
118
         * Obtiene el valor de los elementos de una fila seleccionada
119
         *
120
         * @return Object[] Array con los objetos de cada una de las columnas de la fila seleccionada
121
         */
122
        public Object[] getSelectedRowElements() {
123
                Object[] values = new Object[3];
124

    
125
                MyTableModel m = (MyTableModel) table.getModel();
126
                int[] selectedRows = table.getSelectedRows();
127

    
128
                if(selectedRows.length != 1)
129
                        return null;
130

    
131
                for (int i = 0; i < 3; i++) {
132
                        values[i] = m.getValueAt(selectedRows[0], i);
133
                }
134

    
135
                return values;
136
        }
137
        /**
138
         * A?ade una fila al modelo.
139
         *
140
         * @param vector
141
         *            Fila en forma de vector de Object para a?adir al modelo.
142
         */
143
        public void addRow(Object[] vector) {
144
                MyTableModel m = (MyTableModel) table.getModel();
145
                m.addRow(vector);
146

    
147
        }
148

    
149
        /**
150
         * Elimina la fila que tiene como clave el objeto que se pasa como
151
         * par?metro.
152
         *
153
         * @param obj
154
         *            clave del objeto a eliminar.
155
         */
156
        public void removeRow(Object obj) {
157
                MyTableModel m = (MyTableModel) table.getModel();
158

    
159
                for (int i = 0; i < m.getRowCount(); i++) {
160
//                        if (m.getValueAt(i, 1) instanceof NullUniqueValue
161
//                                        || m.getValueAt(i, 1) instanceof NullIntervalValue) {
162
//                                m.removeRow(i);
163
//                        }
164
                        if (m.getValueAt(i, 1) == null) {
165
                                m.removeRow(i);
166
                        }
167
                }
168
        }
169

    
170
        /**
171
         * Elimina las filas que est?n seleccionadas.
172
         */
173
        public void removeSelectedRows() {
174
                if (table.getCellEditor() != null) {
175
                        table.getCellEditor().cancelCellEditing();
176
                }
177

    
178
                MyTableModel m = (MyTableModel) table.getModel();
179
                int[] selectedRows = table.getSelectedRows();
180

    
181
                for (int i = selectedRows.length - 1; i >= 0; i--) {
182
                        m.removeRow(selectedRows[i]);
183
                }
184
        }
185

    
186
        /**
187
         * Rellena la tabla con los s?mbolos valores y descripciones que se pasan
188
         * como par?metro.
189
         *
190
         * @param symbols
191
         *            Array de s?mbolos
192
         * @param values
193
         *            Array de valores.
194
         * @param descriptions
195
         *            Array de descripciones.
196
         */
197
        public void fillTableFromSymbolList(ISymbol[] symbols, Object[] values,
198
                        String[] descriptions) {
199
                ISymbol theSymbol;
200

    
201
                for (int i = 0; i < symbols.length; i++) {
202
                        theSymbol = symbols[i];
203
//                        if(!(values[i] instanceof NullIntervalValue) && !(values[i] instanceof NullUniqueValue))
204
//                                addTableRecord(theSymbol, values[i], descriptions[i]);
205
                        if(!(values[i] == null))
206
                                addTableRecord(theSymbol, values[i], descriptions[i]);
207
                }
208
        }
209

    
210
        /**
211
         * A?ade una fila con los objetos que se pasan como par?metros.
212
         *
213
         * @param symbol
214
         *            s?mbolo de la fila.
215
         * @param value
216
         *            Valor de la fila.
217
         * @param description
218
         *            Descripci?n.
219
         */
220
        public void addTableRecord(ISymbol symbol, Object value, String description) {
221
                Object[] theRow = new Object[3];
222
                theRow[0] = symbol;
223
                theRow[1] = value;
224
                theRow[2] = description;
225
                addRow(theRow);
226
        }
227

    
228
        /**
229
         * Devuelve el valor a partie del n?mero de fila y columna.
230
         *
231
         * @param row
232
         *            n?mero de fila.
233
         * @param col
234
         *            n?mero de columna.
235
         *
236
         * @return Objeto.
237
         */
238
        public Object getFieldValue(int row, int col) {
239
                MyTableModel m = (MyTableModel) table.getModel();
240

    
241
                return m.getValueAt(row, col);
242
        }
243

    
244
        /**
245
         * Devuelve el n?mero total de filas que contiene el modelo.
246
         *
247
         * @return N?mero de filas.
248
         */
249
        public int getRowCount() {
250
                MyTableModel m = (MyTableModel) table.getModel();
251

    
252
                return m.getRowCount();
253
        }
254

    
255
        /**
256
         * Elimina todas las filas del modelo.
257
         */
258
        public void removeAllItems() {
259
                table.setModel(new MyTableModel());
260
                setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
261
                setUpValueColumn(table, table.getColumnModel().getColumn(1),cellEditors.get(this.type));
262
                setUpLabelColumn(table, table.getColumnModel().getColumn(2));
263
        }
264

    
265
        /**
266
         * Inicializa el cell editor de tipo descripci?n de la columna que se pasa
267
         * como par?metro.
268
         *
269
         * @param table2
270
         *            Tabla.
271
         * @param column
272
         *            Columna.
273
         */
274
        public void setUpLabelColumn(JTable table2, TableColumn column) {
275
                TextFieldCellEditor labeleditor = new TextFieldCellEditor();
276
                column.setCellEditor(labeleditor);
277
        }
278

    
279
        /**
280
         * Inicializa el cell editor de tipo valor de la columna que se pasa como
281
         * par?metro.
282
         *
283
         * @param table2
284
         *            Tabla.
285
         * @param column
286
         *            Columna.
287
         * @param tableCellEditor
288
         */
289
        public void setUpValueColumn(JTable table2,TableColumn column, TableCellEditor tableCellEditor) {
290
                column.setCellEditor(tableCellEditor);
291
        }
292
        /**
293
         * Inicializa el cell editor de tipo s?mbolo de la columna que se pasa como
294
         * par?metro.
295
         *
296
         * @param table2
297
         *            Tabla.
298
         * @param column
299
         *            Columna.
300
         */
301
        public void setUpSymbolColumn(JTable table2, TableColumn column) {
302
                // Set up the editor
303
                column.setMaxWidth(100);
304
                column.setWidth(60);
305
                column.setPreferredWidth(60);
306
                column.setMinWidth(50);
307

    
308
                // FSymbolCellEditor symboleditor = new FSymbolCellEditor();
309
                SymbolCellEditor symboleditor = new SymbolCellEditor(shapeType);
310
                column.setCellEditor(symboleditor);
311

    
312
                TableSymbolCellRenderer renderer = new TableSymbolCellRenderer(true);
313
                column.setCellRenderer(renderer);
314
        }
315

    
316
        /**
317
         * Modelo que propio que se aplica a la tabla.
318
         *
319
         * @author Vicente Caballero Navarro
320
         */
321
        class MyTableModel extends DefaultTableModel {
322
                private static final long serialVersionUID = 1L;
323

    
324
                // AbstractTableModel {
325
                private String[] columnNames = {
326
                                PluginServices.getText(this, "Simbolo"),
327
                                PluginServices.getText(this, "Valor"),
328
                                PluginServices.getText(this, "Etiqueta") };
329

    
330
                /**
331
                 * Devuelve el n?mero de columnas.
332
                 *
333
                 * @return N?mero de columnas.
334
                 */
335
                public int getColumnCount() {
336
                        return columnNames.length;
337
                }
338

    
339
                /**
340
                 * Devuelve el String del valor de la columna.
341
                 *
342
                 * @param col
343
                 *            N?mero de columna.
344
                 *
345
                 * @return Nombre de la columna.
346
                 */
347
                public String getColumnName(int col) {
348
                        return columnNames[col];
349
                }
350

    
351
                public Class getColumnClass(int c) {
352
            switch (c) {
353
            case 0:
354
                return ISymbol.class;
355
            case 1:
356
                // TODO: take the value from the related FeatureType attribute
357
                Object value = getValueAt(0, c);
358
                return value == null ? Object.class : value.getClass();
359
            case 2:
360
                return String.class;
361
            default:
362
                return Object.class;
363
            }
364
                }
365

    
366
                /*
367
                 * Don't need to implement this method unless your table's editable.
368
                 */
369
                public boolean isCellEditable(int row, int col) {
370
                        // Note that the data/cell address is constant,
371
                        // no matter where the cell appears onscreen.
372
                        // if (col > 0) {
373
                        return true;
374
                }
375

    
376
                @Override
377
                public Object getValueAt(int row, int column) {
378
                        if(column == 2)
379
                                return ((ISymbol)getValueAt(row,0)).getDescription();
380

    
381
                        return super.getValueAt(row, column);
382
                }
383

    
384
                @Override
385
                public void setValueAt(Object aValue, int row, int column) {
386

    
387
                        if(column == 2){
388
                                ISymbol symbol = (ISymbol) getValueAt(row,0);
389
                                symbol.setDescription((String) aValue);
390
                                setValueAt(symbol,row,0);
391
                        }
392

    
393
                        super.setValueAt(aValue, row, column);
394
                }
395

    
396
        }
397
        
398
        
399
        
400
    public void moveDownRows(int startPos, int endPos, int numOfElements) {
401
        if(startPos > endPos)
402
            return;
403
        if(startPos >= getRowCount()-1 )
404
            return;
405
        if(startPos == getRowCount()-1)
406
            return;
407

    
408
        Object[][] values = new Object[getRowCount()][3];
409
        for (int i = 0; i < getRowCount(); i++) {
410
            values[i][0] = table.getModel().getValueAt(i,0);
411
            values[i][1] = table.getModel().getValueAt(i,1);
412
            values[i][2] = table.getModel().getValueAt(i,2);
413
        }
414

    
415
        Object[][]aux = new Object[numOfElements][3];
416
        for (int i = 0; i < numOfElements; i++) {
417

    
418
            aux[numOfElements - i - 1][0] = values[startPos - i][0];
419
            aux[numOfElements - i - 1][1] = values[startPos - i][1];
420
            aux[numOfElements - i - 1][2] = values[startPos - i][2];
421
        }
422

    
423
        Object [][] targetVal = {{values[endPos][0],values[endPos][1],values[endPos][2]}};
424

    
425
        values[startPos - numOfElements + 1][0] = targetVal[0][0];
426
        values[startPos - numOfElements + 1][1] = targetVal[0][1];
427
        values[startPos - numOfElements + 1][2] = targetVal[0][2];
428

    
429
        for (int i = 0; i < numOfElements; i++) {
430
            values[endPos - i][0] = aux[numOfElements - i - 1][0];
431
            values[endPos - i][1] = aux[numOfElements - i - 1][1];
432
            values[endPos - i][2] = aux[numOfElements - i - 1][2];
433
        }
434

    
435
        ISymbol[] symbols = new ISymbol[getRowCount()];
436
        Object[] objects = new Object[getRowCount()];
437
        String[] cads = new String[getRowCount()];
438

    
439
        for (int i = 0; i < getRowCount(); i++) {
440
            symbols[i] = (ISymbol) values[i][0];
441
            objects[i] = values[i][1];
442
            cads[i] = (String) values[i][2];
443
        }
444

    
445
        removeAllItems();
446
        fillTableFromSymbolList(symbols,objects,cads);
447
        table.addRowSelectionInterval(endPos-numOfElements+1,endPos);
448
    }
449

    
450

    
451
    public void moveUpRows(int startPos, int endPos, int numOfElements) {
452

    
453
        if(startPos == 0)
454
            return;
455
        if(endPos > startPos)
456
            return;
457

    
458

    
459
        Object[][] values = new Object[getRowCount()][3];
460
        for (int i = 0; i < getRowCount(); i++) {
461
            values[i][0] = table.getModel().getValueAt(i,0);
462
            values[i][1] = table.getModel().getValueAt(i,1);
463
            values[i][2] = table.getModel().getValueAt(i,2);
464
        }
465

    
466
        Object[][]aux = new Object[numOfElements][3];
467
        for (int i = 0; i < numOfElements; i++) {
468

    
469
            aux[i][0] = values[startPos + i][0];
470
            aux[i][1] = values[startPos + i][1];
471
            aux[i][2] = values[startPos + i][2];
472
        }
473

    
474
        Object [][] targetVal = {{values[endPos][0],values[endPos][1],values[endPos][2]}};
475

    
476
        values[startPos + numOfElements - 1][0] = targetVal[0][0];
477
        values[startPos + numOfElements - 1][1] = targetVal[0][1];
478
        values[startPos + numOfElements - 1][2] = targetVal[0][2];
479

    
480
        for (int i = 0; i < numOfElements; i++) {
481

    
482
            values[endPos + i][0] = aux[i][0];
483
            values[endPos + i][1] = aux[i][1];
484
            values[endPos + i][2] = aux[i][2];
485
        }
486

    
487
        ISymbol[] symbols = new ISymbol[getRowCount()];
488
        Object[] objects = new Object[getRowCount()];
489
        String[] cads = new String[getRowCount()];
490

    
491
        for (int i = 0; i < getRowCount(); i++) {
492
            symbols[i] = (ISymbol) values[i][0];
493
            objects[i] = values[i][1];
494
            cads[i] = (String) values[i][2];
495
        }
496

    
497
        removeAllItems();
498
        fillTableFromSymbolList(symbols,objects,cads);
499
        table.addRowSelectionInterval(endPos,endPos+numOfElements-1);
500
    }
501
    
502
    
503
    public int[] getSelectedRows(){
504
        return table.getSelectedRows();
505
    }
506

    
507
}