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

/src/mpv5/ui/popups/FileTablePopUp.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 180 lines | 110 code | 28 blank | 42 comment | 5 complexity | 0ee8c6d4b31036bcf2ce6792f8ce3c0d 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 java.io.File;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javax.swing.JMenuItem;
  27. import javax.swing.JPopupMenu;
  28. import javax.swing.JTable;
  29. import mpv5.db.common.Context;
  30. import mpv5.db.common.QueryHandler;
  31. import mpv5.globals.Messages;
  32. import mpv5.ui.frames.MPView;
  33. import mpv5.ui.panels.DataPanel;
  34. import mpv5.utils.files.FileDirectoryHandler;
  35. /**
  36. *
  37. *
  38. */
  39. public class FileTablePopUp extends JPopupMenu {
  40. private static final long serialVersionUID = 1L;
  41. private static MouseListener pop;
  42. private static FileTablePopUp p;
  43. /**
  44. *
  45. * @param t
  46. * @return
  47. */
  48. public static FileTablePopUp instanceOf(final JTable t) {
  49. if (p == null) {
  50. p = FileTablePopUp.getDefaultPopupMenu(t);
  51. }
  52. return p;
  53. }
  54. private static FileTablePopUp getDefaultPopupMenu(final JTable dataTable) {
  55. FileTablePopUp t = new FileTablePopUp();
  56. t.addDefaultPopupMenu(dataTable);
  57. return t;
  58. }
  59. /**
  60. * Adds a popup menu to the given table
  61. * @param items
  62. */
  63. public FileTablePopUp(JMenuItem[] items) {
  64. super();
  65. for (int i = 0; i < items.length; i++) {
  66. JMenuItem item = items[i];
  67. item.setHorizontalTextPosition(JMenuItem.LEFT);
  68. this.add(item);
  69. }
  70. }
  71. /**
  72. * Adds a Popup menu with default actions (delete, open, edit) to the given table
  73. *
  74. * @param dataTable A table holding File Objects, first column MUST be the ID, <br/>
  75. * second column the desired file name
  76. */
  77. public void addDefaultPopupMenu(final JTable dataTable) {
  78. JMenuItem x = new JMenuItem(Messages.SAVE_AS.getValue());
  79. x.addActionListener(new ActionListener() {
  80. @Override
  81. public void actionPerformed(ActionEvent e) {
  82. FileDirectoryHandler.save(QueryHandler.instanceOf().clone(Context.getFiles()).
  83. retrieveFile(dataTable.getModel().getValueAt(dataTable.getSelectedRow(), 0).
  84. toString(), new File(FileDirectoryHandler.getTempDir() + dataTable.getModel().
  85. getValueAt(dataTable.getSelectedRow(), 1).toString())));
  86. }
  87. });
  88. JMenuItem i = new JMenuItem(Messages.ACTION_OPEN.getValue());
  89. i.addActionListener(new ActionListener() {
  90. @Override
  91. public void actionPerformed(ActionEvent e) {
  92. FileDirectoryHandler.open(QueryHandler.instanceOf().clone(Context.getFiles()).
  93. retrieveFile(dataTable.getModel().getValueAt(dataTable.getSelectedRow(), 0).
  94. toString(), new File(FileDirectoryHandler.getTempDir() + dataTable.getModel().
  95. getValueAt(dataTable.getSelectedRow(), 1).toString())));
  96. }
  97. });
  98. JMenuItem h = new JMenuItem(Messages.ACTION_EDIT.getValue());
  99. h.addActionListener(new ActionListener() {
  100. @Override
  101. public void actionPerformed(ActionEvent e) {
  102. FileDirectoryHandler.edit(QueryHandler.instanceOf().clone(Context.getFiles()).
  103. retrieveFile(dataTable.getModel().getValueAt(dataTable.getSelectedRow(), 0).
  104. toString(), new File(FileDirectoryHandler.getTempDir() + dataTable.getModel().
  105. getValueAt(dataTable.getSelectedRow(), 1).toString())));
  106. }
  107. });
  108. JMenuItem g = new JMenuItem(Messages.ACTION_DELETE.getValue());
  109. g.addActionListener(new ActionListener() {
  110. @Override
  111. public void actionPerformed(ActionEvent e) {
  112. try {
  113. QueryHandler.instanceOf().clone(Context.getFiles()).removeFile(dataTable.getModel().getValueAt(dataTable.getSelectedRow(), 0).toString());
  114. ((DataPanel) mpv5.YabsViewProxy.instance().getCurrentTab()).refresh();
  115. } catch (Exception ex) {
  116. mpv5.logging.Log.Debug(ex);//Logger.getLogger(FileTablePopUp.class.getName()).log(Level.SEVERE, null, ex);
  117. }
  118. }
  119. });
  120. this.add(x);
  121. this.add(i);
  122. this.add(h);
  123. this.add(g);
  124. }
  125. /**
  126. * Remove the previously added Popup Mouselistener, if any
  127. * @param to
  128. */
  129. public static void clear(final JTable to) {
  130. to.removeMouseListener(pop);
  131. }
  132. /**
  133. * Create a new, empty popup
  134. */
  135. public FileTablePopUp() {
  136. super();
  137. }
  138. class MousePopupListener extends MouseAdapter {
  139. @Override
  140. public void mouseReleased(MouseEvent e) {
  141. if (e.isPopupTrigger()) {
  142. JTable source = (JTable) e.getSource();
  143. int row = source.rowAtPoint(e.getPoint());
  144. int column = source.columnAtPoint(e.getPoint());
  145. if (!source.isRowSelected(row)) {
  146. source.changeSelection(row, column, false, false);
  147. }
  148. show(source, e.getX(), e.getY());
  149. }
  150. }
  151. }
  152. }