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 @ 44886
History | View | Annotate | Download (14.9 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.JTable; |
35 |
import javax.swing.table.DefaultTableModel; |
36 |
import javax.swing.table.TableCellEditor; |
37 |
import javax.swing.table.TableColumn; |
38 |
|
39 |
import org.gvsig.andami.PluginServices; |
40 |
import org.gvsig.app.project.documents.gui.SymbolCellEditor; |
41 |
import org.gvsig.app.project.documents.gui.TableSymbolCellRenderer; |
42 |
import org.gvsig.app.project.documents.view.legend.edition.gui.IntervalCellEditor; |
43 |
import org.gvsig.app.project.documents.view.legend.edition.gui.ValueCellEditor; |
44 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
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 |
public SymbolTable(Component ownerComponent, String type, int shapeType) { |
65 |
this(new org.gvsig.utils.swing.jtable.JTable(), ownerComponent, type, shapeType); |
66 |
} |
67 |
|
68 |
/**
|
69 |
* Crea un nuevo FSymbolTable.
|
70 |
*
|
71 |
* @param theTable
|
72 |
* @param ownerComponent
|
73 |
* @param type, tipo de valor si es intervalo: "intervals" y si es por valores: "values".
|
74 |
* @param shapeType
|
75 |
*/
|
76 |
public SymbolTable(JTable theTable, Component ownerComponent, String type, int shapeType) { |
77 |
super(new GridLayout(1, 0)); |
78 |
this.type = type;
|
79 |
this.shapeType = shapeType;
|
80 |
|
81 |
table = theTable; |
82 |
table.setModel(new MyTableModel());
|
83 |
table.setPreferredScrollableViewportSize(new Dimension(480, 110)); |
84 |
|
85 |
initializeCellEditors(); |
86 |
|
87 |
// Create the scroll pane and add the table to it.
|
88 |
JScrollPane scrollPane = new JScrollPane(table); |
89 |
|
90 |
// Set up column sizes.
|
91 |
// initColumnSizes(table);
|
92 |
setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
|
93 |
|
94 |
if(cellEditors.get(type) == null) |
95 |
throw new Error("Symbol table type not set!"); |
96 |
|
97 |
setUpValueColumn(table, table.getColumnModel().getColumn(1),cellEditors.get(this.type)); |
98 |
setUpLabelColumn(table, table.getColumnModel().getColumn(2));
|
99 |
|
100 |
// Add the scroll pane to this panel.
|
101 |
add(scrollPane); |
102 |
table.setRowSelectionAllowed(true);
|
103 |
// default is 16
|
104 |
table.setRowHeight(24);
|
105 |
} |
106 |
|
107 |
public void setCellEditor(TableCellEditor cellEditor) { |
108 |
TableColumn column = table.getColumnModel().getColumn(1); |
109 |
column.setCellEditor(cellEditor); |
110 |
} |
111 |
|
112 |
/**
|
113 |
* Inicializa los valores de los CellEditors que la SymbolTable poseer? por defecto
|
114 |
*/
|
115 |
private void initializeCellEditors() { |
116 |
this.cellEditors.put(this.INTERVALS_TYPE,new IntervalCellEditor()); |
117 |
this.cellEditors.put(this.VALUES_TYPE, new ValueCellEditor()); |
118 |
} |
119 |
/**
|
120 |
* A?ade un nuevo CellEditor a la lista de disponibles
|
121 |
*
|
122 |
* @param key String con el nombre identificativo del CellEditor
|
123 |
* @param cellEditor CellEditor que va a ser a?adido
|
124 |
*/
|
125 |
public static void addNewCellEditor(String key,TableCellEditor cellEditor ) { |
126 |
cellEditors.put(key, cellEditor); |
127 |
} |
128 |
/**
|
129 |
* Obtiene el valor de los elementos de una fila seleccionada
|
130 |
*
|
131 |
* @return Object[] Array con los objetos de cada una de las columnas de la fila seleccionada
|
132 |
*/
|
133 |
public Object[] getSelectedRowElements() { |
134 |
Object[] values = new Object[3]; |
135 |
|
136 |
MyTableModel m = (MyTableModel) table.getModel(); |
137 |
int[] selectedRows = table.getSelectedRows(); |
138 |
|
139 |
if(selectedRows.length != 1) |
140 |
return null; |
141 |
|
142 |
for (int i = 0; i < 3; i++) { |
143 |
values[i] = m.getValueAt(selectedRows[0], i);
|
144 |
} |
145 |
|
146 |
return values;
|
147 |
} |
148 |
/**
|
149 |
* A?ade una fila al modelo.
|
150 |
*
|
151 |
* @param vector
|
152 |
* Fila en forma de vector de Object para a?adir al modelo.
|
153 |
*/
|
154 |
public void addRow(Object[] vector) { |
155 |
MyTableModel m = (MyTableModel) table.getModel(); |
156 |
m.addRow(vector); |
157 |
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* Elimina la fila que tiene como clave el objeto que se pasa como
|
162 |
* par?metro.
|
163 |
*
|
164 |
* @param obj
|
165 |
* clave del objeto a eliminar.
|
166 |
*/
|
167 |
public void removeRow(Object obj) { |
168 |
MyTableModel m = (MyTableModel) table.getModel(); |
169 |
|
170 |
for (int i = 0; i < m.getRowCount(); i++) { |
171 |
// if (m.getValueAt(i, 1) instanceof NullUniqueValue
|
172 |
// || m.getValueAt(i, 1) instanceof NullIntervalValue) {
|
173 |
// m.removeRow(i);
|
174 |
// }
|
175 |
if (m.getValueAt(i, 1) == null) { |
176 |
m.removeRow(i); |
177 |
} |
178 |
} |
179 |
} |
180 |
|
181 |
/**
|
182 |
* Elimina las filas que est?n seleccionadas.
|
183 |
*/
|
184 |
public void removeSelectedRows() { |
185 |
if (table.getCellEditor() != null) { |
186 |
table.getCellEditor().cancelCellEditing(); |
187 |
} |
188 |
|
189 |
MyTableModel m = (MyTableModel) table.getModel(); |
190 |
int[] selectedRows = table.getSelectedRows(); |
191 |
|
192 |
for (int i = selectedRows.length - 1; i >= 0; i--) { |
193 |
m.removeRow(selectedRows[i]); |
194 |
} |
195 |
} |
196 |
|
197 |
/**
|
198 |
* Rellena la tabla con los s?mbolos valores y descripciones que se pasan
|
199 |
* como par?metro.
|
200 |
*
|
201 |
* @param symbols
|
202 |
* Array de s?mbolos
|
203 |
* @param values
|
204 |
* Array de valores.
|
205 |
* @param descriptions
|
206 |
* Array de descripciones.
|
207 |
*/
|
208 |
public void fillTableFromSymbolList(ISymbol[] symbols, Object[] values, |
209 |
String[] descriptions) { |
210 |
ISymbol theSymbol; |
211 |
|
212 |
for (int i = 0; i < symbols.length; i++) { |
213 |
theSymbol = symbols[i]; |
214 |
// if(!(values[i] instanceof NullIntervalValue) && !(values[i] instanceof NullUniqueValue))
|
215 |
// addTableRecord(theSymbol, values[i], descriptions[i]);
|
216 |
if(!(values[i] == null)) |
217 |
addTableRecord(theSymbol, values[i], descriptions[i]); |
218 |
} |
219 |
} |
220 |
|
221 |
/**
|
222 |
* A?ade una fila con los objetos que se pasan como par?metros.
|
223 |
*
|
224 |
* @param symbol
|
225 |
* s?mbolo de la fila.
|
226 |
* @param value
|
227 |
* Valor de la fila.
|
228 |
* @param description
|
229 |
* Descripci?n.
|
230 |
*/
|
231 |
public void addTableRecord(ISymbol symbol, Object value, String description) { |
232 |
Object[] theRow = new Object[3]; |
233 |
theRow[0] = symbol;
|
234 |
theRow[1] = value;
|
235 |
theRow[2] = description;
|
236 |
addRow(theRow); |
237 |
} |
238 |
|
239 |
/**
|
240 |
* Devuelve el valor a partie del n?mero de fila y columna.
|
241 |
*
|
242 |
* @param row
|
243 |
* n?mero de fila.
|
244 |
* @param col
|
245 |
* n?mero de columna.
|
246 |
*
|
247 |
* @return Objeto.
|
248 |
*/
|
249 |
public Object getFieldValue(int row, int col) { |
250 |
MyTableModel m = (MyTableModel) table.getModel(); |
251 |
|
252 |
return m.getValueAt(row, col);
|
253 |
} |
254 |
|
255 |
/**
|
256 |
* Devuelve el n?mero total de filas que contiene el modelo.
|
257 |
*
|
258 |
* @return N?mero de filas.
|
259 |
*/
|
260 |
public int getRowCount() { |
261 |
MyTableModel m = (MyTableModel) table.getModel(); |
262 |
|
263 |
return m.getRowCount();
|
264 |
} |
265 |
|
266 |
/**
|
267 |
* Elimina todas las filas del modelo.
|
268 |
*/
|
269 |
public void removeAllItems() { |
270 |
table.setModel(new MyTableModel());
|
271 |
setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
|
272 |
setUpValueColumn(table, table.getColumnModel().getColumn(1),cellEditors.get(this.type)); |
273 |
setUpLabelColumn(table, table.getColumnModel().getColumn(2));
|
274 |
} |
275 |
|
276 |
/**
|
277 |
* Inicializa el cell editor de tipo descripci?n de la columna que se pasa
|
278 |
* como par?metro.
|
279 |
*
|
280 |
* @param table2
|
281 |
* Tabla.
|
282 |
* @param column
|
283 |
* Columna.
|
284 |
*/
|
285 |
public void setUpLabelColumn(JTable table2, TableColumn column) { |
286 |
TextFieldCellEditor labeleditor = new TextFieldCellEditor();
|
287 |
column.setCellEditor(labeleditor); |
288 |
} |
289 |
|
290 |
/**
|
291 |
* Inicializa el cell editor de tipo valor de la columna que se pasa como
|
292 |
* par?metro.
|
293 |
*
|
294 |
* @param table2
|
295 |
* Tabla.
|
296 |
* @param column
|
297 |
* Columna.
|
298 |
* @param tableCellEditor
|
299 |
*/
|
300 |
public void setUpValueColumn(JTable table2,TableColumn column, TableCellEditor tableCellEditor) { |
301 |
column.setCellEditor(tableCellEditor); |
302 |
} |
303 |
/**
|
304 |
* Inicializa el cell editor de tipo s?mbolo de la columna que se pasa como
|
305 |
* par?metro.
|
306 |
*
|
307 |
* @param table2
|
308 |
* Tabla.
|
309 |
* @param column
|
310 |
* Columna.
|
311 |
*/
|
312 |
public void setUpSymbolColumn(JTable table2, TableColumn column) { |
313 |
// Set up the editor
|
314 |
column.setMaxWidth(100);
|
315 |
column.setWidth(60);
|
316 |
column.setPreferredWidth(60);
|
317 |
column.setMinWidth(50);
|
318 |
|
319 |
// FSymbolCellEditor symboleditor = new FSymbolCellEditor();
|
320 |
SymbolCellEditor symboleditor = new SymbolCellEditor(shapeType);
|
321 |
column.setCellEditor(symboleditor); |
322 |
|
323 |
TableSymbolCellRenderer renderer = new TableSymbolCellRenderer(true); |
324 |
column.setCellRenderer(renderer); |
325 |
} |
326 |
|
327 |
/**
|
328 |
* Modelo que propio que se aplica a la tabla.
|
329 |
*
|
330 |
* @author Vicente Caballero Navarro
|
331 |
*/
|
332 |
class MyTableModel extends DefaultTableModel { |
333 |
private static final long serialVersionUID = 1L; |
334 |
|
335 |
// AbstractTableModel {
|
336 |
private String[] columnNames = { |
337 |
PluginServices.getText(this, "Simbolo"), |
338 |
PluginServices.getText(this, "Valor"), |
339 |
PluginServices.getText(this, "Etiqueta") }; |
340 |
|
341 |
/**
|
342 |
* Devuelve el n?mero de columnas.
|
343 |
*
|
344 |
* @return N?mero de columnas.
|
345 |
*/
|
346 |
public int getColumnCount() { |
347 |
return columnNames.length;
|
348 |
} |
349 |
|
350 |
/**
|
351 |
* Devuelve el String del valor de la columna.
|
352 |
*
|
353 |
* @param col
|
354 |
* N?mero de columna.
|
355 |
*
|
356 |
* @return Nombre de la columna.
|
357 |
*/
|
358 |
public String getColumnName(int col) { |
359 |
return columnNames[col];
|
360 |
} |
361 |
|
362 |
public Class getColumnClass(int c) { |
363 |
switch (c) {
|
364 |
case 0: |
365 |
return ISymbol.class;
|
366 |
case 1: |
367 |
// TODO: take the value from the related FeatureType attribute
|
368 |
Object value = getValueAt(0, c); |
369 |
return value == null ? Object.class : value.getClass(); |
370 |
case 2: |
371 |
return String.class; |
372 |
default:
|
373 |
return Object.class; |
374 |
} |
375 |
} |
376 |
|
377 |
/*
|
378 |
* Don't need to implement this method unless your table's editable.
|
379 |
*/
|
380 |
public boolean isCellEditable(int row, int col) { |
381 |
// Note that the data/cell address is constant,
|
382 |
// no matter where the cell appears onscreen.
|
383 |
// if (col > 0) {
|
384 |
return true; |
385 |
} |
386 |
|
387 |
@Override
|
388 |
public Object getValueAt(int row, int column) { |
389 |
if(column == 2) |
390 |
return ((ISymbol)getValueAt(row,0)).getDescription(); |
391 |
|
392 |
return super.getValueAt(row, column); |
393 |
} |
394 |
|
395 |
@Override
|
396 |
public void setValueAt(Object aValue, int row, int column) { |
397 |
|
398 |
if(column == 2){ |
399 |
ISymbol symbol = (ISymbol) getValueAt(row,0);
|
400 |
symbol.setDescription((String) aValue);
|
401 |
setValueAt(symbol,row,0);
|
402 |
} |
403 |
|
404 |
super.setValueAt(aValue, row, column);
|
405 |
} |
406 |
|
407 |
} |
408 |
|
409 |
|
410 |
|
411 |
public void moveDownRows(int startPos, int endPos, int numOfElements) { |
412 |
if(startPos > endPos)
|
413 |
return;
|
414 |
if(startPos >= getRowCount()-1 ) |
415 |
return;
|
416 |
if(startPos == getRowCount()-1) |
417 |
return;
|
418 |
|
419 |
Object[][] values = new Object[getRowCount()][3]; |
420 |
for (int i = 0; i < getRowCount(); i++) { |
421 |
values[i][0] = table.getModel().getValueAt(i,0); |
422 |
values[i][1] = table.getModel().getValueAt(i,1); |
423 |
values[i][2] = table.getModel().getValueAt(i,2); |
424 |
} |
425 |
|
426 |
Object[][]aux = new Object[numOfElements][3]; |
427 |
for (int i = 0; i < numOfElements; i++) { |
428 |
|
429 |
aux[numOfElements - i - 1][0] = values[startPos - i][0]; |
430 |
aux[numOfElements - i - 1][1] = values[startPos - i][1]; |
431 |
aux[numOfElements - i - 1][2] = values[startPos - i][2]; |
432 |
} |
433 |
|
434 |
Object [][] targetVal = {{values[endPos][0],values[endPos][1],values[endPos][2]}}; |
435 |
|
436 |
values[startPos - numOfElements + 1][0] = targetVal[0][0]; |
437 |
values[startPos - numOfElements + 1][1] = targetVal[0][1]; |
438 |
values[startPos - numOfElements + 1][2] = targetVal[0][2]; |
439 |
|
440 |
for (int i = 0; i < numOfElements; i++) { |
441 |
values[endPos - i][0] = aux[numOfElements - i - 1][0]; |
442 |
values[endPos - i][1] = aux[numOfElements - i - 1][1]; |
443 |
values[endPos - i][2] = aux[numOfElements - i - 1][2]; |
444 |
} |
445 |
|
446 |
ISymbol[] symbols = new ISymbol[getRowCount()]; |
447 |
Object[] objects = new Object[getRowCount()]; |
448 |
String[] cads = new String[getRowCount()]; |
449 |
|
450 |
for (int i = 0; i < getRowCount(); i++) { |
451 |
symbols[i] = (ISymbol) values[i][0];
|
452 |
objects[i] = values[i][1];
|
453 |
cads[i] = (String) values[i][2]; |
454 |
} |
455 |
|
456 |
removeAllItems(); |
457 |
fillTableFromSymbolList(symbols,objects,cads); |
458 |
table.addRowSelectionInterval(endPos-numOfElements+1,endPos);
|
459 |
} |
460 |
|
461 |
|
462 |
public void moveUpRows(int startPos, int endPos, int numOfElements) { |
463 |
|
464 |
if(startPos == 0) |
465 |
return;
|
466 |
if(endPos > startPos)
|
467 |
return;
|
468 |
|
469 |
|
470 |
Object[][] values = new Object[getRowCount()][3]; |
471 |
for (int i = 0; i < getRowCount(); i++) { |
472 |
values[i][0] = table.getModel().getValueAt(i,0); |
473 |
values[i][1] = table.getModel().getValueAt(i,1); |
474 |
values[i][2] = table.getModel().getValueAt(i,2); |
475 |
} |
476 |
|
477 |
Object[][]aux = new Object[numOfElements][3]; |
478 |
for (int i = 0; i < numOfElements; i++) { |
479 |
|
480 |
aux[i][0] = values[startPos + i][0]; |
481 |
aux[i][1] = values[startPos + i][1]; |
482 |
aux[i][2] = values[startPos + i][2]; |
483 |
} |
484 |
|
485 |
Object [][] targetVal = {{values[endPos][0],values[endPos][1],values[endPos][2]}}; |
486 |
|
487 |
values[startPos + numOfElements - 1][0] = targetVal[0][0]; |
488 |
values[startPos + numOfElements - 1][1] = targetVal[0][1]; |
489 |
values[startPos + numOfElements - 1][2] = targetVal[0][2]; |
490 |
|
491 |
for (int i = 0; i < numOfElements; i++) { |
492 |
|
493 |
values[endPos + i][0] = aux[i][0]; |
494 |
values[endPos + i][1] = aux[i][1]; |
495 |
values[endPos + i][2] = aux[i][2]; |
496 |
} |
497 |
|
498 |
ISymbol[] symbols = new ISymbol[getRowCount()]; |
499 |
Object[] objects = new Object[getRowCount()]; |
500 |
String[] cads = new String[getRowCount()]; |
501 |
|
502 |
for (int i = 0; i < getRowCount(); i++) { |
503 |
symbols[i] = (ISymbol) values[i][0];
|
504 |
objects[i] = values[i][1];
|
505 |
cads[i] = (String) values[i][2]; |
506 |
} |
507 |
|
508 |
removeAllItems(); |
509 |
fillTableFromSymbolList(symbols,objects,cads); |
510 |
table.addRowSelectionInterval(endPos,endPos+numOfElements-1);
|
511 |
} |
512 |
|
513 |
|
514 |
public int[] getSelectedRows(){ |
515 |
return table.getSelectedRows();
|
516 |
} |
517 |
|
518 |
} |