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