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 | 10741 | nacho | /* 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 | 10779 | bsanchez | import java.awt.BorderLayout; |
22 | 10741 | nacho | import java.awt.Color; |
23 | 15679 | bsanchez | import java.awt.FlowLayout; |
24 | 18146 | nbrodin | import java.util.ArrayList; |
25 | 10741 | nacho | |
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 | 15679 | bsanchez | |
32 | 10741 | nacho | /**
|
33 | 15679 | bsanchez | * Contenedor para los componentes de la tabla. Incluye la tabla y el panel de
|
34 | * control.
|
||
35 | 12368 | bsanchez | * @author Nacho Brodin (brodin_ign@gva.es)
|
36 | 10741 | nacho | */
|
37 | 15679 | bsanchez | public class TableContainer extends JPanel { |
38 | 12368 | bsanchez | private static final long serialVersionUID = 384372026944926838L; |
39 | 12138 | bsanchez | |
40 | 12514 | bsanchez | private Table table = null; |
41 | 15679 | bsanchez | 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 | 12368 | bsanchez | // Variable que indica si la tabla ha sido inicializada
|
47 | 15679 | bsanchez | private boolean initTable = false; |
48 | private String tableModelClass = "ListModel"; |
||
49 | 18146 | nbrodin | private ArrayList listeners = null; |
50 | 12138 | bsanchez | |
51 | 15679 | bsanchez | /**
|
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 | 10779 | bsanchez | public TableContainer(String[] columnNames, int[] columnWidths) { |
60 | 10741 | nacho | this.columnNames = columnNames;
|
61 | this.columnWidths = columnWidths;
|
||
62 | } |
||
63 | 18146 | nbrodin | |
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 | 12138 | bsanchez | |
81 | 10741 | nacho | /**
|
82 | * This method initializes this
|
||
83 | */
|
||
84 | public void initialize() { |
||
85 | 10779 | bsanchez | initTable = true;
|
86 | 13414 | bsanchez | tableListener = new TableListener(this); |
87 | 15679 | bsanchez | getTable().getJTable().getSelectionModel().addListSelectionListener(tableListener); |
88 | |||
89 | 10779 | bsanchez | this.setLayout(new BorderLayout(5, 5)); |
90 | 12592 | bsanchez | this.setBorder(javax.swing.BorderFactory.createEmptyBorder(8, 8, 8, 8)); |
91 | 12514 | bsanchez | this.add(getTable(), BorderLayout.CENTER); |
92 | 15679 | bsanchez | |
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 | 10741 | nacho | } |
100 | 12138 | bsanchez | |
101 | 10741 | nacho | /**
|
102 | 12368 | bsanchez | * This method initializes jPanel
|
103 | * @return javax.swing.JPanel
|
||
104 | 10741 | nacho | */
|
105 | 12514 | bsanchez | public Table getTable() {
|
106 | if (table == null) { |
||
107 | 18146 | nbrodin | table = new Table(columnNames, columnWidths, tableModelClass, listeners);
|
108 | 12514 | bsanchez | table.setTableContainer(this);
|
109 | 10741 | nacho | } |
110 | 12514 | bsanchez | return table;
|
111 | 10741 | nacho | } |
112 | |||
113 | /**
|
||
114 | 12368 | bsanchez | * This method initializes jPanel
|
115 | * @return javax.swing.JPanel
|
||
116 | 10741 | nacho | */
|
117 | public TableControlerPanel getPTableControl() {
|
||
118 | if (pTableControl == null) { |
||
119 | pTableControl = new TableControlerPanel(tableListener);
|
||
120 | } |
||
121 | return pTableControl;
|
||
122 | } |
||
123 | |||
124 | /**
|
||
125 | 12368 | bsanchez | * This method initializes jPanel
|
126 | * @return javax.swing.JPanel
|
||
127 | 10741 | nacho | */
|
128 | 15679 | bsanchez | public MoveRowsPanel getMoveRowsPanel() {
|
129 | if (moveRowsPanel == null) { |
||
130 | moveRowsPanel = new MoveRowsPanel(tableListener);
|
||
131 | moveRowsPanel.setVisible(false);
|
||
132 | } |
||
133 | return moveRowsPanel;
|
||
134 | } |
||
135 | 10779 | bsanchez | |
136 | 15679 | bsanchez | // ********************M?todos de Tabla*****************************//
|
137 | 12138 | bsanchez | |
138 | 10741 | nacho | /**
|
139 | * A?ade una fila a la tabla.
|
||
140 | * @param list Lista de cadenas
|
||
141 | */
|
||
142 | 15679 | bsanchez | public void addRow(Object[] list) throws NotInitializeException { |
143 | 14257 | bsanchez | if (!initTable)
|
144 | 10741 | nacho | throw new NotInitializeException(); |
145 | 12138 | bsanchez | |
146 | 10741 | nacho | TableListener.comboEventEnable = false;
|
147 | 12514 | bsanchez | getTable().addRow(list); |
148 | 12592 | bsanchez | getPTableControl().addPointToTable((getTable()).getJTable().getRowCount()); |
149 | 10741 | nacho | setSelectedIndex(getRowCount() - 1);
|
150 | TableListener.comboEventEnable = true;
|
||
151 | } |
||
152 | 12138 | bsanchez | |
153 | 10741 | nacho | /**
|
154 | * Elimina una fila de la tabla.
|
||
155 | * @param i Fila a eliminar
|
||
156 | */
|
||
157 | 15679 | bsanchez | public void delRow(int i) throws NotInitializeException { |
158 | if (!initTable)
|
||
159 | 10741 | nacho | throw new NotInitializeException(); |
160 | 12138 | bsanchez | |
161 | 10741 | nacho | TableListener.comboEventEnable = false;
|
162 | 15679 | bsanchez | try {
|
163 | 12553 | bsanchez | setSelectedIndex(getSelectedRow() - 1);
|
164 | 15679 | bsanchez | } catch (ArrayIndexOutOfBoundsException ex) { |
165 | try {
|
||
166 | 12553 | bsanchez | setSelectedIndex(getSelectedRow() + 1);
|
167 | 15679 | bsanchez | } catch (ArrayIndexOutOfBoundsException exc) { |
168 | } |
||
169 | 10741 | nacho | } |
170 | 12514 | bsanchez | getTable().delRow(i); |
171 | 12592 | bsanchez | getPTableControl().setNItems(getRowCount()); |
172 | 10741 | nacho | TableListener.comboEventEnable = true;
|
173 | } |
||
174 | |||
175 | /**
|
||
176 | 15679 | bsanchez | * 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 | 10741 | nacho | * Elimina todas las filas de la tabla.
|
190 | */
|
||
191 | 15679 | bsanchez | public void removeAllRows() throws NotInitializeException { |
192 | 12553 | bsanchez | if (!initTable)
|
193 | 10741 | nacho | throw new NotInitializeException(); |
194 | 12138 | bsanchez | |
195 | 10741 | nacho | TableListener.comboEventEnable = false;
|
196 | 12514 | bsanchez | getTable().removeAllRows(); |
197 | 12592 | bsanchez | getPTableControl().setNItems(0);
|
198 | 10741 | nacho | TableListener.comboEventEnable = true;
|
199 | } |
||
200 | 12138 | bsanchez | |
201 | 10741 | nacho | /**
|
202 | * Obtiene el n?mero de filas en la tabla
|
||
203 | * @return N?mero de filas de la tabla
|
||
204 | */
|
||
205 | 15679 | bsanchez | public int getRowCount() throws NotInitializeException { |
206 | if (!initTable)
|
||
207 | 10741 | nacho | throw new NotInitializeException(); |
208 | 12138 | bsanchez | |
209 | 12514 | bsanchez | return getTable().getJTable().getRowCount();
|
210 | 10741 | nacho | } |
211 | 12138 | bsanchez | |
212 | 10741 | nacho | /**
|
213 | * Selecciona un punto de la lista
|
||
214 | * @param i punto a seleccionar
|
||
215 | */
|
||
216 | 15679 | bsanchez | public void setSelectedIndex(int i) throws NotInitializeException { |
217 | 13414 | bsanchez | if (!initTable)
|
218 | 10741 | nacho | throw new NotInitializeException(); |
219 | 12138 | bsanchez | |
220 | 10741 | nacho | TableListener.comboEventEnable = false;
|
221 | 13414 | bsanchez | try {
|
222 | 12592 | bsanchez | getPTableControl().setSelectedIndex(i); |
223 | 15679 | bsanchez | getMoveRowsPanel().setSelectedIndex(i, getRowCount()); |
224 | 12514 | bsanchez | getTable().getJTable().setRowSelectionInterval(i, i); |
225 | 13414 | bsanchez | } catch (IllegalArgumentException ex) { |
226 | 12138 | bsanchez | |
227 | 10741 | nacho | } |
228 | TableListener.comboEventEnable = true;
|
||
229 | } |
||
230 | 12138 | bsanchez | |
231 | 10741 | nacho | /**
|
232 | * Obtiene el punto seleccionado de la lista
|
||
233 | * @return Posici?n del punto seleccionado de la tabla
|
||
234 | */
|
||
235 | 15679 | bsanchez | public int getSelectedRow() throws NotInitializeException { |
236 | 12553 | bsanchez | if (!initTable)
|
237 | 10741 | nacho | throw new NotInitializeException(); |
238 | 12138 | bsanchez | |
239 | 12514 | bsanchez | return getTable().getJTable().getSelectedRow();
|
240 | 10741 | nacho | } |
241 | |||
242 | /**
|
||
243 | 12553 | bsanchez | * Obtiene los puntos seleccionados de la lista
|
244 | * @return Posici?n del punto seleccionado de la tabla
|
||
245 | */
|
||
246 | 15679 | bsanchez | public int[] getSelectedRows() throws NotInitializeException { |
247 | 12553 | bsanchez | if (!initTable)
|
248 | throw new NotInitializeException(); |
||
249 | |||
250 | return getTable().getJTable().getSelectedRows();
|
||
251 | } |
||
252 | 15679 | bsanchez | |
253 | 12553 | bsanchez | /**
|
254 | 10741 | nacho | * Asigna un valor a una posici?n de la tabla
|
255 | * @param value Valor
|
||
256 | * @param row Fila
|
||
257 | * @param col Columna
|
||
258 | */
|
||
259 | 15679 | bsanchez | public void setValueAt(Object value, int row, int col) throws NotInitializeException { |
260 | if (!initTable)
|
||
261 | 10741 | nacho | throw new NotInitializeException(); |
262 | 12138 | bsanchez | |
263 | 12514 | bsanchez | getTable().getJTable().setValueAt(value, row, col); |
264 | 10741 | nacho | } |
265 | 12138 | bsanchez | |
266 | 10741 | nacho | /**
|
267 | 15679 | bsanchez | * Dice si una tabla es editable o no. Este flag hay que asignarlo antes de la
|
268 | * inicializaci?n de tabla.
|
||
269 | 10741 | nacho | * @param editable
|
270 | */
|
||
271 | 15679 | bsanchez | public void setEditable(boolean editable) throws NotInitializeException { |
272 | if (!initTable)
|
||
273 | 10741 | nacho | throw new NotInitializeException(); |
274 | 12138 | bsanchez | |
275 | 15679 | bsanchez | if (!editable)
|
276 | 12514 | bsanchez | getTable().getJTable().setBackground(this.getBackground());
|
277 | 10741 | nacho | else
|
278 | 12514 | bsanchez | getTable().getJTable().setBackground(Color.white);
|
279 | 12138 | bsanchez | |
280 | 12514 | bsanchez | getTable().getJTable().setEnabled(editable); |
281 | 10741 | nacho | } |
282 | 12138 | bsanchez | |
283 | 10741 | nacho | /**
|
284 | * Asigna el modelo de la tabla
|
||
285 | * @param model cadena con el nombre del modelo
|
||
286 | */
|
||
287 | 15679 | bsanchez | public void setModel(String model) { |
288 | 10741 | nacho | tableModelClass = model; |
289 | } |
||
290 | 12138 | bsanchez | |
291 | 10741 | nacho | /**
|
292 | * Obtiene el model de la tabla
|
||
293 | * @return
|
||
294 | */
|
||
295 | 15679 | bsanchez | public DefaultTableModel getModel() { |
296 | 12514 | bsanchez | return (DefaultTableModel) getTable().getJTable().getModel(); |
297 | 10741 | nacho | } |
298 | |||
299 | /**
|
||
300 | 15679 | bsanchez | * Asigna al panel de control de tabla la propiedad de visible/invisible a
|
301 | * true o false
|
||
302 | 10741 | nacho | * @param visible
|
303 | */
|
||
304 | 10779 | bsanchez | public void setControlVisible(boolean visible) { |
305 | 10741 | nacho | getPTableControl().setVisible(visible); |
306 | } |
||
307 | 15679 | bsanchez | |
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 | 12138 | bsanchez | |
317 | 10741 | nacho | /**
|
318 | * Obtiene el control de tabla
|
||
319 | * @return TableControlerPanel
|
||
320 | */
|
||
321 | 15679 | bsanchez | public TableControlerPanel getControl() {
|
322 | 12592 | bsanchez | return getPTableControl();
|
323 | 10741 | nacho | } |
324 | 12138 | bsanchez | |
325 | 10741 | nacho | /**
|
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 | 15679 | bsanchez | public void setEnableControlsListener(boolean enabled) { |
331 | 10741 | nacho | tableListener.enableNewLineListener = enabled; |
332 | } |
||
333 | 12138 | bsanchez | |
334 | 10741 | nacho | /**
|
335 | * Activar o desactivar los componentes del panel
|
||
336 | */
|
||
337 | 15679 | bsanchez | public void setEnabled(boolean enabled) { |
338 | 12514 | bsanchez | getTable().getJTable().setEnabled(enabled); |
339 | 15679 | bsanchez | |
340 | if (!enabled)
|
||
341 | 10741 | nacho | getPTableControl().disableAllControls(); |
342 | else
|
||
343 | getPTableControl().restoreControlsValue(); |
||
344 | } |
||
345 | 15679 | bsanchez | } |