svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libUIComponent / src / org / gvsig / gui / beans / table / listeners / TableListener.java @ 30043
History | View | Annotate | Download (4.89 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 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.listeners; |
20 |
|
21 |
import java.awt.event.ActionEvent; |
22 |
import java.awt.event.ActionListener; |
23 |
|
24 |
import javax.swing.event.ListSelectionEvent; |
25 |
import javax.swing.event.ListSelectionListener; |
26 |
|
27 |
import org.gvsig.gui.beans.table.MoveRowsPanel; |
28 |
import org.gvsig.gui.beans.table.Table; |
29 |
import org.gvsig.gui.beans.table.TableContainer; |
30 |
import org.gvsig.gui.beans.table.TableControlerPanel; |
31 |
import org.gvsig.gui.beans.table.exceptions.NotInitializeException; |
32 |
import org.gvsig.gui.beans.table.models.IModel; |
33 |
/**
|
34 |
* Listener para la tabla
|
35 |
* @author Nacho Brodin <brodin_ign@gva.es>
|
36 |
*/
|
37 |
public class TableListener implements ActionListener, ListSelectionListener { |
38 |
public static boolean comboEventEnable = true; |
39 |
private TableContainer tableContainer = null; |
40 |
private TableControlerPanel controlPanel = null; |
41 |
private MoveRowsPanel moveRowsPanel = null; |
42 |
private Table table = null; |
43 |
public boolean enableNewLineListener = true; |
44 |
|
45 |
/**
|
46 |
* Constructor
|
47 |
* @param tableContainer
|
48 |
*/
|
49 |
public TableListener(TableContainer tableContainer){
|
50 |
this.tableContainer = tableContainer;
|
51 |
} |
52 |
|
53 |
private void initialize(){ |
54 |
controlPanel = tableContainer.getPTableControl(); |
55 |
moveRowsPanel = tableContainer.getMoveRowsPanel(); |
56 |
table = tableContainer.getTable(); |
57 |
} |
58 |
|
59 |
/**
|
60 |
* Captura y gestiona los eventos del control de la tabla para a?adir filas,
|
61 |
* eliminar filas y moverse por la tabla.
|
62 |
*/
|
63 |
public void actionPerformed(ActionEvent e) { |
64 |
if (controlPanel == null || table == null) |
65 |
initialize(); |
66 |
|
67 |
try {
|
68 |
/**
|
69 |
* Nueva entrada
|
70 |
*/
|
71 |
if (enableNewLineListener && e.getSource() == controlPanel.getBNew()) {
|
72 |
if (table.getTableModel() instanceof IModel) |
73 |
tableContainer.addRow(((IModel) table.getTableModel()).getNewLine()); |
74 |
} |
75 |
|
76 |
/**
|
77 |
* Eliminar todas las filas
|
78 |
*/
|
79 |
if (enableNewLineListener && e.getSource() == controlPanel.getBClear()) {
|
80 |
tableContainer.removeAllRows(); |
81 |
} |
82 |
|
83 |
/**
|
84 |
* Elimina una entrada concreta de la tabla
|
85 |
*/
|
86 |
if (enableNewLineListener && e.getSource() == controlPanel.getBDelPoint()) {
|
87 |
int[] lista = tableContainer.getSelectedRows(); |
88 |
for (int i = lista.length - 1; i>=0; i--) |
89 |
tableContainer.delRow(lista[i]); |
90 |
} |
91 |
|
92 |
/**
|
93 |
* Subir un elemento
|
94 |
*/
|
95 |
if (e.getSource() == moveRowsPanel.getBUp()) {
|
96 |
int[] lista = tableContainer.getSelectedRows(); |
97 |
if (lista.length > 0) { |
98 |
for (int i = 0; i < lista.length; i++) |
99 |
tableContainer.swapRow(lista[i] - 1, lista[i]);
|
100 |
if (lista[0] > 0) |
101 |
tableContainer.setSelectedIndex(lista[0] - 1); |
102 |
} |
103 |
} |
104 |
|
105 |
/**
|
106 |
* Bajar un elemento
|
107 |
*/
|
108 |
if (e.getSource() == moveRowsPanel.getBDown()) {
|
109 |
int[] lista = tableContainer.getSelectedRows(); |
110 |
if (lista.length > 0) { |
111 |
for (int i = lista.length - 1; i >= 0; i--) |
112 |
tableContainer.swapRow(lista[i], lista[i] + 1);
|
113 |
tableContainer.setSelectedIndex(lista[0] + 1); |
114 |
} |
115 |
} |
116 |
|
117 |
if (e.getSource() == controlPanel.getBFirst()) {
|
118 |
tableContainer.setSelectedIndex(0);
|
119 |
} |
120 |
|
121 |
if (e.getSource() == controlPanel.getBLast()) {
|
122 |
tableContainer.setSelectedIndex(tableContainer.getRowCount() - 1);
|
123 |
} |
124 |
|
125 |
if (e.getSource() == controlPanel.getBNext()) {
|
126 |
if (tableContainer.getSelectedRow() < tableContainer.getRowCount())
|
127 |
tableContainer.setSelectedIndex(tableContainer.getSelectedRow() + 1);
|
128 |
} |
129 |
|
130 |
if (e.getSource() == controlPanel.getBPrev()) {
|
131 |
if (tableContainer.getSelectedRow() > 0) |
132 |
tableContainer.setSelectedIndex(tableContainer.getSelectedRow() - 1);
|
133 |
} |
134 |
|
135 |
if (e.getSource() == controlPanel.getCPoint()) {
|
136 |
if (comboEventEnable)
|
137 |
tableContainer.setSelectedIndex(controlPanel.getCPoint().getSelectedIndex()); |
138 |
} |
139 |
|
140 |
} catch (NotInitializeException ex) {
|
141 |
|
142 |
} |
143 |
} |
144 |
|
145 |
/*
|
146 |
* (non-Javadoc)
|
147 |
* @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
|
148 |
*/
|
149 |
public void valueChanged(ListSelectionEvent e) { |
150 |
try {
|
151 |
tableContainer.setSelectedIndex(tableContainer.getSelectedRow()); |
152 |
} catch (NotInitializeException e1) {
|
153 |
e1.printStackTrace(); |
154 |
} |
155 |
} |
156 |
} |