root / trunk / libraries / libUIComponent_praster / src / org / gvsig / gui / beans / table / Table.java @ 8026
History | View | Annotate | Download (6.45 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.Dimension; |
22 |
import java.awt.FlowLayout; |
23 |
import java.awt.event.MouseAdapter; |
24 |
import java.awt.event.MouseEvent; |
25 |
|
26 |
import javax.swing.JScrollPane; |
27 |
import javax.swing.JTable; |
28 |
import javax.swing.table.DefaultTableModel; |
29 |
import javax.swing.table.TableColumn; |
30 |
|
31 |
import org.gvsig.gui.beans.BaseComponent; |
32 |
import org.gvsig.gui.beans.table.exceptions.NotInitializeException; |
33 |
import org.gvsig.gui.beans.table.listeners.TableListener; |
34 |
import org.gvsig.gui.beans.table.models.ModelLoader; |
35 |
|
36 |
/**
|
37 |
* Componente tabla
|
38 |
*
|
39 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
40 |
*
|
41 |
*/
|
42 |
public class Table extends BaseComponent { |
43 |
private static final int INTERNAL_MARGIN = 4; |
44 |
|
45 |
private JTable table = null; |
46 |
private JScrollPane scrollPanel = null; |
47 |
private DefaultTableModel tableModel = null; |
48 |
private int width, height; |
49 |
private String[] columnNames = null; |
50 |
private int[] columnWidths = null; |
51 |
private TableListener tableListener = null; |
52 |
private TableContainer tableContainer = null; |
53 |
public String tableModelClass = null; |
54 |
|
55 |
|
56 |
/**
|
57 |
*
|
58 |
* @param width Ancho de la tabla en pixeles
|
59 |
* @param height Alto de la tabla en pixeles
|
60 |
* @param columnNames Vector de nombres de columna
|
61 |
* @param columnsWidth Vector de anchos para cada columna. Ha de tener el mismo n?mero de elementos que columnNames.
|
62 |
* Si vale null las columnas se pondr?n equidistantes.
|
63 |
* @param tableListener Listener de la tabla
|
64 |
*/
|
65 |
public Table(int width, int height, String[] columnNames, int[] columnWidths, TableListener tableListener, String tableModelClass){ |
66 |
this.width = width;
|
67 |
this.height = height;
|
68 |
this.columnNames = columnNames;
|
69 |
this.columnWidths = columnWidths;
|
70 |
this.tableListener = tableListener;
|
71 |
this.tableModelClass = tableModelClass;
|
72 |
initialize(); |
73 |
} |
74 |
|
75 |
/**
|
76 |
* This method initializes this
|
77 |
*
|
78 |
*/
|
79 |
private void initialize() { |
80 |
this.setSize(new java.awt.Dimension(width, height)); |
81 |
FlowLayout flowLayout = new FlowLayout(); |
82 |
flowLayout.setHgap(0);
|
83 |
flowLayout.setVgap(0);
|
84 |
this.setLayout(flowLayout);
|
85 |
this.add(getJScrollPane(), null); |
86 |
} |
87 |
|
88 |
/**
|
89 |
* Asigna el tama?o del componente
|
90 |
* @param w Nuevo ancho del componente padre
|
91 |
* @param h Nuevo alto del componente padre
|
92 |
*/
|
93 |
public void setComponentSize(int w, int h){ |
94 |
this.setSize(new java.awt.Dimension(w, h)); |
95 |
scrollPanel.setSize(w, h); |
96 |
//this.setPreferredSize(new java.awt.Dimension(w, h));
|
97 |
scrollPanel.setPreferredSize(new Dimension(w, h)); |
98 |
} |
99 |
|
100 |
|
101 |
/**
|
102 |
* Obtiene la Tabla
|
103 |
* @return Tabla de bandas de la imagen
|
104 |
*/
|
105 |
public JTable getTable() { |
106 |
if (table == null) { |
107 |
//tableModel = new ListModel(columnNames);
|
108 |
ModelLoader loader = new ModelLoader();
|
109 |
table = loader.load(tableModelClass, columnNames); |
110 |
tableModel = loader.getTableModel(); |
111 |
tableModel.addTableModelListener(tableListener); |
112 |
table.setPreferredScrollableViewportSize(new Dimension(width, height)); |
113 |
table.addMouseListener(new MouseAdapter() { |
114 |
public void mouseClicked(MouseEvent e) { |
115 |
try{
|
116 |
if(tableContainer != null) |
117 |
tableContainer.setSelectedIndex(table.getSelectedRow()); |
118 |
}catch(NotInitializeException ex){}
|
119 |
} |
120 |
}); |
121 |
|
122 |
TableColumn column = null; |
123 |
|
124 |
int widthPerColumn = (int)(width / columnNames.length); |
125 |
for (int i = 0; i < columnNames.length; i++) { |
126 |
column = table.getColumnModel().getColumn(i); |
127 |
column.setResizable(true);
|
128 |
if(columnWidths == null) |
129 |
column.setPreferredWidth(widthPerColumn); |
130 |
else
|
131 |
column.setPreferredWidth(columnWidths[i]); |
132 |
} |
133 |
} |
134 |
|
135 |
return table;
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* This method initializes jScrollPane
|
140 |
*
|
141 |
* @return javax.swing.JPanel
|
142 |
*/
|
143 |
private JScrollPane getJScrollPane() { |
144 |
if (scrollPanel == null) { |
145 |
scrollPanel = new JScrollPane(getTable()); |
146 |
scrollPanel.setSize(width, height); |
147 |
scrollPanel.setPreferredSize(new Dimension(width , height)); |
148 |
} |
149 |
return scrollPanel;
|
150 |
} |
151 |
|
152 |
/**
|
153 |
* Obtiene el contenedor padre de la tabla
|
154 |
* @return
|
155 |
*/
|
156 |
public TableContainer getTableContainer() {
|
157 |
return tableContainer;
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* Asigna el contenedor padre de la tabla
|
162 |
* @param tableContainer
|
163 |
*/
|
164 |
public void setTableContainer(TableContainer tableContainer) { |
165 |
this.tableContainer = tableContainer;
|
166 |
} |
167 |
|
168 |
/**
|
169 |
* Obtiene el modelo de la tabla
|
170 |
* @return
|
171 |
*/
|
172 |
public DefaultTableModel getTableModel() { |
173 |
return tableModel;
|
174 |
} |
175 |
|
176 |
//********************M?todos de Tabla*****************************//
|
177 |
|
178 |
/**
|
179 |
* A?ade una fila a la tabla.
|
180 |
* @param list Lista de cadenas
|
181 |
*/
|
182 |
public void addRow(Object[] list){ |
183 |
((DefaultTableModel)this.getTable().getModel()).addRow(list); |
184 |
/*try{
|
185 |
for(int i = 0; i < list.length; i++)
|
186 |
((DefaultTableModel)this.getTable().getModel()).setValueAt(list[i],
|
187 |
((DefaultTableModel)this.getTable().getModel()).getRowCount() - 1,
|
188 |
i);
|
189 |
}catch(ArrayIndexOutOfBoundsException exc){
|
190 |
//Se intenta acceder a una columna que no existe. Terminamos el bucle.
|
191 |
}*/
|
192 |
} |
193 |
|
194 |
/**
|
195 |
* Elimina una fila de la tabla.
|
196 |
* @param i Fila a eliminar
|
197 |
*/
|
198 |
public void delRow(int i){ |
199 |
((DefaultTableModel)this.getTable().getModel()).removeRow(i); |
200 |
} |
201 |
|
202 |
/**
|
203 |
* Elimina todas las filas de la tabla.
|
204 |
*/
|
205 |
public void removeAllRows(){ |
206 |
int nRows = ((DefaultTableModel)this.getTable().getModel()).getRowCount(); |
207 |
for(int i = 0; i < nRows; i++) |
208 |
((DefaultTableModel)this.getTable().getModel()).removeRow(0); |
209 |
} |
210 |
|
211 |
|
212 |
} |