PageRenderTime 36ms CodeModel.GetById 23ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  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. */
  17. package mpv5.ui.popups;
  18. import java.awt.event.ActionListener;
  19. import java.awt.event.MouseAdapter;
  20. import java.awt.event.MouseEvent;
  21. import java.awt.event.MouseListener;
  22. import javax.swing.JMenuItem;
  23. import javax.swing.JPopupMenu;
  24. import javax.swing.JSeparator;
  25. import javax.swing.JTable;
  26. /**
  27. */
  28. public class TablePopUp extends JPopupMenu {
  29. private ActionListener[] actions;
  30. /**
  31. * Adds a popup menu to the given table
  32. * @param table
  33. * @param Items
  34. * @param actions
  35. */
  36. public TablePopUp(JTable table, String[] Items, ActionListener[] actions) {
  37. super();
  38. JMenuItem[] items = new JMenuItem[Items.length];
  39. for (int j = 0; j < actions.length; j++) {
  40. JMenuItem i = null;
  41. if (actions[j] != null) {
  42. ActionListener actionListener = actions[j];
  43. i = new JMenuItem(Items[j]);
  44. i.addActionListener(actionListener);
  45. }
  46. items[j] = i;
  47. }
  48. for (int i = 0; i < items.length; i++) {
  49. JMenuItem item = items[i];
  50. if (item != null) {
  51. item.setHorizontalTextPosition(JMenuItem.LEFT);
  52. this.add(item);
  53. } else {
  54. add(new JSeparator(JSeparator.HORIZONTAL));
  55. }
  56. }
  57. table.addMouseListener(new MouseAdapter() {
  58. @Override
  59. public void mouseReleased(MouseEvent e) {
  60. if (e.getButton() == MouseEvent.BUTTON2 || e.getButton() == MouseEvent.BUTTON3) {
  61. JTable source = (JTable) e.getSource();
  62. int row = source.rowAtPoint(e.getPoint());
  63. int column = source.columnAtPoint(e.getPoint());
  64. if (!source.isRowSelected(row)) {
  65. source.changeSelection(row, column, false, false);
  66. }
  67. show(source, e.getX(), e.getY());
  68. }
  69. }
  70. });
  71. this.actions = actions;
  72. this.actions = actions;
  73. }
  74. }