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 | 10741 | nacho | /* 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 | 13414 | bsanchez | import javax.swing.event.ListSelectionEvent; |
25 | import javax.swing.event.ListSelectionListener; |
||
26 | |||
27 | 15679 | bsanchez | import org.gvsig.gui.beans.table.MoveRowsPanel; |
28 | 10741 | nacho | 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 | 12514 | bsanchez | * Listener para la tabla
|
35 | 10741 | nacho | * @author Nacho Brodin <brodin_ign@gva.es>
|
36 | */
|
||
37 | 13414 | bsanchez | public class TableListener implements ActionListener, ListSelectionListener { |
38 | 15679 | bsanchez | 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 | 10741 | nacho | |
45 | /**
|
||
46 | * Constructor
|
||
47 | * @param tableContainer
|
||
48 | */
|
||
49 | public TableListener(TableContainer tableContainer){
|
||
50 | this.tableContainer = tableContainer;
|
||
51 | } |
||
52 | 12138 | bsanchez | |
53 | 10741 | nacho | private void initialize(){ |
54 | 15679 | bsanchez | controlPanel = tableContainer.getPTableControl(); |
55 | moveRowsPanel = tableContainer.getMoveRowsPanel(); |
||
56 | table = tableContainer.getTable(); |
||
57 | 10741 | nacho | } |
58 | 12138 | bsanchez | |
59 | 10741 | nacho | /**
|
60 | 12514 | bsanchez | * Captura y gestiona los eventos del control de la tabla para a?adir filas,
|
61 | 10741 | nacho | * eliminar filas y moverse por la tabla.
|
62 | */
|
||
63 | public void actionPerformed(ActionEvent e) { |
||
64 | 12138 | bsanchez | if (controlPanel == null || table == null) |
65 | 10741 | nacho | initialize(); |
66 | 13414 | bsanchez | |
67 | 12138 | bsanchez | try {
|
68 | 10741 | nacho | /**
|
69 | * Nueva entrada
|
||
70 | */
|
||
71 | 12138 | bsanchez | if (enableNewLineListener && e.getSource() == controlPanel.getBNew()) {
|
72 | if (table.getTableModel() instanceof IModel) |
||
73 | tableContainer.addRow(((IModel) table.getTableModel()).getNewLine()); |
||
74 | 10741 | nacho | } |
75 | 12138 | bsanchez | |
76 | 10741 | nacho | /**
|
77 | * Eliminar todas las filas
|
||
78 | */
|
||
79 | 12138 | bsanchez | if (enableNewLineListener && e.getSource() == controlPanel.getBClear()) {
|
80 | 10741 | nacho | tableContainer.removeAllRows(); |
81 | } |
||
82 | 12138 | bsanchez | |
83 | 10741 | nacho | /**
|
84 | * Elimina una entrada concreta de la tabla
|
||
85 | */
|
||
86 | 12138 | bsanchez | if (enableNewLineListener && e.getSource() == controlPanel.getBDelPoint()) {
|
87 | 12553 | bsanchez | int[] lista = tableContainer.getSelectedRows(); |
88 | for (int i = lista.length - 1; i>=0; i--) |
||
89 | tableContainer.delRow(lista[i]); |
||
90 | 10741 | nacho | } |
91 | 12138 | bsanchez | |
92 | 15679 | bsanchez | /**
|
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 | 12138 | bsanchez | if (e.getSource() == controlPanel.getBFirst()) {
|
118 | 10741 | nacho | tableContainer.setSelectedIndex(0);
|
119 | } |
||
120 | 12138 | bsanchez | |
121 | if (e.getSource() == controlPanel.getBLast()) {
|
||
122 | 10741 | nacho | tableContainer.setSelectedIndex(tableContainer.getRowCount() - 1);
|
123 | } |
||
124 | 12138 | bsanchez | |
125 | if (e.getSource() == controlPanel.getBNext()) {
|
||
126 | 12553 | bsanchez | if (tableContainer.getSelectedRow() < tableContainer.getRowCount())
|
127 | tableContainer.setSelectedIndex(tableContainer.getSelectedRow() + 1);
|
||
128 | 10741 | nacho | } |
129 | 12138 | bsanchez | |
130 | if (e.getSource() == controlPanel.getBPrev()) {
|
||
131 | 12553 | bsanchez | if (tableContainer.getSelectedRow() > 0) |
132 | tableContainer.setSelectedIndex(tableContainer.getSelectedRow() - 1);
|
||
133 | 10741 | nacho | } |
134 | 12138 | bsanchez | |
135 | if (e.getSource() == controlPanel.getCPoint()) {
|
||
136 | if (comboEventEnable)
|
||
137 | 10741 | nacho | tableContainer.setSelectedIndex(controlPanel.getCPoint().getSelectedIndex()); |
138 | } |
||
139 | |||
140 | 12138 | bsanchez | } catch (NotInitializeException ex) {
|
141 | 10741 | nacho | |
142 | 12138 | bsanchez | } |
143 | } |
||
144 | 13414 | bsanchez | |
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 | 12138 | bsanchez | } |