PageRenderTime 142ms CodeModel.GetById 80ms app.highlight 6ms RepoModel.GetById 54ms app.codeStats 0ms

/src/mpv5/ui/popups/TablePopUp.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 86 lines | 50 code | 12 blank | 24 comment | 12 complexity | eee4a490f8441a6d1f40ef873ab3e319 MD5 | raw file
 1/*
 2 *  This file is part of YaBS.
 3 *
 4 *      YaBS is free software: you can redistribute it and/or modify
 5 *      it under the terms of the GNU General Public License as published by
 6 *      the Free Software Foundation, either version 3 of the License, or
 7 *      (at your option) any later version.
 8 *
 9 *      YaBS 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
12 *      GNU General Public License for more details.
13 *
14 *      You should have received a copy of the GNU General Public License
15 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
16 */
17package mpv5.ui.popups;
18
19import java.awt.event.ActionListener;
20import java.awt.event.MouseAdapter;
21import java.awt.event.MouseEvent;
22import java.awt.event.MouseListener;
23import javax.swing.JMenuItem;
24import javax.swing.JPopupMenu;
25import javax.swing.JSeparator;
26import javax.swing.JTable;
27
28/**
29 */
30public class TablePopUp extends JPopupMenu {
31
32    private ActionListener[] actions;
33
34    /**
35     * Adds a popup menu to the given table
36     * @param table
37     * @param Items
38     * @param actions
39     */
40    public TablePopUp(JTable table, String[] Items, ActionListener[] actions) {
41        super();
42
43        JMenuItem[] items = new JMenuItem[Items.length];
44        for (int j = 0; j < actions.length; j++) {
45            JMenuItem i = null;
46            if (actions[j] != null) {
47                ActionListener actionListener = actions[j];
48                i = new JMenuItem(Items[j]);
49                i.addActionListener(actionListener);
50            }
51            items[j] = i;
52        }
53
54        for (int i = 0; i < items.length; i++) {
55            JMenuItem item = items[i];
56            if (item != null) {
57                item.setHorizontalTextPosition(JMenuItem.LEFT);
58                this.add(item);
59            } else {
60                add(new JSeparator(JSeparator.HORIZONTAL));
61            }
62        }
63
64
65        table.addMouseListener(new MouseAdapter() {
66
67            @Override
68            public void mouseReleased(MouseEvent e) {
69
70                if (e.getButton() == MouseEvent.BUTTON2 || e.getButton() == MouseEvent.BUTTON3) {
71                    JTable source = (JTable) e.getSource();
72                    int row = source.rowAtPoint(e.getPoint());
73                    int column = source.columnAtPoint(e.getPoint());
74
75                    if (!source.isRowSelected(row)) {
76                        source.changeSelection(row, column, false, false);
77                    }
78
79                    show(source, e.getX(), e.getY());
80                }
81            }
82        });
83        this.actions = actions;
84        this.actions = actions;
85    }
86}