svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / table / Table.java @ 40561
History | View | Annotate | Download (5.63 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.util.ArrayList; |
28 |
|
29 |
import javax.swing.JPanel; |
30 |
import javax.swing.JScrollPane; |
31 |
import javax.swing.JTable; |
32 |
import javax.swing.table.DefaultTableModel; |
33 |
import javax.swing.table.TableColumn; |
34 |
|
35 |
import org.gvsig.gui.beans.table.models.ModelLoader; |
36 |
/**
|
37 |
* Componente tabla
|
38 |
*
|
39 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
40 |
*/
|
41 |
public class Table extends JPanel { |
42 |
private static final long serialVersionUID = -4375500244443538451L; |
43 |
private JTable jTable = null; |
44 |
private JScrollPane scrollPanel = null; |
45 |
private DefaultTableModel tableModel = null; |
46 |
private String[] columnNames = null; |
47 |
private int[] columnWidths = null; |
48 |
private TableContainer tableContainer = null; |
49 |
public String tableModelClass = null; |
50 |
private ArrayList listeners = null; |
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 Table(String[] columnNames, int[] columnWidths, String tableModelClass){ |
61 |
this.columnNames = columnNames;
|
62 |
this.columnWidths = columnWidths;
|
63 |
this.tableModelClass = tableModelClass;
|
64 |
initialize(); |
65 |
} |
66 |
|
67 |
/**
|
68 |
*
|
69 |
* @param width Ancho de la tabla en pixeles
|
70 |
* @param height Alto de la tabla en pixeles
|
71 |
* @param columnNames Vector de nombres de columna
|
72 |
* @param columnsWidth Vector de anchos para cada columna. Ha de tener el mismo n?mero de elementos que columnNames.
|
73 |
* @para listener Liste de eventos para recoger en las tablas. Cada modelo tiene la posibilida de recoger unos listener
|
74 |
* u otros. El usuario le pasar? el listener y el modelo se encargar? de gestionarlos si puede hacerlo. Si no puede no
|
75 |
* har? nada con ellos.
|
76 |
* Si vale null las columnas se pondr?n equidistantes.
|
77 |
*/
|
78 |
public Table(String[] columnNames, int[] columnWidths, String tableModelClass, ArrayList listeners){ |
79 |
this.columnNames = columnNames;
|
80 |
this.columnWidths = columnWidths;
|
81 |
this.tableModelClass = tableModelClass;
|
82 |
this.listeners = listeners;
|
83 |
initialize(); |
84 |
} |
85 |
|
86 |
|
87 |
/**
|
88 |
* This method initializes this
|
89 |
*/
|
90 |
private void initialize() { |
91 |
this.setLayout(new BorderLayout(5, 5)); |
92 |
this.add(getJScrollPane(), BorderLayout.CENTER); |
93 |
} |
94 |
|
95 |
/**
|
96 |
* Obtiene la Tabla
|
97 |
* @return Tabla de bandas de la imagen
|
98 |
*/
|
99 |
public JTable getJTable() { |
100 |
if (jTable == null) { |
101 |
// tableModel = new ListModel(columnNames);
|
102 |
ModelLoader loader = new ModelLoader();
|
103 |
jTable = loader.load(tableModelClass, columnNames, listeners); |
104 |
tableModel = loader.getTableModel(); |
105 |
|
106 |
TableColumn column = null; |
107 |
|
108 |
int widthPerColumn = (int) (this.getWidth() / columnNames.length); |
109 |
for (int i = 0; i < columnNames.length; i++) { |
110 |
column = jTable.getColumnModel().getColumn(i); |
111 |
column.setResizable(true);
|
112 |
if (columnWidths == null) |
113 |
column.setPreferredWidth(widthPerColumn); |
114 |
else
|
115 |
column.setPreferredWidth(columnWidths[i]); |
116 |
} |
117 |
} |
118 |
|
119 |
return jTable;
|
120 |
} |
121 |
|
122 |
/**
|
123 |
* This method initializes jScrollPane
|
124 |
* @return javax.swing.JPanel
|
125 |
*/
|
126 |
private JScrollPane getJScrollPane() { |
127 |
if (scrollPanel == null) { |
128 |
scrollPanel = new JScrollPane(getJTable()); |
129 |
} |
130 |
return scrollPanel;
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* Obtiene el contenedor padre de la tabla
|
135 |
* @return
|
136 |
*/
|
137 |
public TableContainer getTableContainer() {
|
138 |
return tableContainer;
|
139 |
} |
140 |
|
141 |
/**
|
142 |
* Asigna el contenedor padre de la tabla
|
143 |
* @param tableContainer
|
144 |
*/
|
145 |
public void setTableContainer(TableContainer tableContainer) { |
146 |
this.tableContainer = tableContainer;
|
147 |
} |
148 |
|
149 |
/**
|
150 |
* Obtiene el modelo de la tabla
|
151 |
* @return
|
152 |
*/
|
153 |
public DefaultTableModel getTableModel() { |
154 |
return tableModel;
|
155 |
} |
156 |
|
157 |
//********************M?todos de Tabla*****************************//
|
158 |
|
159 |
/**
|
160 |
* A?ade una fila a la tabla.
|
161 |
* @param list Lista de cadenas
|
162 |
*/
|
163 |
public void addRow(Object[] list){ |
164 |
((DefaultTableModel)this.getJTable().getModel()).addRow(list); |
165 |
} |
166 |
|
167 |
/**
|
168 |
* Elimina una fila de la tabla.
|
169 |
* @param i Fila a eliminar
|
170 |
*/
|
171 |
public void delRow(int i){ |
172 |
((DefaultTableModel)this.getJTable().getModel()).removeRow(i); |
173 |
} |
174 |
|
175 |
/**
|
176 |
* Intercambia una fila de la tabla por otra.
|
177 |
* @param i
|
178 |
* @param j
|
179 |
*/
|
180 |
public void swapRow(int i, int j) { |
181 |
DefaultTableModel model = (DefaultTableModel) this.getJTable().getModel(); |
182 |
if ((i < 0) || (j < 0)) |
183 |
return;
|
184 |
if ((i >= model.getRowCount()) || (j >= model.getRowCount()))
|
185 |
return;
|
186 |
model.moveRow(i, i, j); |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Elimina todas las filas de la tabla.
|
191 |
*/
|
192 |
public void removeAllRows(){ |
193 |
((DefaultTableModel)this.getJTable().getModel()).setNumRows(0); |
194 |
} |
195 |
} |