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