Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / thememanager / legendmanager / panels / FSymbolTable.java @ 2796

History | View | Annotate | Download (12.8 KB)

1
/*
2
 * Created on 27-abr-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package com.iver.cit.gvsig.gui.thememanager.legendmanager.panels;
48

    
49
import com.hardcode.gdbms.engine.values.NullValue;
50

    
51
import com.iver.andami.PluginServices;
52

    
53
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
54
import com.iver.cit.gvsig.fmap.rendering.NullIntervalValue;
55
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
56
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.FCellSymbolRenderer;
57
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.IntervalCellEditor;
58
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.SymbolCellEditor;
59
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.ValueCellEditor;
60

    
61
import com.iver.utiles.swing.jtable.JTable;
62
import com.iver.utiles.swing.jtable.TextFieldCellEditor;
63

    
64
import java.awt.Dimension;
65
import java.awt.GridLayout;
66

    
67
import javax.swing.JPanel;
68
import javax.swing.JScrollPane;
69
import javax.swing.table.DefaultTableModel;
70
import javax.swing.table.TableColumn;
71

    
72

    
73
/**
74
 * JPanel que contiene la tabla con los s?mbolos intervalos o valores y
75
 * etiquetado de estos valores.
76
 *
77
 * @author Vicente Caballero Navarro
78
 */
79
public class FSymbolTable extends JPanel {
80
    private static final long serialVersionUID = 1L;
81

    
82
    //        private boolean DEBUG = true;
83
    // private  MyTableModel m_TableModel;
84
    private JTable table;
85
    private String type;
86

    
87
    /**
88
     * Crea un nuevo FSymbolTable.
89
     *
90
     * @param type tipo de valor si es intervalo: "intervals" y si es por
91
     *        valores: "values".
92
     */
93
    public FSymbolTable(String type) {
94
        super(new GridLayout(1, 0));
95
        this.type = type;
96
        table = new JTable();
97
        table.setModel(new MyTableModel());
98
        table.setPreferredScrollableViewportSize(new Dimension(480, 110));
99

    
100
        //Create the scroll pane and add the table to it.
101
        JScrollPane scrollPane = new JScrollPane(table);
102

    
103
        //Set up column sizes.
104
        //initColumnSizes(table);
105
        setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
106
        setUpValueColumn(table, table.getColumnModel().getColumn(1));
107
        setUpLabelColumn(table, table.getColumnModel().getColumn(2));
108

    
109
        //Add the scroll pane to this panel.
110
        add(scrollPane);
111
        table.setRowSelectionAllowed(true);
112
    }
113

    
114
    /**
115
     * A?ade una fila al modelo.
116
     *
117
     * @param vector Fila en forma de vector de Object para a?adir al modelo.
118
     */
119
    public void addRow(Object[] vector) {
120
        MyTableModel m = (MyTableModel) table.getModel();
121
        m.addRow(vector);
122
    }
123

    
124
    /**
125
     * Elimina la fila que tiene como clave el objeto que se pasa como
126
     * par?metro.
127
     *
128
     * @param obj clave del objeto a eliminar.
129
     */
130
    public void removeRow(Object obj) {
131
        MyTableModel m = (MyTableModel) table.getModel();
132

    
133
        for (int i = 0; i < m.getRowCount(); i++) {
134
            if (m.getValueAt(i, 1) instanceof NullUniqueValue ||
135
                    m.getValueAt(i, 1) instanceof NullIntervalValue) {
136
                m.removeRow(i);
137
            }
138
        }
139
    }
140

    
141
    /**
142
     * Elimina las filas que est?n seleccionadas.
143
     */
144
    public void removeSelectedRows() {
145
        if (table.getCellEditor() != null) {
146
            table.getCellEditor().cancelCellEditing();
147
        }
148

    
149
        MyTableModel m = (MyTableModel) table.getModel();
150
        int[] selectedRows = table.getSelectedRows();
151

    
152
        for (int i = selectedRows.length - 1; i >= 0; i--) {
153
            m.removeRow(selectedRows[i]);
154
        }
155
    }
156

    
157
    /**
158
     * Rellena la tabla con los s?mbolos valores y descripciones que se pasan
159
     * como par?metro.
160
     *
161
     * @param symbols Array de s?mbolos
162
     * @param values Array de valores.
163
     * @param descriptions Array de descripciones.
164
     */
165
    public void fillTableFromSymbolList(FSymbol[] symbols, Object[] values,
166
        String[] descriptions) {
167
        FSymbol theSymbol;
168

    
169
        for (int i = 0; i < symbols.length; i++) {
170
            theSymbol = symbols[i];
171
            addTableRecord(theSymbol, values[i], descriptions[i]);
172
        }
173
    }
174

    
175
    /**
176
     * A?ade una fila con los objetos que se pasan como par?metros.
177
     *
178
     * @param symbol s?mbolo de la fila.
179
     * @param value Valor de la fila.
180
     * @param description Descripci?n.
181
     */
182
    public void addTableRecord(FSymbol symbol, Object value, String description) {
183
        Object[] theRow = new Object[3];
184
        theRow[0] = symbol;
185
        theRow[1] = value;
186
        theRow[2] = description;
187
        addRow(theRow);
188
    }
189

    
190
    /**
191
     * Devuelve el valor a partie del n?mero de fila y columna.
192
     *
193
     * @param row n?mero de fila.
194
     * @param col n?mero de columna.
195
     *
196
     * @return Objeto.
197
     */
198
    public Object getFieldValue(int row, int col) {
199
        MyTableModel m = (MyTableModel) table.getModel();
200

    
201
        return m.getValueAt(row, col);
202
    }
203

    
204
    /**
205
     * Devuelve el n?mero total de filas que contiene el modelo.
206
     *
207
     * @return N?mero de filas.
208
     */
209
    public int getRowCount() {
210
        MyTableModel m = (MyTableModel) table.getModel();
211

    
212
        return m.getRowCount();
213
    }
214

    
215
    /**
216
     * Elimina todas las filas del modelo.
217
     */
218
    public void removeAllItems() {
219
        table.setModel(new MyTableModel());
220
        setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
221
        setUpValueColumn(table, table.getColumnModel().getColumn(1));
222
        setUpLabelColumn(table, table.getColumnModel().getColumn(2));
223
    }
224

    
225
    /**
226
     * Inicializa el cell editor de tipo descripci?n de la columna que se pasa
227
     * como par?metro.
228
     *
229
     * @param table2 Tabla.
230
     * @param column Columna.
231
     */
232
    public void setUpLabelColumn(JTable table2, TableColumn column) {
233
        TextFieldCellEditor labeleditor = new TextFieldCellEditor();
234
        column.setCellEditor(labeleditor);
235
    }
236

    
237
    /**
238
     * Inicializa el cell editor de tipo valor de la columna que se pasa como
239
     * par?metro.
240
     *
241
     * @param table2 Tabla.
242
     * @param column Columna.
243
     */
244
    public void setUpValueColumn(JTable table2, TableColumn column) {
245
        if (type.equals("intervals")) {
246
            //FIntervalCellEditor intervaleditor = new FIntervalCellEditor();
247
            IntervalCellEditor intervaleditor = new IntervalCellEditor();
248
            column.setCellEditor(intervaleditor);
249

    
250
            ///FCellIntervalRenderer renderer = new FCellIntervalRenderer(true);
251
            ///column.setCellRenderer(renderer);
252
        } else {
253
            ///FValueCellEditor valueeditor = new FValueCellEditor();
254
            ///TextFieldCellEditor valueeditor = new TextFieldCellEditor();
255
            ValueCellEditor valueeditor = new ValueCellEditor();
256
            column.setCellEditor(valueeditor);
257
        }
258
    }
259

    
260
    /*
261
     * This method picks good column sizes.
262
     * If all column heads are wider than the column's cells'
263
     * contents, then you can just use column.sizeWidthToFit().
264
     */
265

    
266
    //private void initColumnSizes(JTable table) {
267
    //MyTableModel model = (MyTableModel) table.getModel();
268
    //TableColumn column = null;
269
    //Component comp = null;
270
    //int headerWidth = 0;
271
    //int cellWidth = 0;
272
    //TableCellRenderer headerRenderer = table.getTableHeader()
273
    //                                                                                .getDefaultRenderer();
274
    //}
275

    
276
    /**
277
     * Inicializa el cell editor de tipo s?mbolo de la columna que se pasa como
278
     * par?metro.
279
     *
280
     * @param table2 Tabla.
281
     * @param column Columna.
282
     */
283
    public void setUpSymbolColumn(JTable table2, TableColumn column) {
284
        //Set up the editor 
285
        column.setMaxWidth(100);
286
        column.setWidth(60);
287
        column.setPreferredWidth(60);
288
        column.setMinWidth(50);
289

    
290
        //FSymbolCellEditor symboleditor = new FSymbolCellEditor();
291
        SymbolCellEditor symboleditor = new SymbolCellEditor();
292
        column.setCellEditor(symboleditor);
293

    
294
        FCellSymbolRenderer renderer = new FCellSymbolRenderer(true);
295
        column.setCellRenderer(renderer);
296
    }
297

    
298
    /**
299
     * Modelo que propio que se aplica a la tabla.
300
     *
301
     * @author Vicente Caballero Navarro
302
     */
303
    class MyTableModel extends DefaultTableModel { /**
304
         *
305
         */
306

    
307
        private static final long serialVersionUID = 1L;
308

    
309
        //  AbstractTableModel {
310
        private String[] columnNames = {
311
                PluginServices.getText(this, "Simbolo"),
312
                PluginServices.getText(this, "Valor"),
313
                PluginServices.getText(this, "Etiqueta")
314
            };
315

    
316
        /**
317
         * Devuelve el n?mero de columnas.
318
         *
319
         * @return N?mero de columnas.
320
         */
321
        public int getColumnCount() {
322
            return columnNames.length;
323
        }
324

    
325
        /**
326
         * Devuelve el String del valor de la columna.
327
         *
328
         * @param col N?mero de columna.
329
         *
330
         * @return Nombre de la columna.
331
         */
332
        public String getColumnName(int col) {
333
            return columnNames[col];
334
        }
335

    
336
        /*
337
         * JTable uses this method to determine the default renderer/
338
         * editor for each cell.  If we didn't implement this method,
339
         * then the last column would contain text ("true"/"false"),
340
         * rather than a check box.
341
         */
342
        public Class getColumnClass(int c) {
343
            if (getValueAt(0, c) == null) {
344
                return NullValue.class;
345
            }
346

    
347
            return getValueAt(0, c).getClass();
348
        }
349

    
350
        /*
351
         * Don't need to implement this method unless your table's
352
         * editable.
353
         */
354
        public boolean isCellEditable(int row, int col) {
355
            //Note that the data/cell address is constant,
356
            //no matter where the cell appears onscreen.
357
            //if (col > 0) {
358
            return true;
359

    
360
            /* } else {
361
               return false;
362
               } */
363
        }
364

    
365
        /*
366
         * Don't need to implement this method unless your table's
367
         * data can change.
368
         */
369
        /* public void setValueAt(Object value, int row, int col) {
370
           if (DEBUG) {
371
                   System.out.println("Setting value at " + row + "," + col
372
                                                      + " to " + value
373
                                                      + " (an instance of "
374
                                                      + value.getClass() + ")");
375
           }
376
           // FSymbol theSymbol;
377
           switch (col)
378
           {
379
                   case 0: // Simbolo
380
                           // m_FRenderer.setValueSymbol(row,(FSymbol) value);
381
                           break;
382
                   case 1: // Clave
383
                           FInterval newInterval = FInterval.parseString((String) value);
384
                           this.setValueAt(newInterval,row,col);
385
                           break;
386
                   case 2: // Etiqueta
387
                           // theSymbol = m_FRenderer.getSymbolByID(row);
388
                           // theSymbol.m_Descrip = (String) value;
389
        
390
           }
391
           // data[row][col] = value;
392
           fireTableCellUpdated(row, col);
393
           }  */
394
        /* private void printDebugData() {
395
           int numRows = getRowCount();
396
           int numCols = getColumnCount();
397
           for (int i=0; i < numRows; i++) {
398
                   System.out.print("    row " + i + ":");
399
                   for (int j=0; j < numCols; j++) {
400
                           System.out.print("  " + data[i][j]);
401
                   }
402
                   System.out.println();
403
           }
404
           System.out.println("--------------------------");
405
           } */
406
    }
407
}