Revision 1705 org.gvsig.raster/tags/2.0.0/org.gvsig.raster.swing/org.gvsig.raster.swing.impl/src/main/java/org/gvsig/raster/swing/impl/pagedtable/PagedTableImpl.java
PagedTableImpl.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.raster.swing.impl.pagedtable; |
25 | 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 |
import java.util.Observable; |
|
33 |
import java.util.Observer; |
|
26 |
import javax.swing.JComponent; |
|
34 | 27 |
|
35 |
import javax.swing.JPanel; |
|
36 |
import javax.swing.JTable; |
|
37 |
import javax.swing.table.DefaultTableModel; |
|
38 |
import javax.swing.table.TableColumn; |
|
39 |
|
|
40 | 28 |
import org.gvsig.raster.swing.pagedtable.ModelLoader; |
41 | 29 |
import org.gvsig.raster.swing.pagedtable.PagedTable; |
42 | 30 |
import org.gvsig.raster.swing.pagedtable.TableModel; |
... | ... | |
47 | 35 |
* |
48 | 36 |
* @author Nacho Brodin (nachobrodin@gmail.com) |
49 | 37 |
*/ |
50 |
public class PagedTableImpl extends JPanel implements PagedTable, MouseListener, Observer {
|
|
38 |
public class PagedTableImpl implements PagedTable {
|
|
51 | 39 |
private static final long serialVersionUID = 1L; |
52 |
private PagedJTable jTable = null; |
|
53 | 40 |
private ModelLoader modelLoader = null; |
54 |
private DefaultTableModel model = null;
|
|
41 |
private PagedTablePanel panel = null;
|
|
55 | 42 |
private PaginationBarPanel paginationBar = null; |
56 | 43 |
private TableEntryControllerPanel tableEntryController = null; |
57 | 44 |
private TableControllerListener tableListener = null; |
58 | 45 |
private MoveRowsPanel moveRowsPanel = null; |
59 | 46 |
private Pager pager = null; |
60 |
private int heightLine = 16; |
|
61 |
private int currentPage = 0; |
|
62 | 47 |
|
63 |
public class PagedJTable extends JTable { |
|
64 |
private static final long serialVersionUID = 1L; |
|
65 |
|
|
66 |
public PagedJTable(DefaultTableModel model) { |
|
67 |
super(model); |
|
68 |
} |
|
69 |
|
|
70 |
/** |
|
71 |
* Reimplemented to calculate the number of pages when the component gets |
|
72 |
* a resize event |
|
73 |
*/ |
|
74 |
public void setBounds(int x, int y, int width, int height) { |
|
75 |
super.setBounds(x, y, width, height); |
|
76 |
int entriesPerPage = (int)(getHeight() / (double)heightLine); |
|
77 |
getPager(entriesPerPage); |
|
78 |
reloadPage(); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 | 48 |
public PagedTableImpl(ModelLoader modelLoader) { |
83 | 49 |
this.modelLoader = modelLoader; |
84 |
this.model = (DefaultTableModel)modelLoader.getTableModel(); |
|
85 | 50 |
tableListener = new TableControllerListener(this); |
86 |
init();
|
|
87 |
getPager(0).addObserver(getTableEntryController());
|
|
88 |
getPager(0).addObserver(this);
|
|
89 |
getPager(0).addObserver(getPaginationBar());
|
|
51 |
getPager().addObserver(getTableEntryController());
|
|
52 |
getPager().addObserver(getPaginationBar());
|
|
53 |
getPager().addObserver(getMoveRowsPanel());
|
|
54 |
getPager().addObserver(getPagedTablePanel());
|
|
90 | 55 |
} |
91 | 56 |
|
92 |
private void init() { |
|
93 |
setLayout(new GridBagLayout()); |
|
94 |
GridBagConstraints gbc = new GridBagConstraints(); |
|
95 |
gbc.fill = GridBagConstraints.BOTH; |
|
96 |
gbc.weightx = 1; |
|
97 |
gbc.weighty = 1; |
|
98 |
gbc.gridx = 0; |
|
99 |
gbc.gridy = 0; |
|
100 |
|
|
101 |
add(getJTable(), gbc); |
|
102 |
|
|
103 |
gbc.fill = GridBagConstraints.VERTICAL; |
|
104 |
gbc.weightx = 0; |
|
105 |
gbc.weighty = 1; |
|
106 |
gbc.gridx = 1; |
|
107 |
gbc.gridy = 0; |
|
108 |
|
|
109 |
add(getPaginationBar(), gbc); |
|
110 |
|
|
111 |
gbc.fill = GridBagConstraints.HORIZONTAL; |
|
112 |
gbc.weightx = 1; |
|
113 |
gbc.weighty = 0; |
|
114 |
gbc.gridx = 0; |
|
115 |
gbc.gridy = 1; |
|
116 |
gbc.gridwidth = 2; |
|
117 |
|
|
118 |
JPanel panel = new JPanel(); |
|
119 |
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); |
|
120 |
panel.add(getTableEntryController()); |
|
121 |
panel.add(getMoveRowsPanel()); |
|
122 |
add(panel, gbc); |
|
123 |
} |
|
124 |
|
|
125 |
public Pager getPager(int nEntriesPerPage) { |
|
57 |
public Pager getPager() { |
|
126 | 58 |
if(pager == null) { |
127 |
pager = new Pager(nEntriesPerPage); |
|
128 |
} else |
|
129 |
pager.setEntriesPerPage(nEntriesPerPage); |
|
59 |
pager = new Pager(0); |
|
60 |
} |
|
130 | 61 |
return pager; |
131 | 62 |
} |
132 | 63 |
|
... | ... | |
137 | 68 |
return tableEntryController; |
138 | 69 |
} |
139 | 70 |
|
140 |
public PaginationBarPanel getPaginationBar() { |
|
141 |
if(paginationBar == null) { |
|
142 |
paginationBar = new PaginationBarPanel(tableListener); |
|
71 |
public PagedTablePanel getPagedTablePanel() { |
|
72 |
if(panel == null) { |
|
73 |
panel = new PagedTablePanel(modelLoader, |
|
74 |
getPager(), |
|
75 |
getPaginationBar(), |
|
76 |
getMoveRowsPanel(), |
|
77 |
getTableEntryController()); |
|
143 | 78 |
} |
144 |
return paginationBar;
|
|
79 |
return panel;
|
|
145 | 80 |
} |
146 | 81 |
|
147 |
/** |
|
148 |
* This method initializes jPanel |
|
149 |
* @return javax.swing.JPanel |
|
150 |
*/ |
|
82 |
public Object getValueAt(int row, int column) { |
|
83 |
Object[] entry = pager.getEntry(row); |
|
84 |
if(entry != null && column >= 0 && column < entry.length) |
|
85 |
return entry[column]; |
|
86 |
return null; |
|
87 |
} |
|
88 |
|
|
89 |
public void setValueAt(Object obj, int row, int column) { |
|
90 |
Object[] entry = pager.getEntry(row); |
|
91 |
if(entry != null && column >= 0 && column < entry.length) |
|
92 |
entry[column] = obj; |
|
93 |
} |
|
94 |
|
|
95 |
public int getColumnCount() { |
|
96 |
return panel.getJTable().getModel().getColumnCount(); |
|
97 |
} |
|
98 |
|
|
99 |
public int getRowCount() { |
|
100 |
return pager.getEntriesCount(); |
|
101 |
} |
|
102 |
|
|
151 | 103 |
public MoveRowsPanel getMoveRowsPanel() { |
152 | 104 |
if (moveRowsPanel == null) { |
153 | 105 |
moveRowsPanel = new MoveRowsPanel(tableListener); |
154 |
moveRowsPanel.setVisible(false); |
|
155 | 106 |
} |
156 | 107 |
return moveRowsPanel; |
157 | 108 |
} |
158 | 109 |
|
159 |
/** |
|
160 |
* Obtiene la Tabla |
|
161 |
* @return Tabla de bandas de la imagen |
|
162 |
*/ |
|
163 |
public JTable getJTable() { |
|
164 |
if (jTable == null) { |
|
165 |
jTable = new PagedJTable(model); |
|
166 |
jTable.addMouseListener(this); |
|
167 |
|
|
168 |
TableColumn column = null; |
|
169 |
|
|
170 |
int widthPerColumn = (int) (this.getWidth() / modelLoader.getColumnNames().length); |
|
171 |
for (int i = 0; i < modelLoader.getColumnNames().length; i++) { |
|
172 |
column = jTable.getColumnModel().getColumn(i); |
|
173 |
column.setResizable(true); |
|
174 |
if (modelLoader.getColumnWidths() == null) |
|
175 |
column.setPreferredWidth(widthPerColumn); |
|
176 |
else |
|
177 |
column.setPreferredWidth(modelLoader.getColumnWidths()[i]); |
|
178 |
} |
|
110 |
public PaginationBarPanel getPaginationBar() { |
|
111 |
if(paginationBar == null) { |
|
112 |
paginationBar = new PaginationBarPanel(tableListener); |
|
179 | 113 |
} |
180 |
|
|
181 |
return jTable; |
|
182 |
} |
|
183 |
|
|
184 |
public void setLineHeight(int height) { |
|
185 |
this.heightLine = height; |
|
114 |
return paginationBar; |
|
186 | 115 |
} |
187 | 116 |
|
188 |
//************************************************ |
|
189 |
// API |
|
190 |
//************************************************ |
|
191 |
|
|
192 | 117 |
public void addRow(Object[] list) { |
193 | 118 |
pager.addEntry(list); |
194 | 119 |
} |
195 | 120 |
|
196 | 121 |
public void delRow(int i) { |
197 |
currentPage = pager.getSelectedPageNumber(); |
|
198 | 122 |
pager.removeEntry(i); |
199 | 123 |
} |
200 | 124 |
|
... | ... | |
207 | 131 |
} |
208 | 132 |
|
209 | 133 |
public void swapRow(int i, int j) { |
210 |
if ((i < 0) || (j < 0)) |
|
211 |
return; |
|
212 |
if ((i >= model.getRowCount()) || (j >= model.getRowCount())) |
|
213 |
return; |
|
214 |
model.moveRow(i, i, j); |
|
215 | 134 |
pager.swapRow(i, j); |
216 | 135 |
} |
217 | 136 |
|
... | ... | |
239 | 158 |
pager.decreaseSelectedRows(); |
240 | 159 |
} |
241 | 160 |
|
242 |
public TableModel getTableModel() {
|
|
243 |
return modelLoader.getTableModel();
|
|
161 |
public void nextPage() {
|
|
162 |
pager.increaseSelectedPage();
|
|
244 | 163 |
} |
245 | 164 |
|
246 |
//************************************************ |
|
165 |
public void prevPage() { |
|
166 |
pager.decreaseSelectedPage(); |
|
167 |
} |
|
247 | 168 |
|
248 |
/** |
|
249 |
* Gets the number of rows in this table |
|
250 |
* @return N?mero de filas de la tabla |
|
251 |
*/ |
|
252 |
public int getRowTableCount() { |
|
253 |
return model.getRowCount(); |
|
169 |
public void setSelectedPage(int pageNumber) { |
|
170 |
pager.setSelectedPage(pageNumber); |
|
254 | 171 |
} |
255 | 172 |
|
256 |
/** |
|
257 |
* Gets the selected point |
|
258 |
* @return position of the selected point |
|
259 |
*/ |
|
260 |
public int getSelectedTableRow() { |
|
261 |
return getJTable().getSelectedRow(); |
|
173 |
public TableModel getTableModel() { |
|
174 |
return modelLoader.getTableModel(); |
|
262 | 175 |
} |
263 | 176 |
|
264 |
/** |
|
265 |
* Reloads the selected page in the visual table |
|
266 |
*/ |
|
267 |
private int reloadPage() { |
|
268 |
model.setNumRows(0); |
|
269 |
List<Object> lines = pager.getSelectedPage(); |
|
270 |
if(lines == null) |
|
271 |
return pager.getSelectedPageNumber(); |
|
272 |
for (int i = 0; i < lines.size(); i++) { |
|
273 |
model.addRow((Object[])lines.get(i)); |
|
274 |
} |
|
275 |
return pager.getSelectedPageNumber(); |
|
177 |
public JComponent getComponent() { |
|
178 |
return panel; |
|
276 | 179 |
} |
277 | 180 |
|
278 |
|
|
279 |
private void loadPageInTable(int page, int selection) { |
|
280 |
for (int i = model.getRowCount() - 1; i >= 0; i--) { |
|
281 |
model.removeRow(i); |
|
282 |
} |
|
283 |
List<Object> pageElements = pager.getPage(page); |
|
284 |
for (int i = 0; i < pageElements.size(); i++) { |
|
285 |
model.addRow((Object[])pageElements.get(i)); |
|
286 |
} |
|
287 |
if(selection >= 0 && selection < model.getRowCount()) { |
|
288 |
getJTable().setRowSelectionInterval(selection, selection); |
|
289 |
} |
|
181 |
public void showMoveRowsControls(boolean show) { |
|
182 |
getMoveRowsPanel().setVisible(false); |
|
290 | 183 |
} |
291 |
|
|
292 |
public void mouseClicked(MouseEvent e) { |
|
293 |
if(e.getSource() == jTable) { |
|
294 |
//When the user clicks on table, it selects the entry in the table control |
|
295 |
int selVisualRow = getSelectedTableRow(); |
|
296 |
if(selVisualRow >= 0) { |
|
297 |
int selRow = pager.getSelectedPageNumber() * pager.getEntriesPerPage() + selVisualRow; |
|
298 |
pager.setSelectedRows(new int[]{selRow}); |
|
299 |
} |
|
300 |
} |
|
301 |
} |
|
302 |
|
|
303 |
public void mouseEntered(MouseEvent e) { |
|
304 |
|
|
305 |
} |
|
306 |
|
|
307 |
public void mouseExited(MouseEvent e) { |
|
308 |
|
|
309 |
} |
|
310 |
|
|
311 |
public void mousePressed(MouseEvent e) { |
|
312 |
|
|
313 |
} |
|
314 |
|
|
315 |
public void mouseReleased(MouseEvent e) { |
|
316 |
|
|
317 |
} |
|
318 |
|
|
319 | 184 |
|
320 |
public void update(Observable o, Object arg) { |
|
321 |
if(o instanceof Pager && arg instanceof Integer) { |
|
322 |
int option = ((Integer)arg).intValue(); |
|
323 |
if(option == Pager.CHANGE_SELECTED_ROWS ) { |
|
324 |
Pager pager = (Pager)o; |
|
325 |
int[] selectedRows = pager.getSelectedRows(); |
|
326 |
if(selectedRows != null && selectedRows.length > 0) { |
|
327 |
if(!pager.isInCurrentPage(selectedRows[0])) { |
|
328 |
pager.setSelectedPage(pager.getPageFromRow(selectedRows[0])); |
|
329 |
reloadPage(); |
|
330 |
} |
|
331 |
int r = selectedRows[0] % pager.getEntriesPerPage(); |
|
332 |
if(r >= 0 && r < model.getRowCount()) |
|
333 |
getJTable().setRowSelectionInterval(r, r); |
|
334 |
} |
|
335 |
} |
|
336 |
|
|
337 |
if(option == Pager.CHANGE_ADD_ENTRY ) { |
|
338 |
//Cambio de p?gina |
|
339 |
if(model.getRowCount() >= pager.getEntriesPerPage()) { |
|
340 |
for (int i = model.getRowCount() - 1; i >= 0; i--) { |
|
341 |
model.removeRow(i); |
|
342 |
} |
|
343 |
pager.increaseSelectedPage(); |
|
344 |
getPaginationBar().getSelectedPage().setText(pager.getSelectedPageNumber() + ""); |
|
345 |
} |
|
346 |
model.addRow(pager.getLastEntry()); |
|
347 |
int lastTableRow = model.getRowCount() - 1; |
|
348 |
getJTable().setRowSelectionInterval(lastTableRow, lastTableRow); |
|
349 |
} |
|
350 |
|
|
351 |
if(option == Pager.CHANGE_REMOVE_ENTRY ) { |
|
352 |
int[] selectedRows = pager.getSelectedRows(); |
|
353 |
int pageLoaded = reloadPage(); |
|
354 |
|
|
355 |
//Se ha cargado una nueva p?gina |
|
356 |
if(currentPage != pageLoaded) { |
|
357 |
int r = model.getRowCount() - 1; |
|
358 |
getJTable().setRowSelectionInterval(r, r); |
|
359 |
} else { |
|
360 |
int r = selectedRows[0] % pager.getEntriesPerPage(); |
|
361 |
if(r >= 0 && r < model.getRowCount()) { |
|
362 |
getJTable().setRowSelectionInterval(r, r); |
|
363 |
} |
|
364 |
} |
|
365 |
} |
|
366 |
|
|
367 |
if(option == Pager.CHANGE_REMOVE_ALL ) { |
|
368 |
model.setNumRows(0); |
|
369 |
} |
|
370 |
|
|
371 |
} |
|
185 |
public void showControllerTable(boolean show) { |
|
186 |
getTableEntryController().setVisible(false); |
|
372 | 187 |
} |
373 | 188 |
} |
Also available in: Unified diff