1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Point;
|
---|
6 | import java.awt.event.FocusAdapter;
|
---|
7 | import java.awt.event.FocusEvent;
|
---|
8 | import java.awt.event.MouseAdapter;
|
---|
9 | import java.awt.event.MouseEvent;
|
---|
10 |
|
---|
11 | import javax.swing.JList;
|
---|
12 | import javax.swing.JPopupMenu;
|
---|
13 | import javax.swing.JTable;
|
---|
14 | import javax.swing.JTree;
|
---|
15 | import javax.swing.SwingUtilities;
|
---|
16 | import javax.swing.tree.TreePath;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Utility class that helps to display popup menus on mouse events.
|
---|
20 | * @since 2688
|
---|
21 | */
|
---|
22 | public class PopupMenuLauncher extends MouseAdapter {
|
---|
23 | protected JPopupMenu menu;
|
---|
24 | private final boolean checkEnabled;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Creates a new {@link PopupMenuLauncher} with no defined menu.
|
---|
28 | * It is then needed to override the {@link #launch} method.
|
---|
29 | * @see #launch(MouseEvent)
|
---|
30 | */
|
---|
31 | public PopupMenuLauncher() {
|
---|
32 | this(null);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Creates a new {@link PopupMenuLauncher} with the given menu.
|
---|
37 | * @param menu The popup menu to display
|
---|
38 | */
|
---|
39 | public PopupMenuLauncher(JPopupMenu menu) {
|
---|
40 | this(menu, false);
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Creates a new {@link PopupMenuLauncher} with the given menu.
|
---|
45 | * @param menu The popup menu to display
|
---|
46 | * @param checkEnabled if {@code true}, the popup menu will only be displayed if the component triggering the mouse event is enabled
|
---|
47 | * @since 5886
|
---|
48 | */
|
---|
49 | public PopupMenuLauncher(JPopupMenu menu, boolean checkEnabled) {
|
---|
50 | this.menu = menu;
|
---|
51 | this.checkEnabled = checkEnabled;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void mousePressed(MouseEvent e) {
|
---|
56 | processEvent(e);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void mouseReleased(MouseEvent e) {
|
---|
61 | processEvent(e);
|
---|
62 | }
|
---|
63 |
|
---|
64 | private void processEvent(MouseEvent e) {
|
---|
65 | if (e.isPopupTrigger() && (!checkEnabled || e.getComponent().isEnabled())) {
|
---|
66 | launch(e);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Launches the popup menu according to the given mouse event.
|
---|
72 | * This method needs to be overriden if the default constructor has been called.
|
---|
73 | * @param evt A mouse event
|
---|
74 | */
|
---|
75 | public void launch(final MouseEvent evt) {
|
---|
76 | if (evt != null) {
|
---|
77 | final Component component = evt.getComponent();
|
---|
78 | if (checkSelection(component, evt.getPoint())) {
|
---|
79 | checkFocusAndShowMenu(component, evt);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected boolean checkSelection(Component component, Point p) {
|
---|
85 | if (component instanceof JList) {
|
---|
86 | return checkListSelection((JList<?>) component, p) > -1;
|
---|
87 | } else if (component instanceof JTable) {
|
---|
88 | return checkTableSelection((JTable) component, p) > -1;
|
---|
89 | } else if (component instanceof JTree) {
|
---|
90 | return checkTreeSelection((JTree) component, p) != null;
|
---|
91 | }
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected void checkFocusAndShowMenu(final Component component, final MouseEvent evt) {
|
---|
96 | if (component != null && component.isFocusable() && !component.hasFocus() && component.requestFocusInWindow()) {
|
---|
97 | component.addFocusListener(new FocusAdapter() {
|
---|
98 | @Override
|
---|
99 | public void focusGained(FocusEvent e) {
|
---|
100 | showMenu(evt);
|
---|
101 | component.removeFocusListener(this);
|
---|
102 | }
|
---|
103 | });
|
---|
104 | } else {
|
---|
105 | showMenu(evt);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected void showMenu(MouseEvent evt) {
|
---|
110 | if (menu != null && evt != null) {
|
---|
111 | menu.show(evt.getComponent(), evt.getX(), evt.getY());
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected int checkListSelection(JList<?> list, Point p) {
|
---|
116 | int idx = list.locationToIndex(p);
|
---|
117 | if (idx >= 0 && idx < list.getModel().getSize() && list.getSelectedIndices().length < 2 && !list.isSelectedIndex(idx)) {
|
---|
118 | list.setSelectedIndex(idx);
|
---|
119 | }
|
---|
120 | return idx;
|
---|
121 | }
|
---|
122 |
|
---|
123 | protected int checkTableSelection(JTable table, Point p) {
|
---|
124 | int row = table.rowAtPoint(p);
|
---|
125 | if (row >= 0 && row < table.getRowCount() && table.getSelectedRowCount() < 2 && table.getSelectedRow() != row) {
|
---|
126 | table.getSelectionModel().setSelectionInterval(row, row);
|
---|
127 | }
|
---|
128 | return row;
|
---|
129 | }
|
---|
130 |
|
---|
131 | protected TreePath checkTreeSelection(JTree tree, Point p) {
|
---|
132 | TreePath path = tree.getPathForLocation(p.x, p.y);
|
---|
133 | if (path != null && tree.getSelectionCount() < 2 && !tree.isPathSelected(path)) {
|
---|
134 | tree.setSelectionPath(path);
|
---|
135 | }
|
---|
136 | return path;
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected static boolean isDoubleClick(MouseEvent e) {
|
---|
140 | return e != null && SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * @return the popup menu if defined, {@code null} otherwise.
|
---|
145 | * @since 5884
|
---|
146 | */
|
---|
147 | public final JPopupMenu getMenu() {
|
---|
148 | return menu;
|
---|
149 | }
|
---|
150 | }
|
---|