root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / table / TableContainer.java @ 13414
History | View | Annotate | Download (8.43 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.GridBagConstraints; |
24 |
|
25 |
import javax.swing.JPanel; |
26 |
import javax.swing.table.DefaultTableModel; |
27 |
|
28 |
import org.gvsig.gui.beans.table.exceptions.NotInitializeException; |
29 |
import org.gvsig.gui.beans.table.listeners.TableListener; |
30 |
/**
|
31 |
* Contenedor para los componentes de la tabla. Incluye la tabla y el panel
|
32 |
* de control.
|
33 |
*
|
34 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
35 |
*/
|
36 |
public class TableContainer extends JPanel { |
37 |
private static final long serialVersionUID = 384372026944926838L; |
38 |
private static final int INTERNAL_MARGIN = 10; |
39 |
private int PHEIGHT = 120; |
40 |
|
41 |
private Table table = null; |
42 |
private TableControlerPanel pTableControl = 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 |
|
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 mismo n?mero de elementos que columnNames.
|
56 |
* Si vale null las columnas se pondr?n equidistantes.
|
57 |
*/
|
58 |
public TableContainer(String[] columnNames, int[] columnWidths) { |
59 |
this.columnNames = columnNames;
|
60 |
this.columnWidths = columnWidths;
|
61 |
} |
62 |
|
63 |
/**
|
64 |
* This method initializes this
|
65 |
*
|
66 |
*/
|
67 |
public void initialize() { |
68 |
initTable = true;
|
69 |
tableListener = new TableListener(this); |
70 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
71 |
gridBagConstraints.gridx = 0;
|
72 |
gridBagConstraints.gridy = 0;
|
73 |
gridBagConstraints.insets = new java.awt.Insets(0,0,(INTERNAL_MARGIN / 2),0); |
74 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
75 |
gridBagConstraints1.gridx = 0;
|
76 |
gridBagConstraints1.gridy = 1;
|
77 |
gridBagConstraints1.insets = new java.awt.Insets(0,0,0,0); |
78 |
this.setPreferredSize(new java.awt.Dimension(0, PHEIGHT)); |
79 |
this.setLayout(new BorderLayout(5, 5)); |
80 |
this.setBorder(javax.swing.BorderFactory.createEmptyBorder(8, 8, 8, 8)); |
81 |
this.add(getTable(), BorderLayout.CENTER); |
82 |
this.add(getPTableControl(), BorderLayout.SOUTH); |
83 |
} |
84 |
|
85 |
/**
|
86 |
* This method initializes jPanel
|
87 |
*
|
88 |
* @return javax.swing.JPanel
|
89 |
*/
|
90 |
public Table getTable() {
|
91 |
if (table == null) { |
92 |
table = new Table(columnNames, columnWidths, tableModelClass);
|
93 |
table.setTableContainer(this);
|
94 |
} |
95 |
return table;
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* This method initializes jPanel
|
100 |
*
|
101 |
* @return javax.swing.JPanel
|
102 |
*/
|
103 |
public TableControlerPanel getPTableControl() {
|
104 |
if (pTableControl == null) { |
105 |
pTableControl = new TableControlerPanel(tableListener);
|
106 |
getTable().getJTable().getSelectionModel().addListSelectionListener(tableListener); |
107 |
} |
108 |
return pTableControl;
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* This method initializes jPanel
|
113 |
*
|
114 |
* @return javax.swing.JPanel
|
115 |
*/
|
116 |
|
117 |
|
118 |
//********************M?todos de Tabla*****************************//
|
119 |
|
120 |
/**
|
121 |
* A?ade una fila a la tabla.
|
122 |
* @param list Lista de cadenas
|
123 |
*/
|
124 |
public void addRow(Object[] list)throws NotInitializeException{ |
125 |
if(!initTable)
|
126 |
throw new NotInitializeException(); |
127 |
|
128 |
TableListener.comboEventEnable = false;
|
129 |
getTable().addRow(list); |
130 |
getPTableControl().addPointToTable((getTable()).getJTable().getRowCount()); |
131 |
setSelectedIndex(getRowCount() - 1);
|
132 |
TableListener.comboEventEnable = true;
|
133 |
} |
134 |
|
135 |
/**
|
136 |
* Elimina una fila de la tabla.
|
137 |
* @param i Fila a eliminar
|
138 |
*/
|
139 |
public void delRow(int i)throws NotInitializeException{ |
140 |
if(!initTable)
|
141 |
throw new NotInitializeException(); |
142 |
|
143 |
TableListener.comboEventEnable = false;
|
144 |
try{
|
145 |
setSelectedIndex(getSelectedRow() - 1);
|
146 |
}catch(ArrayIndexOutOfBoundsException ex){ |
147 |
try{
|
148 |
setSelectedIndex(getSelectedRow() + 1);
|
149 |
}catch(ArrayIndexOutOfBoundsException exc){} |
150 |
} |
151 |
getTable().delRow(i); |
152 |
getPTableControl().setNItems(getRowCount()); |
153 |
TableListener.comboEventEnable = true;
|
154 |
} |
155 |
|
156 |
/**
|
157 |
* Elimina todas las filas de la tabla.
|
158 |
*/
|
159 |
public void removeAllRows()throws NotInitializeException{ |
160 |
if (!initTable)
|
161 |
throw new NotInitializeException(); |
162 |
|
163 |
TableListener.comboEventEnable = false;
|
164 |
getTable().removeAllRows(); |
165 |
getPTableControl().setNItems(0);
|
166 |
TableListener.comboEventEnable = true;
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Obtiene el n?mero de filas en la tabla
|
171 |
* @return N?mero de filas de la tabla
|
172 |
*/
|
173 |
public int getRowCount()throws NotInitializeException{ |
174 |
if(!initTable)
|
175 |
throw new NotInitializeException(); |
176 |
|
177 |
return getTable().getJTable().getRowCount();
|
178 |
} |
179 |
|
180 |
/**
|
181 |
* Selecciona un punto de la lista
|
182 |
* @param i punto a seleccionar
|
183 |
*/
|
184 |
public void setSelectedIndex(int i)throws NotInitializeException{ |
185 |
if (!initTable)
|
186 |
throw new NotInitializeException(); |
187 |
|
188 |
TableListener.comboEventEnable = false;
|
189 |
try {
|
190 |
getPTableControl().setSelectedIndex(i); |
191 |
getTable().getJTable().setRowSelectionInterval(i, i); |
192 |
} catch (IllegalArgumentException ex) { |
193 |
|
194 |
} |
195 |
TableListener.comboEventEnable = true;
|
196 |
} |
197 |
|
198 |
/**
|
199 |
* Obtiene el punto seleccionado de la lista
|
200 |
* @return Posici?n del punto seleccionado de la tabla
|
201 |
*/
|
202 |
public int getSelectedRow()throws NotInitializeException{ |
203 |
if (!initTable)
|
204 |
throw new NotInitializeException(); |
205 |
|
206 |
return getTable().getJTable().getSelectedRow();
|
207 |
} |
208 |
|
209 |
/**
|
210 |
* Obtiene los puntos seleccionados de la lista
|
211 |
* @return Posici?n del punto seleccionado de la tabla
|
212 |
*/
|
213 |
public int[] getSelectedRows()throws NotInitializeException{ |
214 |
if (!initTable)
|
215 |
throw new NotInitializeException(); |
216 |
|
217 |
return getTable().getJTable().getSelectedRows();
|
218 |
} |
219 |
/**
|
220 |
* Asigna un valor a una posici?n de la tabla
|
221 |
* @param value Valor
|
222 |
* @param row Fila
|
223 |
* @param col Columna
|
224 |
*/
|
225 |
public void setValueAt(Object value, int row, int col)throws NotInitializeException{ |
226 |
if(!initTable)
|
227 |
throw new NotInitializeException(); |
228 |
|
229 |
getTable().getJTable().setValueAt(value, row, col); |
230 |
} |
231 |
|
232 |
/**
|
233 |
* Dice si una tabla es editable o no.
|
234 |
* Este flag hay que asignarlo antes de la inicializaci?n de tabla.
|
235 |
* @param editable
|
236 |
*/
|
237 |
public void setEditable(boolean editable)throws NotInitializeException{ |
238 |
if(!initTable)
|
239 |
throw new NotInitializeException(); |
240 |
|
241 |
if(!editable)
|
242 |
getTable().getJTable().setBackground(this.getBackground());
|
243 |
else
|
244 |
getTable().getJTable().setBackground(Color.white);
|
245 |
|
246 |
getTable().getJTable().setEnabled(editable); |
247 |
} |
248 |
|
249 |
/**
|
250 |
* Asigna el modelo de la tabla
|
251 |
* @param model cadena con el nombre del modelo
|
252 |
*/
|
253 |
public void setModel(String model){ |
254 |
tableModelClass = model; |
255 |
} |
256 |
|
257 |
/**
|
258 |
* Obtiene el model de la tabla
|
259 |
* @return
|
260 |
*/
|
261 |
public DefaultTableModel getModel(){ |
262 |
return (DefaultTableModel) getTable().getJTable().getModel(); |
263 |
} |
264 |
|
265 |
/**
|
266 |
* Asigna al panel de control de tabla la propiedad de visible/invisible a true o false
|
267 |
* @param visible
|
268 |
*/
|
269 |
public void setControlVisible(boolean visible) { |
270 |
getPTableControl().setVisible(visible); |
271 |
} |
272 |
|
273 |
/**
|
274 |
* Obtiene el control de tabla
|
275 |
* @return TableControlerPanel
|
276 |
*/
|
277 |
public TableControlerPanel getControl(){
|
278 |
return getPTableControl();
|
279 |
} |
280 |
|
281 |
/**
|
282 |
* Desactiva o activa el evento de nueva linea. Si se desactiva tendr? que ser
|
283 |
* gestionado por el cliente
|
284 |
* @param enabled true para activar y false para desactivar
|
285 |
*/
|
286 |
public void setEnableControlsListener(boolean enabled){ |
287 |
tableListener.enableNewLineListener = enabled; |
288 |
} |
289 |
|
290 |
/**
|
291 |
* Activar o desactivar los componentes del panel
|
292 |
*/
|
293 |
public void setEnabled(boolean enabled){ |
294 |
getTable().getJTable().setEnabled(enabled); |
295 |
if(!enabled)
|
296 |
getPTableControl().disableAllControls(); |
297 |
else
|
298 |
getPTableControl().restoreControlsValue(); |
299 |
} |
300 |
} |