source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/PopupMenuLauncher.java@ 8002

Last change on this file since 8002 was 7001, checked in by Don-vip, 10 years ago

see #8465 - switch core to Java 7

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.Component;
5import java.awt.Point;
6import java.awt.event.FocusEvent;
7import java.awt.event.FocusListener;
8import java.awt.event.MouseAdapter;
9import java.awt.event.MouseEvent;
10
11import javax.swing.JList;
12import javax.swing.JPopupMenu;
13import javax.swing.JTable;
14import javax.swing.JTree;
15import javax.swing.SwingUtilities;
16import javax.swing.tree.TreePath;
17
18/**
19 * Utility class that helps to display popup menus on mouse events.
20 * @since 2688
21 */
22public 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 public void mousePressed(MouseEvent e) { processEvent(e); }
55 @Override public void mouseClicked(MouseEvent e) {}
56 @Override public void mouseReleased(MouseEvent e) { processEvent(e); }
57
58 private void processEvent(MouseEvent e) {
59 if (e.isPopupTrigger() && (!checkEnabled || e.getComponent().isEnabled())) {
60 launch(e);
61 }
62 }
63
64 /**
65 * Launches the popup menu according to the given mouse event.
66 * This method needs to be overriden if the default constructor has been called.
67 * @param evt A mouse event
68 */
69 public void launch(final MouseEvent evt) {
70 if (evt != null) {
71 final Component component = evt.getComponent();
72 if (checkSelection(component, evt.getPoint())) {
73 checkFocusAndShowMenu(component, evt);
74 }
75 }
76 }
77
78 protected boolean checkSelection(Component component, Point p) {
79 if (component instanceof JList) {
80 return checkListSelection((JList<?>) component, p) > -1;
81 } else if (component instanceof JTable) {
82 return checkTableSelection((JTable) component, p) > -1;
83 } else if (component instanceof JTree) {
84 return checkTreeSelection((JTree) component, p) != null;
85 }
86 return true;
87 }
88
89 protected void checkFocusAndShowMenu(final Component component, final MouseEvent evt) {
90 if (component != null && component.isFocusable() && !component.hasFocus() && component.requestFocusInWindow()) {
91 component.addFocusListener(new FocusListener() {
92 @Override public void focusLost(FocusEvent e) {}
93 @Override public void focusGained(FocusEvent e) {
94 showMenu(evt);
95 component.removeFocusListener(this);
96 }
97 });
98 } else {
99 showMenu(evt);
100 }
101 }
102
103 protected void showMenu(MouseEvent evt) {
104 if (menu != null && evt != null) {
105 menu.show(evt.getComponent(), evt.getX(), evt.getY());
106 }
107 }
108
109 protected int checkListSelection(JList<?> list, Point p) {
110 int idx = list.locationToIndex(p);
111 if (idx >= 0 && idx < list.getModel().getSize() && list.getSelectedIndices().length < 2 && !list.isSelectedIndex(idx)) {
112 list.setSelectedIndex(idx);
113 }
114 return idx;
115 }
116
117 protected int checkTableSelection(JTable table, Point p) {
118 int row = table.rowAtPoint(p);
119 if (row >= 0 && row < table.getRowCount() && table.getSelectedRowCount() < 2 && table.getSelectedRow() != row) {
120 table.getSelectionModel().setSelectionInterval(row, row);
121 }
122 return row;
123 }
124
125 protected TreePath checkTreeSelection(JTree tree, Point p) {
126 TreePath path = tree.getPathForLocation(p.x, p.y);
127 if (path != null && tree.getSelectionCount() < 2 && !tree.isPathSelected(path)) {
128 tree.setSelectionPath(path);
129 }
130 return path;
131 }
132
133 protected static boolean isDoubleClick(MouseEvent e) {
134 return e != null && SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2;
135 }
136
137 /**
138 * @return the popup menu if defined, {@code null} otherwise.
139 * @since 5884
140 */
141 public final JPopupMenu getMenu() {
142 return menu;
143 }
144}
Note: See TracBrowser for help on using the repository browser.