gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.impl / src / main / java / org / gvsig / raster / swing / impl / pagedtable / PagedTable.java @ 1503
History | View | Annotate | Download (10.4 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2012 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 2
|
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.raster.swing.impl.pagedtable; |
25 |
|
26 |
import java.awt.FlowLayout; |
27 |
import java.awt.GridBagConstraints; |
28 |
import java.awt.GridBagLayout; |
29 |
import java.awt.event.MouseEvent; |
30 |
import java.awt.event.MouseListener; |
31 |
import java.util.List; |
32 |
|
33 |
import javax.swing.JPanel; |
34 |
import javax.swing.JTable; |
35 |
import javax.swing.table.DefaultTableModel; |
36 |
import javax.swing.table.TableColumn; |
37 |
|
38 |
import org.gvsig.raster.swing.pagedtable.ModelLoader; |
39 |
|
40 |
|
41 |
/**
|
42 |
* Paged table
|
43 |
*
|
44 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
45 |
*/
|
46 |
public class PagedTable extends JPanel implements MouseListener { |
47 |
private static final long serialVersionUID = 1L; |
48 |
private PagedJTable jTable = null; |
49 |
private ModelLoader modelLoader = null; |
50 |
private DefaultTableModel model = null; |
51 |
private PaginationBarPanel paginationBar = null; |
52 |
private TableEntryControllerPanel tableEntryController = null; |
53 |
private TableListener tableListener = null; |
54 |
private MoveRowsPanel moveRowsPanel = null; |
55 |
private boolean initTable = false; |
56 |
private Pager pager = null; |
57 |
private int heightLine = 16; |
58 |
|
59 |
public class PagedJTable extends JTable { |
60 |
private static final long serialVersionUID = 1L; |
61 |
|
62 |
public PagedJTable(DefaultTableModel model) { |
63 |
super(model);
|
64 |
} |
65 |
|
66 |
/**
|
67 |
* Reimplemented to calculate the number of pages when the component gets
|
68 |
* a resize event
|
69 |
*/
|
70 |
public void setBounds(int x, int y, int width, int height) { |
71 |
super.setBounds(x, y, width, height);
|
72 |
int entriesPerPage = (int)(getHeight() / (double)heightLine); |
73 |
getPager(entriesPerPage); |
74 |
reloadPage(); |
75 |
} |
76 |
|
77 |
} |
78 |
|
79 |
public PagedTable(ModelLoader modelLoader) {
|
80 |
this.modelLoader = modelLoader;
|
81 |
this.model = modelLoader.getTableModel();
|
82 |
tableListener = new TableListener(this); |
83 |
init(); |
84 |
} |
85 |
|
86 |
private void init() { |
87 |
initTable = true;
|
88 |
setLayout(new GridBagLayout()); |
89 |
GridBagConstraints gbc = new GridBagConstraints(); |
90 |
gbc.fill = GridBagConstraints.BOTH;
|
91 |
gbc.weightx = 1;
|
92 |
gbc.weighty = 1;
|
93 |
gbc.gridx = 0;
|
94 |
gbc.gridy = 0;
|
95 |
|
96 |
add(getJTable(), gbc); |
97 |
|
98 |
gbc.fill = GridBagConstraints.VERTICAL;
|
99 |
gbc.weightx = 0;
|
100 |
gbc.weighty = 1;
|
101 |
gbc.gridx = 1;
|
102 |
gbc.gridy = 0;
|
103 |
|
104 |
add(getPaginationBar(), gbc); |
105 |
|
106 |
gbc.fill = GridBagConstraints.HORIZONTAL;
|
107 |
gbc.weightx = 1;
|
108 |
gbc.weighty = 0;
|
109 |
gbc.gridx = 0;
|
110 |
gbc.gridy = 1;
|
111 |
gbc.gridwidth = 2;
|
112 |
|
113 |
JPanel panel = new JPanel(); |
114 |
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); |
115 |
panel.add(getTableEntryController()); |
116 |
panel.add(getMoveRowsPanel()); |
117 |
add(panel, gbc); |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Reloads the selected page in the visual table
|
122 |
*/
|
123 |
public int reloadPage() { |
124 |
try {
|
125 |
removeAllVisualRows(); |
126 |
List<Object> lines = pager.getSelectedPage(); |
127 |
if(lines == null) |
128 |
return pager.getIndexSelectedPage();
|
129 |
for (int i = 0; i < lines.size(); i++) { |
130 |
model.addRow((Object[])lines.get(i)); |
131 |
getTableEntryController().setNItems(pager.getEntriesCount()); |
132 |
} |
133 |
} catch (NotInitializeException e) {
|
134 |
} |
135 |
return pager.getIndexSelectedPage();
|
136 |
} |
137 |
|
138 |
public Pager getPager(int nEntriesPerPage) { |
139 |
if(pager == null) |
140 |
pager = new Pager(nEntriesPerPage);
|
141 |
else
|
142 |
pager.setEntriesPerPage(nEntriesPerPage); |
143 |
return pager;
|
144 |
} |
145 |
|
146 |
public TableEntryControllerPanel getTableEntryController() {
|
147 |
if(tableEntryController == null) { |
148 |
tableEntryController = new TableEntryControllerPanel(tableListener);
|
149 |
} |
150 |
return tableEntryController;
|
151 |
} |
152 |
|
153 |
public PaginationBarPanel getPaginationBar() {
|
154 |
if(paginationBar == null) { |
155 |
paginationBar = new PaginationBarPanel();
|
156 |
} |
157 |
return paginationBar;
|
158 |
} |
159 |
|
160 |
public DefaultTableModel getTableModel() { |
161 |
return modelLoader.getTableModel();
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* This method initializes jPanel
|
166 |
* @return javax.swing.JPanel
|
167 |
*/
|
168 |
public MoveRowsPanel getMoveRowsPanel() {
|
169 |
if (moveRowsPanel == null) { |
170 |
moveRowsPanel = new MoveRowsPanel(tableListener);
|
171 |
moveRowsPanel.setVisible(false);
|
172 |
} |
173 |
return moveRowsPanel;
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* Obtiene la Tabla
|
178 |
* @return Tabla de bandas de la imagen
|
179 |
*/
|
180 |
public JTable getJTable() { |
181 |
if (jTable == null) { |
182 |
jTable = new PagedJTable(model);
|
183 |
jTable.addMouseListener(this);
|
184 |
|
185 |
TableColumn column = null; |
186 |
|
187 |
int widthPerColumn = (int) (this.getWidth() / modelLoader.getColumnNames().length); |
188 |
for (int i = 0; i < modelLoader.getColumnNames().length; i++) { |
189 |
column = jTable.getColumnModel().getColumn(i); |
190 |
column.setResizable(true);
|
191 |
if (modelLoader.getColumnWidths() == null) |
192 |
column.setPreferredWidth(widthPerColumn); |
193 |
else
|
194 |
column.setPreferredWidth(modelLoader.getColumnWidths()[i]); |
195 |
} |
196 |
} |
197 |
|
198 |
return jTable;
|
199 |
} |
200 |
|
201 |
public void setLineHeight(int height) { |
202 |
this.heightLine = height;
|
203 |
} |
204 |
|
205 |
/**
|
206 |
* Gets the selected rows
|
207 |
* @return
|
208 |
* @throws NotInitializeException
|
209 |
*/
|
210 |
public int[] getSelectedRows() throws NotInitializeException { |
211 |
if (!initTable)
|
212 |
throw new NotInitializeException(); |
213 |
|
214 |
int[] tSelected = getJTable().getSelectedRows(); |
215 |
int[] result = new int[tSelected.length]; |
216 |
for (int i = 0; i < result.length; i++) { |
217 |
result[i] = pager.getIndexSelectedPage() * pager.getEntriesPerPage() + tSelected[i]; |
218 |
} |
219 |
return result;
|
220 |
} |
221 |
|
222 |
/**
|
223 |
* Intercambia una fila de la tabla por otra.
|
224 |
* @param i Fila a eliminar
|
225 |
*/
|
226 |
public void swapRow(int i, int j) throws NotInitializeException { |
227 |
if (!initTable)
|
228 |
throw new NotInitializeException(); |
229 |
|
230 |
TableListener.comboEventEnable = false;
|
231 |
|
232 |
if ((i < 0) || (j < 0)) |
233 |
return;
|
234 |
if ((i >= model.getRowCount()) || (j >= model.getRowCount()))
|
235 |
return;
|
236 |
model.moveRow(i, i, j); |
237 |
pager.swapRow(i, j); |
238 |
|
239 |
TableListener.comboEventEnable = true;
|
240 |
} |
241 |
|
242 |
/**
|
243 |
* A?ade una fila a la tabla.
|
244 |
* @param list Lista de cadenas
|
245 |
*/
|
246 |
public void addRow(Object[] list) throws NotInitializeException { |
247 |
if (!initTable)
|
248 |
throw new NotInitializeException(); |
249 |
|
250 |
TableListener.comboEventEnable = false;
|
251 |
|
252 |
pager.addEntry(list); |
253 |
//Cambio de p?gina
|
254 |
if(model.getRowCount() >= pager.getEntriesPerPage()) {
|
255 |
for (int i = model.getRowCount() - 1; i >= 0; i--) { |
256 |
model.removeRow(i); |
257 |
} |
258 |
pager.increaseSelectedPage(); |
259 |
getPaginationBar().getSelectedPage().setText(pager.getIndexSelectedPage() + "");
|
260 |
} |
261 |
model.addRow(list); |
262 |
getTableEntryController().addPointToTable(pager.getEntriesCount()); |
263 |
setSelectedIndex(model.getRowCount() - 1);
|
264 |
|
265 |
TableListener.comboEventEnable = true;
|
266 |
} |
267 |
|
268 |
/**
|
269 |
* Selects a i point
|
270 |
* @param i punto a seleccionar
|
271 |
*/
|
272 |
public void setSelectedIndex(int i) throws NotInitializeException { |
273 |
if (!initTable)
|
274 |
throw new NotInitializeException(); |
275 |
|
276 |
TableListener.comboEventEnable = false;
|
277 |
|
278 |
try {
|
279 |
int index = pager.getIndexSelectedPage() * pager.getEntriesPerPage() + i;
|
280 |
getTableEntryController().setSelectedIndex(index); |
281 |
getMoveRowsPanel().setSelectedIndex(i, model.getRowCount()); |
282 |
getJTable().setRowSelectionInterval(i, i); |
283 |
} catch (IllegalArgumentException ex) { |
284 |
|
285 |
} |
286 |
|
287 |
TableListener.comboEventEnable = true;
|
288 |
} |
289 |
|
290 |
/**
|
291 |
* Removes all rows of this table.
|
292 |
*/
|
293 |
public void removeAllRows() throws NotInitializeException { |
294 |
if (!initTable)
|
295 |
throw new NotInitializeException(); |
296 |
|
297 |
TableListener.comboEventEnable = false;
|
298 |
|
299 |
model.setNumRows(0);
|
300 |
getTableEntryController().setNItems(0);
|
301 |
pager.removeAllEntries(); |
302 |
|
303 |
TableListener.comboEventEnable = true;
|
304 |
} |
305 |
|
306 |
/**
|
307 |
* Removes all rows of this table.
|
308 |
*/
|
309 |
public void removeAllVisualRows() throws NotInitializeException { |
310 |
if (!initTable)
|
311 |
throw new NotInitializeException(); |
312 |
|
313 |
TableListener.comboEventEnable = false;
|
314 |
|
315 |
model.setNumRows(0);
|
316 |
getTableEntryController().setNItems(0);
|
317 |
|
318 |
TableListener.comboEventEnable = true;
|
319 |
} |
320 |
|
321 |
/**
|
322 |
* Deletes a row from the table.
|
323 |
* @param Global position of the entry
|
324 |
*/
|
325 |
public void delRow(int i) throws NotInitializeException { |
326 |
if (!initTable)
|
327 |
throw new NotInitializeException(); |
328 |
|
329 |
TableListener.comboEventEnable = false;
|
330 |
|
331 |
int selectedRow = getSelectedVisualRow() - 1; |
332 |
int currentPage = pager.getIndexSelectedPage();
|
333 |
pager.removeEntry(i); |
334 |
getTableEntryController().setNItems(pager.getEntriesCount()); |
335 |
int pageLoaded = reloadPage();
|
336 |
|
337 |
if(currentPage != pageLoaded) { //Se ha cargado una nueva p?gina |
338 |
setSelectedIndex(model.getRowCount() - 1);
|
339 |
} else if(selectedRow >= 0 && selectedRow < model.getRowCount()) { |
340 |
setSelectedIndex(selectedRow); |
341 |
} |
342 |
|
343 |
TableListener.comboEventEnable = true;
|
344 |
} |
345 |
|
346 |
/**
|
347 |
* Gets the selected point
|
348 |
* @return position of the selected point
|
349 |
*/
|
350 |
public int getSelectedVisualRow() throws NotInitializeException { |
351 |
if (!initTable)
|
352 |
throw new NotInitializeException(); |
353 |
|
354 |
return getJTable().getSelectedRow();
|
355 |
} |
356 |
|
357 |
/**
|
358 |
* Gets the number of rows in this table
|
359 |
* @return N?mero de filas de la tabla
|
360 |
*/
|
361 |
public int getRowCount() throws NotInitializeException { |
362 |
if (!initTable)
|
363 |
throw new NotInitializeException(); |
364 |
|
365 |
return model.getRowCount();
|
366 |
} |
367 |
|
368 |
public void mouseClicked(MouseEvent e) { |
369 |
if(e.getSource() == jTable) {
|
370 |
//When the user clicks on table, it selects the entry in the table control
|
371 |
try {
|
372 |
int selVisualRow = getSelectedVisualRow();
|
373 |
if(selVisualRow >= 0) { |
374 |
int selRow = pager.getIndexSelectedPage() * pager.getEntriesPerPage() + selVisualRow;
|
375 |
getTableEntryController().setSelectedIndex(selRow); |
376 |
} |
377 |
} catch (NotInitializeException e1) {
|
378 |
} |
379 |
} |
380 |
} |
381 |
|
382 |
public void mouseEntered(MouseEvent e) { |
383 |
|
384 |
} |
385 |
|
386 |
public void mouseExited(MouseEvent e) { |
387 |
|
388 |
} |
389 |
|
390 |
public void mousePressed(MouseEvent e) { |
391 |
|
392 |
} |
393 |
|
394 |
public void mouseReleased(MouseEvent e) { |
395 |
|
396 |
} |
397 |
} |