Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / table / listeners / TableListener.java @ 40561

History | View | Annotate | Download (4.99 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.listeners;
25

    
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28

    
29
import javax.swing.event.ListSelectionEvent;
30
import javax.swing.event.ListSelectionListener;
31

    
32
import org.gvsig.gui.beans.table.MoveRowsPanel;
33
import org.gvsig.gui.beans.table.Table;
34
import org.gvsig.gui.beans.table.TableContainer;
35
import org.gvsig.gui.beans.table.TableControlerPanel;
36
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
37
import org.gvsig.gui.beans.table.models.IModel;
38
/**
39
 * Listener para la tabla
40
 * @author Nacho Brodin <brodin_ign@gva.es>
41
 */
42
public class TableListener implements ActionListener, ListSelectionListener {
43
        public static boolean       comboEventEnable      = true;
44
        private TableContainer      tableContainer        = null;
45
        private TableControlerPanel controlPanel          = null;
46
        private MoveRowsPanel       moveRowsPanel         = null;
47
        private Table               table                 = null;
48
        public boolean              enableNewLineListener = true;
49

    
50
        /**
51
         * Constructor
52
         * @param tableContainer
53
         */
54
        public TableListener(TableContainer tableContainer){
55
                this.tableContainer = tableContainer;
56
        }
57

    
58
        private void initialize(){
59
                controlPanel = tableContainer.getPTableControl();
60
                moveRowsPanel = tableContainer.getMoveRowsPanel();
61
                table = tableContainer.getTable();
62
        }
63

    
64
        /**
65
         * Captura y gestiona los eventos del control de la tabla para a?adir filas,
66
         * eliminar filas y moverse por la tabla.
67
         */
68
        public void actionPerformed(ActionEvent e) {
69
                if (controlPanel == null || table == null)
70
                        initialize();
71

    
72
                try {
73
                        /**
74
                         * Nueva entrada
75
                         */
76
                        if (enableNewLineListener && e.getSource() == controlPanel.getBNew()) {
77
                                if (table.getTableModel() instanceof IModel)
78
                                        tableContainer.addRow(((IModel) table.getTableModel()).getNewLine());
79
                        }
80

    
81
                        /**
82
                         * Eliminar todas las filas
83
                         */
84
                        if (enableNewLineListener && e.getSource() == controlPanel.getBClear()) {
85
                                tableContainer.removeAllRows();
86
                        }
87

    
88
                        /**
89
                         * Elimina una entrada concreta de la tabla
90
                         */
91
                        if (enableNewLineListener && e.getSource() == controlPanel.getBDelPoint()) {
92
                                int[] lista = tableContainer.getSelectedRows();
93
                                for (int i = lista.length - 1; i>=0; i--)
94
                                        tableContainer.delRow(lista[i]);
95
                        }
96

    
97
                        /**
98
                         * Subir un elemento
99
                         */
100
                        if (e.getSource() == moveRowsPanel.getBUp()) {
101
                                int[] lista = tableContainer.getSelectedRows();
102
                                if (lista.length > 0) {
103
                                  for (int i = 0; i < lista.length; i++)
104
                                          tableContainer.swapRow(lista[i] - 1, lista[i]);
105
                                  if (lista[0] > 0)
106
                                          tableContainer.setSelectedIndex(lista[0] - 1);
107
                                }
108
                        }
109

    
110
                        /**
111
                         * Bajar un elemento
112
                         */
113
                        if (e.getSource() == moveRowsPanel.getBDown()) {
114
                                int[] lista = tableContainer.getSelectedRows();
115
                                if (lista.length > 0) {
116
                                  for (int i = lista.length - 1; i >= 0; i--)
117
                                          tableContainer.swapRow(lista[i], lista[i] + 1);
118
                                  tableContainer.setSelectedIndex(lista[0] + 1);
119
                                }
120
                        }
121

    
122
                        if (e.getSource() == controlPanel.getBFirst()) {
123
                                tableContainer.setSelectedIndex(0);
124
                        }
125

    
126
                        if (e.getSource() == controlPanel.getBLast()) {
127
                                tableContainer.setSelectedIndex(tableContainer.getRowCount() - 1);
128
                        }
129

    
130
                        if (e.getSource() == controlPanel.getBNext()) {
131
                                if (tableContainer.getSelectedRow() < tableContainer.getRowCount())
132
                                        tableContainer.setSelectedIndex(tableContainer.getSelectedRow() + 1);
133
                        }
134

    
135
                        if (e.getSource() == controlPanel.getBPrev()) {
136
                                if (tableContainer.getSelectedRow() > 0)
137
                                        tableContainer.setSelectedIndex(tableContainer.getSelectedRow() - 1);
138
                        }
139

    
140
                        if (e.getSource() == controlPanel.getCPoint()) {
141
                                if (comboEventEnable)
142
                                        tableContainer.setSelectedIndex(controlPanel.getCPoint().getSelectedIndex());
143
                        }
144

    
145
                } catch (NotInitializeException ex) {
146

    
147
                }
148
        }
149

    
150
        /*
151
         * (non-Javadoc)
152
         * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
153
         */
154
        public void valueChanged(ListSelectionEvent e) {
155
                try {
156
            tableContainer.setSelectedIndex(tableContainer.getSelectedRow());
157
    } catch (NotInitializeException e1) {
158
            e1.printStackTrace();
159
    }
160
  }
161
}