Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / FSymbolTable.java @ 11195

History | View | Annotate | Download (11.2 KB)

1 7304 caballero
/*
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.project.documents.view.legend.gui;
48
49 7738 jaume
import java.awt.Dimension;
50
import java.awt.GridLayout;
51 7304 caballero
52 7738 jaume
import javax.swing.JPanel;
53
import javax.swing.JScrollPane;
54
import javax.swing.table.DefaultTableModel;
55
import javax.swing.table.TableColumn;
56 7304 caballero
57 7738 jaume
import com.hardcode.gdbms.engine.values.NullValue;
58
import com.iver.andami.PluginServices;
59 9638 jaume
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
60 7304 caballero
import com.iver.cit.gvsig.fmap.rendering.NullIntervalValue;
61
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
62
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.FCellSymbolRenderer;
63
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.IntervalCellEditor;
64
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.SymbolCellEditor;
65
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.ValueCellEditor;
66
import com.iver.utiles.swing.jtable.JTable;
67
import com.iver.utiles.swing.jtable.TextFieldCellEditor;
68
69
/**
70
 * JPanel que contiene la tabla con los s?mbolos intervalos o valores y
71
 * etiquetado de estos valores.
72 10825 jaume
 *
73 7304 caballero
 * @author Vicente Caballero Navarro
74
 */
75
public class FSymbolTable extends JPanel {
76 10825 jaume
        private static final long serialVersionUID = 1L;
77 7304 caballero
78 10825 jaume
        // private boolean DEBUG = true;
79
        // private MyTableModel m_TableModel;
80
        private JTable table;
81 7304 caballero
82 10825 jaume
        private String type;
83
84 10679 jaume
        private int shapeType;
85
86 10825 jaume
        /**
87
         * Crea un nuevo FSymbolTable.
88
         *
89
         * @param type
90
         *            tipo de valor si es intervalo: "intervals" y si es por
91
         *            valores: "values".
92
         */
93
        public FSymbolTable(String type, int shapeType) {
94
                super(new GridLayout(1, 0));
95
                this.type = type;
96
                this.shapeType = shapeType;
97
                table = new JTable();
98
                table.setModel(new MyTableModel());
99
                table.setPreferredScrollableViewportSize(new Dimension(480, 110));
100 7304 caballero
101 10825 jaume
                // Create the scroll pane and add the table to it.
102
                JScrollPane scrollPane = new JScrollPane(table);
103 7304 caballero
104 10825 jaume
                // Set up column sizes.
105
                // initColumnSizes(table);
106
                setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
107
                setUpValueColumn(table, table.getColumnModel().getColumn(1));
108
                setUpLabelColumn(table, table.getColumnModel().getColumn(2));
109 7304 caballero
110 10825 jaume
                // Add the scroll pane to this panel.
111
                add(scrollPane);
112
                table.setRowSelectionAllowed(true);
113
        }
114 7304 caballero
115 10825 jaume
        /**
116
         * A?ade una fila al modelo.
117
         *
118
         * @param vector
119
         *            Fila en forma de vector de Object para a?adir al modelo.
120
         */
121
        public void addRow(Object[] vector) {
122
                MyTableModel m = (MyTableModel) table.getModel();
123
                m.addRow(vector);
124
        }
125 7304 caballero
126 10825 jaume
        /**
127
         * Elimina la fila que tiene como clave el objeto que se pasa como
128
         * par?metro.
129
         *
130
         * @param obj
131
         *            clave del objeto a eliminar.
132
         */
133
        public void removeRow(Object obj) {
134
                MyTableModel m = (MyTableModel) table.getModel();
135 7304 caballero
136 10825 jaume
                for (int i = 0; i < m.getRowCount(); i++) {
137
                        if (m.getValueAt(i, 1) instanceof NullUniqueValue
138
                                        || m.getValueAt(i, 1) instanceof NullIntervalValue) {
139
                                m.removeRow(i);
140
                        }
141
                }
142
        }
143 7304 caballero
144 10825 jaume
        /**
145
         * Elimina las filas que est?n seleccionadas.
146
         */
147
        public void removeSelectedRows() {
148
                if (table.getCellEditor() != null) {
149
                        table.getCellEditor().cancelCellEditing();
150
                }
151 7304 caballero
152 10825 jaume
                MyTableModel m = (MyTableModel) table.getModel();
153
                int[] selectedRows = table.getSelectedRows();
154 7304 caballero
155 10825 jaume
                for (int i = selectedRows.length - 1; i >= 0; i--) {
156
                        m.removeRow(selectedRows[i]);
157
                }
158
        }
159 7304 caballero
160 10825 jaume
        /**
161
         * Rellena la tabla con los s?mbolos valores y descripciones que se pasan
162
         * como par?metro.
163
         *
164
         * @param symbols
165
         *            Array de s?mbolos
166
         * @param values
167
         *            Array de valores.
168
         * @param descriptions
169
         *            Array de descripciones.
170
         */
171
        public void fillTableFromSymbolList(ISymbol[] symbols, Object[] values,
172
                        String[] descriptions) {
173
                ISymbol theSymbol;
174 7304 caballero
175 10825 jaume
                for (int i = 0; i < symbols.length; i++) {
176
                        theSymbol = symbols[i];
177
                        addTableRecord(theSymbol, values[i], descriptions[i]);
178
                }
179
        }
180 7304 caballero
181 10825 jaume
        /**
182
         * A?ade una fila con los objetos que se pasan como par?metros.
183
         *
184
         * @param symbol
185
         *            s?mbolo de la fila.
186
         * @param value
187
         *            Valor de la fila.
188
         * @param description
189
         *            Descripci?n.
190
         */
191
        public void addTableRecord(ISymbol symbol, Object value, String description) {
192
                Object[] theRow = new Object[3];
193
                theRow[0] = symbol;
194
                theRow[1] = value;
195
                theRow[2] = description;
196
                addRow(theRow);
197
        }
198 7304 caballero
199 10825 jaume
        /**
200
         * Devuelve el valor a partie del n?mero de fila y columna.
201
         *
202
         * @param row
203
         *            n?mero de fila.
204
         * @param col
205
         *            n?mero de columna.
206
         *
207
         * @return Objeto.
208
         */
209
        public Object getFieldValue(int row, int col) {
210
                MyTableModel m = (MyTableModel) table.getModel();
211 7304 caballero
212 10825 jaume
                return m.getValueAt(row, col);
213
        }
214 7304 caballero
215 10825 jaume
        /**
216
         * Devuelve el n?mero total de filas que contiene el modelo.
217
         *
218
         * @return N?mero de filas.
219
         */
220
        public int getRowCount() {
221
                MyTableModel m = (MyTableModel) table.getModel();
222 7304 caballero
223 10825 jaume
                return m.getRowCount();
224
        }
225 7304 caballero
226 10825 jaume
        /**
227
         * Elimina todas las filas del modelo.
228
         */
229
        public void removeAllItems() {
230
                table.setModel(new MyTableModel());
231
                setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
232
                setUpValueColumn(table, table.getColumnModel().getColumn(1));
233
                setUpLabelColumn(table, table.getColumnModel().getColumn(2));
234
        }
235 7304 caballero
236 10825 jaume
        /**
237
         * Inicializa el cell editor de tipo descripci?n de la columna que se pasa
238
         * como par?metro.
239
         *
240
         * @param table2
241
         *            Tabla.
242
         * @param column
243
         *            Columna.
244
         */
245
        public void setUpLabelColumn(JTable table2, TableColumn column) {
246
                TextFieldCellEditor labeleditor = new TextFieldCellEditor();
247
                column.setCellEditor(labeleditor);
248
        }
249 7304 caballero
250 10825 jaume
        /**
251
         * Inicializa el cell editor de tipo valor de la columna que se pasa como
252
         * par?metro.
253
         *
254
         * @param table2
255
         *            Tabla.
256
         * @param column
257
         *            Columna.
258
         */
259
        public void setUpValueColumn(JTable table2, TableColumn column) {
260
                if (type.equals("intervals")) {
261
                        // FIntervalCellEditor intervaleditor = new FIntervalCellEditor();
262
                        IntervalCellEditor intervaleditor = new IntervalCellEditor();
263
                        column.setCellEditor(intervaleditor);
264 7304 caballero
265 10825 jaume
                        // /FCellIntervalRenderer renderer = new
266
                        // FCellIntervalRenderer(true);
267
                        // /column.setCellRenderer(renderer);
268
                } else {
269
                        // /FValueCellEditor valueeditor = new FValueCellEditor();
270
                        // /TextFieldCellEditor valueeditor = new TextFieldCellEditor();
271
                        ValueCellEditor valueeditor = new ValueCellEditor();
272
                        column.setCellEditor(valueeditor);
273
                }
274
        }
275 7304 caballero
276 10825 jaume
        /*
277
         * This method picks good column sizes. If all column heads are wider than
278
         * the column's cells' contents, then you can just use
279
         * column.sizeWidthToFit().
280
         */
281 7304 caballero
282 10825 jaume
        // private void initColumnSizes(JTable table) {
283
        // MyTableModel model = (MyTableModel) table.getModel();
284
        // TableColumn column = null;
285
        // Component comp = null;
286
        // int headerWidth = 0;
287
        // int cellWidth = 0;
288
        // TableCellRenderer headerRenderer = table.getTableHeader()
289
        // .getDefaultRenderer();
290
        // }
291
        /**
292
         * Inicializa el cell editor de tipo s?mbolo de la columna que se pasa como
293
         * par?metro.
294
         *
295
         * @param table2
296
         *            Tabla.
297
         * @param column
298
         *            Columna.
299
         */
300
        public void setUpSymbolColumn(JTable table2, TableColumn column) {
301
                // Set up the editor
302
                column.setMaxWidth(100);
303
                column.setWidth(60);
304
                column.setPreferredWidth(60);
305
                column.setMinWidth(50);
306 7304 caballero
307 10825 jaume
                // FSymbolCellEditor symboleditor = new FSymbolCellEditor();
308
                SymbolCellEditor symboleditor = new SymbolCellEditor(shapeType);
309
                column.setCellEditor(symboleditor);
310 7304 caballero
311 10825 jaume
                FCellSymbolRenderer renderer = new FCellSymbolRenderer(true);
312
                column.setCellRenderer(renderer);
313
        }
314 7304 caballero
315 10825 jaume
        /**
316
         * Modelo que propio que se aplica a la tabla.
317
         *
318
         * @author Vicente Caballero Navarro
319
         */
320
        class MyTableModel extends DefaultTableModel {
321
                private static final long serialVersionUID = 1L;
322 7304 caballero
323 10825 jaume
                // AbstractTableModel {
324
                private String[] columnNames = {
325
                                PluginServices.getText(this, "Simbolo"),
326
                                PluginServices.getText(this, "Valor"),
327
                                PluginServices.getText(this, "Etiqueta") };
328 7304 caballero
329 10825 jaume
                /**
330
                 * Devuelve el n?mero de columnas.
331
                 *
332
                 * @return N?mero de columnas.
333
                 */
334
                public int getColumnCount() {
335
                        return columnNames.length;
336
                }
337 7304 caballero
338 10825 jaume
                /**
339
                 * Devuelve el String del valor de la columna.
340
                 *
341
                 * @param col
342
                 *            N?mero de columna.
343
                 *
344
                 * @return Nombre de la columna.
345
                 */
346
                public String getColumnName(int col) {
347
                        return columnNames[col];
348
                }
349 7304 caballero
350 10825 jaume
                /**
351
                 * JTable uses this method to determine the default renderer/ editor for
352
                 * each cell. If we didn't implement this method, then the last column
353
                 * would contain text ("true"/"false"), rather than a check box.
354
                 */
355
                public Class getColumnClass(int c) {
356
                        if (getValueAt(0, c) == null) {
357
                                return NullValue.class;
358
                        }
359 7304 caballero
360 10825 jaume
                        return getValueAt(0, c).getClass();
361
                }
362 7304 caballero
363 10825 jaume
                /*
364
                 * Don't need to implement this method unless your table's editable.
365
                 */
366
                public boolean isCellEditable(int row, int col) {
367
                        // Note that the data/cell address is constant,
368
                        // no matter where the cell appears onscreen.
369
                        // if (col > 0) {
370
                        return true;
371
                }
372 7304 caballero
373 10825 jaume
                /*
374
                 * Don't need to implement this method unless your table's data can
375
                 * change.
376
                 */
377
                /*
378
                 * public void setValueAt(Object value, int row, int col) { if (DEBUG) {
379
                 * System.out.println("Setting value at " + row + "," + col + " to " +
380
                 * value + " (an instance of " + value.getClass() + ")"); } // FSymbol
381
                 * theSymbol; switch (col) { case 0: // Simbolo //
382
                 * m_FRenderer.setValueSymbol(row,(FSymbol) value); break; case 1: //
383
                 * Clave FInterval newInterval = FInterval.parseString((String) value);
384
                 * this.setValueAt(newInterval,row,col); break; case 2: // Etiqueta //
385
                 * theSymbol = m_FRenderer.getSymbolByID(row); // theSymbol.m_Descrip =
386
                 * (String) value;
387
                 *  } // data[row][col] = value; fireTableCellUpdated(row, col); }
388
                 */
389
                /*
390
                 * private void printDebugData() { int numRows = getRowCount(); int
391
                 * numCols = getColumnCount(); for (int i=0; i < numRows; i++) {
392
                 * System.out.print(" row " + i + ":"); for (int j=0; j < numCols; j++) {
393
                 * System.out.print(" " + data[i][j]); } System.out.println(); }
394
                 * System.out.println("--------------------------"); }
395
                 */
396
        }
397 7304 caballero
}