View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    * 
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   ******************************************************************************/
11  package com.jsql.view.swing.popupmenu;
12  
13  import com.jsql.util.I18nUtil;
14  import com.jsql.view.swing.util.I18nViewUtil;
15  
16  import javax.swing.*;
17  import javax.swing.event.PopupMenuEvent;
18  import javax.swing.event.PopupMenuListener;
19  import java.awt.*;
20  import java.awt.event.ActionEvent;
21  import java.awt.event.InputEvent;
22  import java.awt.event.KeyEvent;
23  
24  /**
25   * Default popup menu and shortcuts for a table.
26   */
27  public class JPopupMenuTable extends JPopupMenu {
28      
29      /**
30       * Table with new menu and shortcut.
31       */
32      private final JTable table;
33  
34      /**
35       * Create popup menu for this table component.
36       * @param table The table receiving the menu
37       */
38      public JPopupMenuTable(JTable table) {
39          this.table = table;
40          table.setComponentPopupMenu(this);
41  
42          JMenuItem copyItem = new JMenuItem(new ActionCopy());
43          copyItem.setText(I18nUtil.valueByKey("CONTEXT_MENU_COPY"));
44          I18nViewUtil.addComponentForKey("CONTEXT_MENU_COPY", copyItem);
45          copyItem.setMnemonic('C');
46          copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK));
47  
48          JMenuItem selectAllItem = new JMenuItem(new ActionSelectAll());
49          selectAllItem.setText(I18nUtil.valueByKey("CONTEXT_MENU_SELECT_ALL"));
50          I18nViewUtil.addComponentForKey("CONTEXT_MENU_SELECT_ALL", selectAllItem);
51          selectAllItem.setMnemonic('A');
52          selectAllItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_DOWN_MASK));
53  
54          this.add(copyItem);
55          this.addSeparator();
56          this.add(selectAllItem);
57  
58          // Show menu next mouse pointer
59          this.addPopupMenuListener(new PopupMenuListener() {
60              @Override
61              public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
62                  // Fix #67773: NullPointerException on getLocation()
63                  if (MouseInfo.getPointerInfo() != null) {
64                      JPopupMenuTable.this.setLocation(MouseInfo.getPointerInfo().getLocation());
65                  }
66              }
67              @Override
68              public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
69                  // Do nothing
70              }
71              @Override
72              public void popupMenuCanceled(PopupMenuEvent e) {
73                  // Do nothing
74              }
75          });
76      }
77  
78      public JPopupMenuTable(JTable tableValues, Action actionShowSearchTable) {
79          this(tableValues);
80  
81          JMenuItem search = new JMenuItem();
82          search.setAction(actionShowSearchTable);
83          search.setText("Search...");
84          search.setMnemonic('S');
85          search.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_DOWN_MASK));
86          this.addSeparator();
87          this.add(search);
88      }
89  
90      /**
91       * An action for Select All shortcut.
92       */
93      private class ActionSelectAll extends AbstractAction {
94          @Override
95          public void actionPerformed(ActionEvent e) {
96              JPopupMenuTable.this.table.selectAll();
97          }
98      }
99  
100     /**
101      * An action for Copy shortcut.
102      */
103     private class ActionCopy extends AbstractAction {
104         @Override
105         public void actionPerformed(ActionEvent e) {
106             var copyEvent = new ActionEvent(JPopupMenuTable.this.table, ActionEvent.ACTION_PERFORMED, "copy");
107             JPopupMenuTable.this.table.getActionMap().get(copyEvent.getActionCommand()).actionPerformed(copyEvent);
108         }
109     }
110 }