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

History | View | Annotate | Download (14.4 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.HashMap;
31
import java.util.Map;
32

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

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

    
48

    
49
/**
50
 * JPanel que contiene la tabla con los s?mbolos intervalos o valores y
51
 * etiquetado de estos valores.
52
 *
53
 * @author Vicente Caballero Navarro
54
 */
55
public class SymbolTable extends JPanel {
56
        private static final long serialVersionUID = -8694846716328735113L;
57
  
58
        public static final String VALUES_TYPE = "values";
59
        public static final String INTERVALS_TYPE = "intervals";
60

    
61
  private static final Map<String,TableCellEditor> CELL_EDITORS = new HashMap<>();
62
        
63
  private final JTable table;
64
        private final String type;
65
        private final int shapeType;
66

    
67
        public SymbolTable(Component ownerComponent, String type, int shapeType) {
68
    this(new org.gvsig.utils.swing.jtable.JTable(), ownerComponent, type, shapeType);
69
  }
70
  
71
        /**
72
         * Crea un nuevo FSymbolTable.
73
         *
74
   * @param theTable
75
   * @param ownerComponent
76
         * @param type, tipo de valor si es intervalo: "intervals" y si es por valores: "values".
77
   * @param shapeType
78
         */
79
        public SymbolTable(JTable theTable, Component ownerComponent, String type, int shapeType) {
80
                super(new GridLayout(1, 0));
81
                this.type = type;
82
                this.shapeType = shapeType;
83

    
84
                table = theTable;
85
                table.setModel(new MyTableModel());
86
                table.setPreferredScrollableViewportSize(new Dimension(480, 110));
87

    
88
                initializeCellEditors();
89

    
90
                // Create the scroll pane and add the table to it.
91
                JScrollPane scrollPane = new JScrollPane(table);
92

    
93
                // Set up column sizes.
94
                // initColumnSizes(table);
95
                setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
96

    
97
                if(CELL_EDITORS.get(type) == null)
98
                        throw new Error("Symbol table type not set!");
99

    
100
                setUpValueColumn(table, table.getColumnModel().getColumn(1),CELL_EDITORS.get(this.type));
101
                setUpLabelColumn(table, table.getColumnModel().getColumn(2));
102

    
103
                // Add the scroll pane to this panel.
104
                add(scrollPane);
105
                table.setRowSelectionAllowed(true);
106
                // default is 16
107
                table.setRowHeight(24);
108
        }
109
  
110
  public void setCellEditor(TableCellEditor cellEditor) {
111
    TableColumn column = table.getColumnModel().getColumn(1);
112
    column.setCellEditor(cellEditor);
113
  }
114
  
115
        /**
116
         * Inicializa los valores de los CellEditors que la SymbolTable poseer? por defecto
117
         */
118
        private void initializeCellEditors() {
119
                this.CELL_EDITORS.put(this.INTERVALS_TYPE,new IntervalCellEditor());
120
                this.CELL_EDITORS.put(this.VALUES_TYPE, new ValueCellEditor());
121
        }
122
        /**
123
         * A?ade un nuevo CellEditor a la lista de disponibles
124
         *
125
         * @param key String con el nombre identificativo del CellEditor
126
         * @param cellEditor CellEditor que va a ser a?adido
127
         */
128
        public static void addNewCellEditor(String key,TableCellEditor cellEditor ) {
129
                CELL_EDITORS.put(key, cellEditor);
130
        }
131
        /**
132
         * Obtiene el valor de los elementos de una fila seleccionada
133
         *
134
         * @return Object[] Array con los objetos de cada una de las columnas de la fila seleccionada
135
         */
136
        public Object[] getSelectedRowElements() {
137
                Object[] values = new Object[3];
138

    
139
                MyTableModel m = (MyTableModel) table.getModel();
140
                int[] selectedRows = table.getSelectedRows();
141

    
142
                if(selectedRows.length != 1)
143
                        return null;
144

    
145
                for (int i = 0; i < 3; i++) {
146
                        values[i] = m.getValueAt(selectedRows[0], i);
147
                }
148

    
149
                return values;
150
        }
151
        /**
152
         * A?ade una fila al modelo.
153
         *
154
         * @param vector
155
         *            Fila en forma de vector de Object para a?adir al modelo.
156
         */
157
        public void addRow(Object[] vector) {
158
                MyTableModel m = (MyTableModel) table.getModel();
159
                m.addRow(vector);
160

    
161
        }
162

    
163
        /**
164
         * Elimina la fila que tiene como clave el objeto que se pasa como
165
         * par?metro.
166
         *
167
         * @param obj
168
         *            clave del objeto a eliminar.
169
         */
170
        public void removeRow(Object obj) {
171
                MyTableModel m = (MyTableModel) table.getModel();
172

    
173
                for (int i = 0; i < m.getRowCount(); i++) {
174
//                        if (m.getValueAt(i, 1) instanceof NullUniqueValue
175
//                                        || m.getValueAt(i, 1) instanceof NullIntervalValue) {
176
//                                m.removeRow(i);
177
//                        }
178
                        if (m.getValueAt(i, 1) == null) {
179
                                m.removeRow(i);
180
                        }
181
                }
182
        }
183

    
184
        /**
185
         * Elimina las filas que est?n seleccionadas.
186
         */
187
        public void removeSelectedRows() {
188
                if (table.getCellEditor() != null) {
189
                        table.getCellEditor().cancelCellEditing();
190
                }
191

    
192
                MyTableModel m = (MyTableModel) table.getModel();
193
                int[] selectedRows = table.getSelectedRows();
194

    
195
                for (int i = selectedRows.length - 1; i >= 0; i--) {
196
                        m.removeRow(selectedRows[i]);
197
                }
198
        }
199

    
200
        /**
201
         * Rellena la tabla con los s?mbolos valores y descripciones que se pasan
202
         * como par?metro.
203
         *
204
         * @param symbols
205
         *            Array de s?mbolos
206
         * @param values
207
         *            Array de valores.
208
         * @param descriptions
209
         *            Array de descripciones.
210
         */
211
        public void fillTableFromSymbolList(ISymbol[] symbols, Object[] values,
212
                        String[] descriptions) {
213
                ISymbol theSymbol;
214

    
215
                for (int i = 0; i < symbols.length; i++) {
216
                        theSymbol = symbols[i];
217
//                        if(!(values[i] instanceof NullIntervalValue) && !(values[i] instanceof NullUniqueValue))
218
//                                addTableRecord(theSymbol, values[i], descriptions[i]);
219
                        if(!(values[i] == null))
220
                                addTableRecord(theSymbol, values[i], descriptions[i]);
221
                }
222
        }
223

    
224
        /**
225
         * A?ade una fila con los objetos que se pasan como par?metros.
226
         *
227
         * @param symbol
228
         *            s?mbolo de la fila.
229
         * @param value
230
         *            Valor de la fila.
231
         * @param description
232
         *            Descripci?n.
233
         */
234
        public void addTableRecord(ISymbol symbol, Object value, String description) {
235
                Object[] theRow = new Object[3];
236
                theRow[0] = symbol;
237
                theRow[1] = value;
238
                theRow[2] = description;
239
                addRow(theRow);
240
        }
241

    
242
        /**
243
         * Devuelve el valor a partie del n?mero de fila y columna.
244
         *
245
         * @param row
246
         *            n?mero de fila.
247
         * @param col
248
         *            n?mero de columna.
249
         *
250
         * @return Objeto.
251
         */
252
        public Object getFieldValue(int row, int col) {
253
                MyTableModel m = (MyTableModel) table.getModel();
254

    
255
                return m.getValueAt(row, col);
256
        }
257

    
258
        /**
259
         * Devuelve el n?mero total de filas que contiene el modelo.
260
         *
261
         * @return N?mero de filas.
262
         */
263
        public int getRowCount() {
264
                MyTableModel m = (MyTableModel) table.getModel();
265

    
266
                return m.getRowCount();
267
        }
268

    
269
        /**
270
         * Elimina todas las filas del modelo.
271
         */
272
        public void removeAllItems() {
273
                table.setModel(new MyTableModel());
274
                setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
275
                setUpValueColumn(table, table.getColumnModel().getColumn(1),CELL_EDITORS.get(this.type));
276
                setUpLabelColumn(table, table.getColumnModel().getColumn(2));
277
        }
278

    
279
        /**
280
         * Inicializa el cell editor de tipo descripci?n de la columna que se pasa
281
         * como par?metro.
282
         *
283
         * @param table2
284
         *            Tabla.
285
         * @param column
286
         *            Columna.
287
         */
288
        public void setUpLabelColumn(JTable table2, TableColumn column) {
289
                TextFieldCellEditor labeleditor = new TextFieldCellEditor();
290
                column.setCellEditor(labeleditor);
291
        }
292

    
293
        /**
294
         * Inicializa el cell editor de tipo valor de la columna que se pasa como
295
         * par?metro.
296
         *
297
         * @param table2
298
         *            Tabla.
299
         * @param column
300
         *            Columna.
301
         * @param tableCellEditor
302
         */
303
        public void setUpValueColumn(JTable table2,TableColumn column, TableCellEditor tableCellEditor) {
304
                column.setCellEditor(tableCellEditor);
305
        }
306
        /**
307
         * Inicializa el cell editor de tipo s?mbolo de la columna que se pasa como
308
         * par?metro.
309
         *
310
         * @param table2
311
         *            Tabla.
312
         * @param column
313
         *            Columna.
314
         */
315
        public void setUpSymbolColumn(JTable table2, TableColumn column) {
316
                // Set up the editor
317
                column.setMaxWidth(100);
318
                column.setWidth(60);
319
                column.setPreferredWidth(60);
320
                column.setMinWidth(50);
321

    
322
                // FSymbolCellEditor symboleditor = new FSymbolCellEditor();
323
                SymbolCellEditor symboleditor = new SymbolCellEditor(shapeType);
324
                column.setCellEditor(symboleditor);
325

    
326
                TableSymbolCellRenderer renderer = new TableSymbolCellRenderer(true);
327
                column.setCellRenderer(renderer);
328
        }
329

    
330
        class MyTableModel extends DefaultTableModel {
331

    
332
                private final String[] columnNames = {
333
                                PluginServices.getText(this, "Simbolo"),
334
                                PluginServices.getText(this, "Valor"),
335
                                PluginServices.getText(this, "Etiqueta") };
336

    
337
    @Override
338
                public int getColumnCount() {
339
                        return columnNames.length;
340
                }
341

    
342
    @Override
343
                public String getColumnName(int col) {
344
                        return columnNames[col];
345
                }
346

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

    
363
    @Override
364
                public boolean isCellEditable(int row, int col) {
365
                        return true;
366
                }
367

    
368
                @Override
369
                public Object getValueAt(int row, int column) {
370
                        if(column == 2)
371
                                return ((ISymbol)getValueAt(row,0)).getDescription();
372

    
373
                        return super.getValueAt(row, column);
374
                }
375

    
376
                @Override
377
                public void setValueAt(Object aValue, int row, int column) {
378

    
379
                        if(column == 2){
380
                                ISymbol symbol = (ISymbol) getValueAt(row,0);
381
                                symbol.setDescription((String) aValue);
382
                                setValueAt(symbol,row,0);
383
                        }
384

    
385
                        super.setValueAt(aValue, row, column);
386
                }
387

    
388
        }
389
        
390
        
391
        
392
    public void moveDownRows(int startPos, int endPos, int numOfElements) {
393
        if(startPos > endPos)
394
            return;
395
        if(startPos >= getRowCount()-1 )
396
            return;
397
        if(startPos == getRowCount()-1)
398
            return;
399

    
400
        Object[][] values = new Object[getRowCount()][3];
401
        for (int i = 0; i < getRowCount(); i++) {
402
            values[i][0] = table.getModel().getValueAt(i,0);
403
            values[i][1] = table.getModel().getValueAt(i,1);
404
            values[i][2] = table.getModel().getValueAt(i,2);
405
        }
406

    
407
        Object[][]aux = new Object[numOfElements][3];
408
        for (int i = 0; i < numOfElements; i++) {
409

    
410
            aux[numOfElements - i - 1][0] = values[startPos - i][0];
411
            aux[numOfElements - i - 1][1] = values[startPos - i][1];
412
            aux[numOfElements - i - 1][2] = values[startPos - i][2];
413
        }
414

    
415
        Object [][] targetVal = {{values[endPos][0],values[endPos][1],values[endPos][2]}};
416

    
417
        values[startPos - numOfElements + 1][0] = targetVal[0][0];
418
        values[startPos - numOfElements + 1][1] = targetVal[0][1];
419
        values[startPos - numOfElements + 1][2] = targetVal[0][2];
420

    
421
        for (int i = 0; i < numOfElements; i++) {
422
            values[endPos - i][0] = aux[numOfElements - i - 1][0];
423
            values[endPos - i][1] = aux[numOfElements - i - 1][1];
424
            values[endPos - i][2] = aux[numOfElements - i - 1][2];
425
        }
426

    
427
        ISymbol[] symbols = new ISymbol[getRowCount()];
428
        Object[] objects = new Object[getRowCount()];
429
        String[] cads = new String[getRowCount()];
430

    
431
        for (int i = 0; i < getRowCount(); i++) {
432
            symbols[i] = (ISymbol) values[i][0];
433
            objects[i] = values[i][1];
434
            cads[i] = (String) values[i][2];
435
        }
436

    
437
        removeAllItems();
438
        fillTableFromSymbolList(symbols,objects,cads);
439
        table.addRowSelectionInterval(endPos-numOfElements+1,endPos);
440
    }
441

    
442

    
443
    public void moveUpRows(int startPos, int endPos, int numOfElements) {
444

    
445
        if(startPos == 0)
446
            return;
447
        if(endPos > startPos)
448
            return;
449

    
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[i][0] = values[startPos + i][0];
462
            aux[i][1] = values[startPos + i][1];
463
            aux[i][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

    
474
            values[endPos + i][0] = aux[i][0];
475
            values[endPos + i][1] = aux[i][1];
476
            values[endPos + i][2] = aux[i][2];
477
        }
478

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

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

    
489
        removeAllItems();
490
        fillTableFromSymbolList(symbols,objects,cads);
491
        table.addRowSelectionInterval(endPos,endPos+numOfElements-1);
492
    }
493
    
494
    
495
    public int[] getSelectedRows(){
496
        return table.getSelectedRows();
497
    }
498

    
499
}