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