root / branches / v2_0_0_prep / libraries / libUIComponent / src / org / gvsig / gui / beans / table / TableContainer.java @ 22758
History | View | Annotate | Download (9.76 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.gui.beans.table; |
20 |
|
21 |
import java.awt.BorderLayout; |
22 |
import java.awt.Color; |
23 |
import java.awt.FlowLayout; |
24 |
import java.util.ArrayList; |
25 |
|
26 |
import javax.swing.JPanel; |
27 |
import javax.swing.table.DefaultTableModel; |
28 |
|
29 |
import org.gvsig.gui.beans.table.exceptions.NotInitializeException; |
30 |
import org.gvsig.gui.beans.table.listeners.TableListener; |
31 |
|
32 |
/**
|
33 |
* Contenedor para los componentes de la tabla. Incluye la tabla y el panel de
|
34 |
* control.
|
35 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
36 |
*/
|
37 |
public class TableContainer extends JPanel { |
38 |
private static final long serialVersionUID = 384372026944926838L; |
39 |
|
40 |
private Table table = null; |
41 |
private TableControlerPanel pTableControl = null; |
42 |
private MoveRowsPanel moveRowsPanel = null; |
43 |
private String[] columnNames = null; |
44 |
private int[] columnWidths = null; |
45 |
private TableListener tableListener = null; |
46 |
// Variable que indica si la tabla ha sido inicializada
|
47 |
private boolean initTable = false; |
48 |
private String tableModelClass = "ListModel"; |
49 |
private ArrayList listeners = null; |
50 |
|
51 |
/**
|
52 |
* @param width Ancho de la tabla en pixeles
|
53 |
* @param height Alto de la tabla en pixeles
|
54 |
* @param columnNames Vector de nombres de columna
|
55 |
* @param columnsWidth Vector de anchos para cada columna. Ha de tener el
|
56 |
* mismo n?mero de elementos que columnNames. Si vale null las
|
57 |
* columnas se pondr?n equidistantes.
|
58 |
*/
|
59 |
public TableContainer(String[] columnNames, int[] columnWidths) { |
60 |
this.columnNames = columnNames;
|
61 |
this.columnWidths = columnWidths;
|
62 |
} |
63 |
|
64 |
/**
|
65 |
* @param width Ancho de la tabla en pixeles
|
66 |
* @param height Alto de la tabla en pixeles
|
67 |
* @param columnNames Vector de nombres de columna
|
68 |
* @param columnsWidth Vector de anchos para cada columna. Ha de tener el
|
69 |
* mismo n?mero de elementos que columnNames. Si vale null las
|
70 |
* columnas se pondr?n equidistantes.
|
71 |
* @para listener Liste de eventos para recoger en las tablas. Cada modelo tiene la posibilida de recoger unos listener
|
72 |
* u otros. El usuario le pasar? el listener y el modelo se encargar? de gestionarlos si puede hacerlo. Si no puede no
|
73 |
* har? nada con ellos.
|
74 |
*/
|
75 |
public TableContainer(String[] columnNames, int[] columnWidths, ArrayList listeners) { |
76 |
this.columnNames = columnNames;
|
77 |
this.columnWidths = columnWidths;
|
78 |
this.listeners = listeners;
|
79 |
} |
80 |
|
81 |
/**
|
82 |
* This method initializes this
|
83 |
*/
|
84 |
public void initialize() { |
85 |
initTable = true;
|
86 |
tableListener = new TableListener(this); |
87 |
getTable().getJTable().getSelectionModel().addListSelectionListener(tableListener); |
88 |
|
89 |
this.setLayout(new BorderLayout(5, 5)); |
90 |
this.setBorder(javax.swing.BorderFactory.createEmptyBorder(8, 8, 8, 8)); |
91 |
this.add(getTable(), BorderLayout.CENTER); |
92 |
|
93 |
JPanel panel = new JPanel(); |
94 |
|
95 |
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); |
96 |
panel.add(getPTableControl()); |
97 |
panel.add(getMoveRowsPanel()); |
98 |
this.add(panel, BorderLayout.SOUTH); |
99 |
} |
100 |
|
101 |
/**
|
102 |
* This method initializes jPanel
|
103 |
* @return javax.swing.JPanel
|
104 |
*/
|
105 |
public Table getTable() {
|
106 |
if (table == null) { |
107 |
table = new Table(columnNames, columnWidths, tableModelClass, listeners);
|
108 |
table.setTableContainer(this);
|
109 |
} |
110 |
return table;
|
111 |
} |
112 |
|
113 |
/**
|
114 |
* This method initializes jPanel
|
115 |
* @return javax.swing.JPanel
|
116 |
*/
|
117 |
public TableControlerPanel getPTableControl() {
|
118 |
if (pTableControl == null) { |
119 |
pTableControl = new TableControlerPanel(tableListener);
|
120 |
} |
121 |
return pTableControl;
|
122 |
} |
123 |
|
124 |
/**
|
125 |
* This method initializes jPanel
|
126 |
* @return javax.swing.JPanel
|
127 |
*/
|
128 |
public MoveRowsPanel getMoveRowsPanel() {
|
129 |
if (moveRowsPanel == null) { |
130 |
moveRowsPanel = new MoveRowsPanel(tableListener);
|
131 |
moveRowsPanel.setVisible(false);
|
132 |
} |
133 |
return moveRowsPanel;
|
134 |
} |
135 |
|
136 |
// ********************M?todos de Tabla*****************************//
|
137 |
|
138 |
/**
|
139 |
* A?ade una fila a la tabla.
|
140 |
* @param list Lista de cadenas
|
141 |
*/
|
142 |
public void addRow(Object[] list) throws NotInitializeException { |
143 |
if (!initTable)
|
144 |
throw new NotInitializeException(); |
145 |
|
146 |
TableListener.comboEventEnable = false;
|
147 |
getTable().addRow(list); |
148 |
getPTableControl().addPointToTable((getTable()).getJTable().getRowCount()); |
149 |
setSelectedIndex(getRowCount() - 1);
|
150 |
TableListener.comboEventEnable = true;
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* Elimina una fila de la tabla.
|
155 |
* @param i Fila a eliminar
|
156 |
*/
|
157 |
public void delRow(int i) throws NotInitializeException { |
158 |
if (!initTable)
|
159 |
throw new NotInitializeException(); |
160 |
|
161 |
TableListener.comboEventEnable = false;
|
162 |
try {
|
163 |
setSelectedIndex(getSelectedRow() - 1);
|
164 |
} catch (ArrayIndexOutOfBoundsException ex) { |
165 |
try {
|
166 |
setSelectedIndex(getSelectedRow() + 1);
|
167 |
} catch (ArrayIndexOutOfBoundsException exc) { |
168 |
} |
169 |
} |
170 |
getTable().delRow(i); |
171 |
getPTableControl().setNItems(getRowCount()); |
172 |
TableListener.comboEventEnable = true;
|
173 |
} |
174 |
|
175 |
/**
|
176 |
* Intercambia una fila de la tabla por otra.
|
177 |
* @param i Fila a eliminar
|
178 |
*/
|
179 |
public void swapRow(int i, int j) throws NotInitializeException { |
180 |
if (!initTable)
|
181 |
throw new NotInitializeException(); |
182 |
|
183 |
TableListener.comboEventEnable = false;
|
184 |
getTable().swapRow(i, j); |
185 |
TableListener.comboEventEnable = true;
|
186 |
} |
187 |
|
188 |
/**
|
189 |
* Elimina todas las filas de la tabla.
|
190 |
*/
|
191 |
public void removeAllRows() throws NotInitializeException { |
192 |
if (!initTable)
|
193 |
throw new NotInitializeException(); |
194 |
|
195 |
TableListener.comboEventEnable = false;
|
196 |
getTable().removeAllRows(); |
197 |
getPTableControl().setNItems(0);
|
198 |
TableListener.comboEventEnable = true;
|
199 |
} |
200 |
|
201 |
/**
|
202 |
* Obtiene el n?mero de filas en la tabla
|
203 |
* @return N?mero de filas de la tabla
|
204 |
*/
|
205 |
public int getRowCount() throws NotInitializeException { |
206 |
if (!initTable)
|
207 |
throw new NotInitializeException(); |
208 |
|
209 |
return getTable().getJTable().getRowCount();
|
210 |
} |
211 |
|
212 |
/**
|
213 |
* Selecciona un punto de la lista
|
214 |
* @param i punto a seleccionar
|
215 |
*/
|
216 |
public void setSelectedIndex(int i) throws NotInitializeException { |
217 |
if (!initTable)
|
218 |
throw new NotInitializeException(); |
219 |
|
220 |
TableListener.comboEventEnable = false;
|
221 |
try {
|
222 |
getPTableControl().setSelectedIndex(i); |
223 |
getMoveRowsPanel().setSelectedIndex(i, getRowCount()); |
224 |
getTable().getJTable().setRowSelectionInterval(i, i); |
225 |
} catch (IllegalArgumentException ex) { |
226 |
|
227 |
} |
228 |
TableListener.comboEventEnable = true;
|
229 |
} |
230 |
|
231 |
/**
|
232 |
* Obtiene el punto seleccionado de la lista
|
233 |
* @return Posici?n del punto seleccionado de la tabla
|
234 |
*/
|
235 |
public int getSelectedRow() throws NotInitializeException { |
236 |
if (!initTable)
|
237 |
throw new NotInitializeException(); |
238 |
|
239 |
return getTable().getJTable().getSelectedRow();
|
240 |
} |
241 |
|
242 |
/**
|
243 |
* Obtiene los puntos seleccionados de la lista
|
244 |
* @return Posici?n del punto seleccionado de la tabla
|
245 |
*/
|
246 |
public int[] getSelectedRows() throws NotInitializeException { |
247 |
if (!initTable)
|
248 |
throw new NotInitializeException(); |
249 |
|
250 |
return getTable().getJTable().getSelectedRows();
|
251 |
} |
252 |
|
253 |
/**
|
254 |
* Asigna un valor a una posici?n de la tabla
|
255 |
* @param value Valor
|
256 |
* @param row Fila
|
257 |
* @param col Columna
|
258 |
*/
|
259 |
public void setValueAt(Object value, int row, int col) throws NotInitializeException { |
260 |
if (!initTable)
|
261 |
throw new NotInitializeException(); |
262 |
|
263 |
getTable().getJTable().setValueAt(value, row, col); |
264 |
} |
265 |
|
266 |
/**
|
267 |
* Dice si una tabla es editable o no. Este flag hay que asignarlo antes de la
|
268 |
* inicializaci?n de tabla.
|
269 |
* @param editable
|
270 |
*/
|
271 |
public void setEditable(boolean editable) throws NotInitializeException { |
272 |
if (!initTable)
|
273 |
throw new NotInitializeException(); |
274 |
|
275 |
if (!editable)
|
276 |
getTable().getJTable().setBackground(this.getBackground());
|
277 |
else
|
278 |
getTable().getJTable().setBackground(Color.white);
|
279 |
|
280 |
getTable().getJTable().setEnabled(editable); |
281 |
} |
282 |
|
283 |
/**
|
284 |
* Asigna el modelo de la tabla
|
285 |
* @param model cadena con el nombre del modelo
|
286 |
*/
|
287 |
public void setModel(String model) { |
288 |
tableModelClass = model; |
289 |
} |
290 |
|
291 |
/**
|
292 |
* Obtiene el model de la tabla
|
293 |
* @return
|
294 |
*/
|
295 |
public DefaultTableModel getModel() { |
296 |
return (DefaultTableModel) getTable().getJTable().getModel(); |
297 |
} |
298 |
|
299 |
/**
|
300 |
* Asigna al panel de control de tabla la propiedad de visible/invisible a
|
301 |
* true o false
|
302 |
* @param visible
|
303 |
*/
|
304 |
public void setControlVisible(boolean visible) { |
305 |
getPTableControl().setVisible(visible); |
306 |
} |
307 |
|
308 |
/**
|
309 |
* Asigna al panel de control de tabla la propiedad de visible/invisible a
|
310 |
* true o false
|
311 |
* @param visible
|
312 |
*/
|
313 |
public void setMoveRowsButtonsVisible(boolean visible) { |
314 |
getMoveRowsPanel().setVisible(visible); |
315 |
} |
316 |
|
317 |
/**
|
318 |
* Obtiene el control de tabla
|
319 |
* @return TableControlerPanel
|
320 |
*/
|
321 |
public TableControlerPanel getControl() {
|
322 |
return getPTableControl();
|
323 |
} |
324 |
|
325 |
/**
|
326 |
* Desactiva o activa el evento de nueva linea. Si se desactiva tendr? que ser
|
327 |
* gestionado por el cliente
|
328 |
* @param enabled true para activar y false para desactivar
|
329 |
*/
|
330 |
public void setEnableControlsListener(boolean enabled) { |
331 |
tableListener.enableNewLineListener = enabled; |
332 |
} |
333 |
|
334 |
/**
|
335 |
* Activar o desactivar los componentes del panel
|
336 |
*/
|
337 |
public void setEnabled(boolean enabled) { |
338 |
getTable().getJTable().setEnabled(enabled); |
339 |
|
340 |
if (!enabled)
|
341 |
getPTableControl().disableAllControls(); |
342 |
else
|
343 |
getPTableControl().restoreControlsValue(); |
344 |
} |
345 |
} |