PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/ui/popups/DOTablePopUp.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 130 lines | 79 code | 17 blank | 34 comment | 6 complexity | d4be170022eb93fcab212c270f5f127c 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.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.awt.event.MouseAdapter;
  21. import java.awt.event.MouseEvent;
  22. import java.awt.event.MouseListener;
  23. import javax.swing.JMenuItem;
  24. import javax.swing.JPopupMenu;
  25. import javax.swing.JTable;
  26. import mpv5.db.common.Context;
  27. import mpv5.db.common.DatabaseObject;
  28. import mpv5.db.common.NodataFoundException;
  29. import mpv5.globals.Messages;
  30. import mpv5.logging.Log;
  31. import mpv5.ui.frames.MPView;
  32. /**
  33. *
  34. *
  35. */
  36. public class DOTablePopUp extends JPopupMenu {
  37. private static final long serialVersionUID = 1L;
  38. private JTable source;
  39. private Context c;
  40. private Context context;
  41. private MouseListener pop;
  42. /**
  43. * Adds a popup menu to the given table
  44. * @param source
  45. * @param items
  46. * @param c
  47. * @param withMouseListener
  48. */
  49. public DOTablePopUp(JTable source, JMenuItem[] items, Context c, boolean withMouseListener) {
  50. super();
  51. this.source = source;
  52. this.context = c;
  53. for (int i = 0; i < items.length; i++) {
  54. JMenuItem item = items[i];
  55. item.setHorizontalTextPosition(JMenuItem.LEFT);
  56. this.add(item);
  57. }
  58. if (withMouseListener) {
  59. pop = new MousePopupListener();
  60. addMouseListener(pop);
  61. }
  62. this.c = c;
  63. }
  64. /**
  65. * Adds a Popup menu with default action (delete, open) to the given table
  66. *
  67. * @param to A table holding DatabaseObjects, first column MUST be the ID
  68. * @param c The context of the DatabaeObjects
  69. * @param withMouseListener
  70. */
  71. public static JPopupMenu addDefaultPopupMenu(final JTable to, final Context c, boolean withMouseListener) {
  72. JMenuItem i = new JMenuItem(Messages.ACTION_DELETE.getValue());
  73. i.addActionListener(new ActionListener() {
  74. @Override
  75. public void actionPerformed(ActionEvent e) {
  76. try {
  77. DatabaseObject.getObject(c, Integer.valueOf(to.getModel().getValueAt(to.getSelectedRow(), 0).toString())).delete();
  78. } catch (NodataFoundException ex) {
  79. Log.Debug(DOTablePopUp.class, ex);
  80. }
  81. }
  82. });
  83. JMenuItem i2 = new JMenuItem(Messages.ACTION_OPEN.getValue());
  84. i2.addActionListener(new ActionListener() {
  85. public void actionPerformed(ActionEvent e) {
  86. try {
  87. mpv5.YabsViewProxy.instance().getIdentifierView().addTab(DatabaseObject.getObject(c, Integer.valueOf(to.getModel().getValueAt(to.getSelectedRow(), 0).toString())));
  88. } catch (NodataFoundException ex) {
  89. Log.Debug(DOTablePopUp.class, ex);
  90. }
  91. }
  92. });
  93. JMenuItem i3 = new JMenuItem(Messages.ACTION_ADDLIST.getValue());
  94. i3.addActionListener(new ActionListener() {
  95. public void actionPerformed(ActionEvent e) {
  96. try {
  97. mpv5.YabsViewProxy.instance().addToClipBoard(DatabaseObject.getObject(c, Integer.valueOf(to.getModel().getValueAt(to.getSelectedRow(), 0).toString())));
  98. } catch (NodataFoundException ex) {
  99. Log.Debug(DOTablePopUp.class, ex);
  100. }
  101. }
  102. });
  103. return new DOTablePopUp(to, new JMenuItem[]{i, i2, i3}, c,withMouseListener);
  104. }
  105. class MousePopupListener extends MouseAdapter {
  106. @Override
  107. public void mouseClicked(MouseEvent e) {
  108. if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 1) {
  109. show(source, e.getX(), e.getY());
  110. }
  111. }
  112. }
  113. }