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

History | View | Annotate | Download (14.5 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
        }
99
        /**
100
         * Inicializa los valores de los CellEditors que la SymbolTable poseer? por defecto
101
         */
102
        private void initializeCellEditors() {
103
                this.cellEditors.put(this.INTERVALS_TYPE,new IntervalCellEditor());
104
                this.cellEditors.put(this.VALUES_TYPE, new ValueCellEditor());
105
        }
106
        /**
107
         * A?ade un nuevo CellEditor a la lista de disponibles
108
         *
109
         * @param key String con el nombre identificativo del CellEditor
110
         * @param cellEditor CellEditor que va a ser a?adido
111
         */
112
        public static void addNewCellEditor(String key,TableCellEditor cellEditor ) {
113
                cellEditors.put(key, cellEditor);
114
        }
115
        /**
116
         * Obtiene el valor de los elementos de una fila seleccionada
117
         *
118
         * @return Object[] Array con los objetos de cada una de las columnas de la fila seleccionada
119
         */
120
        public Object[] getSelectedRowElements() {
121
                Object[] values = new Object[3];
122

    
123
                MyTableModel m = (MyTableModel) table.getModel();
124
                int[] selectedRows = table.getSelectedRows();
125

    
126
                if(selectedRows.length != 1)
127
                        return null;
128

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

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

    
145
        }
146

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

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

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

    
176
                MyTableModel m = (MyTableModel) table.getModel();
177
                int[] selectedRows = table.getSelectedRows();
178

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

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

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

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

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

    
239
                return m.getValueAt(row, col);
240
        }
241

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

    
250
                return m.getRowCount();
251
        }
252

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

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

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

    
306
                // FSymbolCellEditor symboleditor = new FSymbolCellEditor();
307
                SymbolCellEditor symboleditor = new SymbolCellEditor(shapeType);
308
                column.setCellEditor(symboleditor);
309

    
310
                TableSymbolCellRenderer renderer = new TableSymbolCellRenderer(true);
311
                column.setCellRenderer(renderer);
312
        }
313

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

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

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

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

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

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

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

    
379
                        return super.getValueAt(row, column);
380
                }
381

    
382
                @Override
383
                public void setValueAt(Object aValue, int row, int column) {
384

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

    
391
                        super.setValueAt(aValue, row, column);
392
                }
393

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

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

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

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

    
421
        Object [][] targetVal = {{values[endPos][0],values[endPos][1],values[endPos][2]}};
422

    
423
        values[startPos - numOfElements + 1][0] = targetVal[0][0];
424
        values[startPos - numOfElements + 1][1] = targetVal[0][1];
425
        values[startPos - numOfElements + 1][2] = targetVal[0][2];
426

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

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

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

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

    
448

    
449
    public void moveUpRows(int startPos, int endPos, int numOfElements) {
450

    
451
        if(startPos == 0)
452
            return;
453
        if(endPos > startPos)
454
            return;
455

    
456

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

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

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

    
472
        Object [][] targetVal = {{values[endPos][0],values[endPos][1],values[endPos][2]}};
473

    
474
        values[startPos + numOfElements - 1][0] = targetVal[0][0];
475
        values[startPos + numOfElements - 1][1] = targetVal[0][1];
476
        values[startPos + numOfElements - 1][2] = targetVal[0][2];
477

    
478
        for (int i = 0; i < numOfElements; i++) {
479

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

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

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

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

    
505
}