01: /*
02: * Copyright (C) 2005 Jeff Tassin
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package com.jeta.swingbuilder.gui.utils;
20:
21: import java.awt.event.ActionEvent;
22: import java.awt.event.ActionListener;
23:
24: import javax.swing.JTable;
25:
26: import com.jeta.open.gui.framework.JETAController;
27: import com.jeta.open.gui.framework.JETAPanel;
28: import com.jeta.swingbuilder.gui.components.JETATableModel;
29:
30: public class TableSupportHandler extends JETAController {
31: private String m_table_name;
32: private JETAPanel m_view;
33:
34: public TableSupportHandler(JETAPanel view, String tableName,
35: String delId, String moveUp, String moveDown) {
36: super (view);
37: m_view = view;
38: m_table_name = tableName;
39: assignAction(delId, new DeleteItemAction());
40: assignAction(moveDown, new MoveDownAction());
41: assignAction(moveUp, new MoveUpAction());
42: }
43:
44: public class DeleteItemAction implements ActionListener {
45: public void actionPerformed(ActionEvent evt) {
46: JTable table = m_view.getTable(m_table_name);
47: JETATableModel items_model = (JETATableModel) table
48: .getModel();
49: int row = table.getSelectedRow();
50: if (row >= 0) {
51: items_model.removeRow(row);
52: row--;
53: if (row < 0)
54: row = 0;
55: if (items_model.getRowCount() > row) {
56: table.setRowSelectionInterval(row, row);
57: }
58: }
59: table.repaint();
60: m_view.repaint();
61: }
62: }
63:
64: public class MoveUpAction implements ActionListener {
65: public void actionPerformed(ActionEvent evt) {
66: JTable table = m_view.getTable(m_table_name);
67: JETATableModel tmodel = (JETATableModel) table.getModel();
68: int row = table.getSelectedRow();
69: if (row > 0 && table.getRowCount() > 1) {
70: tmodel.moveRow(row, row, row - 1);
71: table.setRowSelectionInterval(row - 1, row - 1);
72: }
73: table.repaint();
74: m_view.repaint();
75: }
76: }
77:
78: public class MoveDownAction implements ActionListener {
79: public void actionPerformed(ActionEvent evt) {
80: JTable table = m_view.getTable(m_table_name);
81: JETATableModel tmodel = (JETATableModel) table.getModel();
82: int row = table.getSelectedRow();
83: if (row >= 0 && (row + 1) < table.getRowCount()) {
84: tmodel.moveRow(row, row, row + 1);
85: table.setRowSelectionInterval(row + 1, row + 1);
86: }
87: table.repaint();
88: m_view.repaint();
89: }
90: }
91: }
|