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