1 | package org.expeditee.gui;
|
---|
2 |
|
---|
3 | import java.awt.AWTEvent;
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Container;
|
---|
6 | import java.awt.Point;
|
---|
7 | import java.awt.Toolkit;
|
---|
8 | import java.awt.event.AWTEventListener;
|
---|
9 | import java.awt.event.MouseEvent;
|
---|
10 | import java.awt.event.MouseWheelEvent;
|
---|
11 |
|
---|
12 | import javax.swing.JComponent;
|
---|
13 | import javax.swing.JMenuBar;
|
---|
14 | import javax.swing.JPopupMenu;
|
---|
15 | import javax.swing.SwingUtilities;
|
---|
16 |
|
---|
17 | import org.expeditee.items.InteractiveWidget;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * The gateway for mouse input; conditionally forwards mouse messages to swing
|
---|
21 | * components / expeditee frames for the Browser
|
---|
22 | *
|
---|
23 | * @author Brook Novak
|
---|
24 | *
|
---|
25 | */
|
---|
26 | public class MouseEventRouter extends JComponent implements AWTEventListener {
|
---|
27 |
|
---|
28 | private static final long serialVersionUID = 1L;
|
---|
29 |
|
---|
30 | private JMenuBar _menuBar;
|
---|
31 |
|
---|
32 | private Container _contentPane;
|
---|
33 |
|
---|
34 | public MouseEventRouter(JMenuBar menuBar, Container contentPane) {
|
---|
35 |
|
---|
36 | if (contentPane == null)
|
---|
37 | throw new NullPointerException("contentPane");
|
---|
38 |
|
---|
39 | // Listen for all AWT events (ensures popups are included)
|
---|
40 | Toolkit.getDefaultToolkit().addAWTEventListener(
|
---|
41 | this,
|
---|
42 | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK
|
---|
43 | | AWTEvent.MOUSE_WHEEL_EVENT_MASK);
|
---|
44 |
|
---|
45 | this._menuBar = menuBar;
|
---|
46 | this._contentPane = contentPane;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * All events for every component in frame are fired to here
|
---|
51 | */
|
---|
52 | public void eventDispatched(AWTEvent event) {
|
---|
53 | if (event instanceof MouseEvent) {
|
---|
54 | routeMouseEvent((MouseEvent) event);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Forwards the mouse event to its appropriate destination. For events that
|
---|
60 | * belong to Expiditee space the events are consumed and manually routed to
|
---|
61 | * the mouse actions handler.
|
---|
62 | *
|
---|
63 | * @param e
|
---|
64 | * The mouse event for any component in the browser frame
|
---|
65 | */
|
---|
66 | private void routeMouseEvent(MouseEvent e) {
|
---|
67 |
|
---|
68 | // First convert the point to expeditee space
|
---|
69 | Point containerPoint = SwingUtilities.convertPoint(e.getComponent(), e
|
---|
70 | .getPoint(), _contentPane);
|
---|
71 |
|
---|
72 | if (containerPoint.y < 0) { // not in the content pane
|
---|
73 |
|
---|
74 | if (_menuBar != null
|
---|
75 | && containerPoint.y + _menuBar.getHeight() >= 0) {
|
---|
76 | // The mouse event is over the menu bar.
|
---|
77 | // Could handle specially.
|
---|
78 | } else {
|
---|
79 | // The mouse event is over non-system window
|
---|
80 | // decorations, such as the ones provided by
|
---|
81 | // the Java look and feel.
|
---|
82 | // Could handle specially.
|
---|
83 | }
|
---|
84 |
|
---|
85 | } else {
|
---|
86 |
|
---|
87 | // Check to see if the mouse is over an expeditee item or
|
---|
88 | // whether an expeditee item is currently picked up
|
---|
89 | boolean forwardToExpiditee = false;
|
---|
90 |
|
---|
91 | // Note: all frame.content pane events belong to expeditee
|
---|
92 | if (e.getSource() == _contentPane
|
---|
93 | || e.getSource() == Browser._theBrowser
|
---|
94 | || Frame.itemAttachedToCursor()) {
|
---|
95 | forwardToExpiditee = true;
|
---|
96 | } else if (DisplayIO.getCurrentFrame() != null) { // is mouse over
|
---|
97 | // a specific
|
---|
98 | // expeditee
|
---|
99 | // item?
|
---|
100 | // NOTE: Do not use FrameUtils.getCurrentItem() - thats relevent
|
---|
101 | // for old mouse position only
|
---|
102 | /*
|
---|
103 | * for (Item i : DisplayIO.getCurrentFrame().getItems()) { if
|
---|
104 | * (i.getPolygon().contains(containerPoint)) {
|
---|
105 | * forwardToExpiditee = true; break; } }
|
---|
106 | */// ABOVE: Does not consider overlays
|
---|
107 | // NOTE: Below is an expensive operation and could be re-used
|
---|
108 | // when passing mouse events!!!
|
---|
109 | forwardToExpiditee = (FrameUtils.onItem(DisplayIO
|
---|
110 | .getCurrentFrame(), containerPoint.x, containerPoint.y) != null);
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (forwardToExpiditee) {
|
---|
114 |
|
---|
115 | // If forwarding to Expiditee ensure that widgets highlighting
|
---|
116 | // is enabled
|
---|
117 | // do give visual feedback yo users such that Expiditee has
|
---|
118 | // focus.
|
---|
119 | InteractiveWidget.enableExepiteeHighlighting(true);
|
---|
120 |
|
---|
121 | // Ensure that underlying widgets don't get the event
|
---|
122 | e.consume();
|
---|
123 |
|
---|
124 | // Create artificial mouse event and forward it to expeditee
|
---|
125 | MouseEvent expediteeEvent = SwingUtilities.convertMouseEvent(e
|
---|
126 | .getComponent(), e, _contentPane);
|
---|
127 |
|
---|
128 | switch (e.getID()) {
|
---|
129 | case MouseEvent.MOUSE_MOVED:
|
---|
130 | FrameMouseActions.getInstance().mouseMoved(expediteeEvent);
|
---|
131 |
|
---|
132 | // Ensure that expiditee has focus only if no popups exist
|
---|
133 | if (!Browser._theBrowser.getContentPane().isFocusOwner()
|
---|
134 | && !isPopupVisible()) {
|
---|
135 | Browser._theBrowser.getContentPane().requestFocus();
|
---|
136 | }
|
---|
137 |
|
---|
138 | break;
|
---|
139 | case MouseEvent.MOUSE_CLICKED:
|
---|
140 | FrameMouseActions.getInstance()
|
---|
141 | .mouseClicked(expediteeEvent);
|
---|
142 | break;
|
---|
143 | case MouseEvent.MOUSE_PRESSED:
|
---|
144 | FrameMouseActions.getInstance().ProccessMousePressedEvent(
|
---|
145 | expediteeEvent, e.getModifiersEx());
|
---|
146 | break;
|
---|
147 | case MouseEvent.MOUSE_RELEASED:
|
---|
148 | FrameMouseActions.getInstance().mouseReleased(
|
---|
149 | expediteeEvent);
|
---|
150 | break;
|
---|
151 | case MouseEvent.MOUSE_WHEEL:
|
---|
152 | FrameMouseActions.getInstance().mouseWheelMoved(
|
---|
153 | (MouseWheelEvent) expediteeEvent);
|
---|
154 | break;
|
---|
155 | case MouseEvent.MOUSE_ENTERED:
|
---|
156 | FrameMouseActions.getInstance()
|
---|
157 | .mouseEntered(expediteeEvent);
|
---|
158 | break;
|
---|
159 | case MouseEvent.MOUSE_EXITED:
|
---|
160 | FrameMouseActions.getInstance().mouseExited(expediteeEvent);
|
---|
161 | break;
|
---|
162 | case MouseEvent.MOUSE_DRAGGED:
|
---|
163 | FrameMouseActions.getInstance()
|
---|
164 | .mouseDragged(expediteeEvent);
|
---|
165 | break;
|
---|
166 | }
|
---|
167 |
|
---|
168 | } else {
|
---|
169 |
|
---|
170 | // If forwarding to swing ensure that widgets are not
|
---|
171 | // highlighted
|
---|
172 | // to give visual feedback yo users such that swing has focus.
|
---|
173 | InteractiveWidget.enableExepiteeHighlighting(false);
|
---|
174 |
|
---|
175 | // Also bring expideditee behaviour to swing: Auto-focus on
|
---|
176 | // component
|
---|
177 | // whenever the mouse moves over it.
|
---|
178 | if (e.getID() == MouseEvent.MOUSE_MOVED) {
|
---|
179 | if (e.getSource() instanceof Component) {
|
---|
180 | Component target = (Component) e.getSource();
|
---|
181 | if (!target.isFocusOwner()) {
|
---|
182 | target.requestFocus();
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | public static boolean isPopupVisible() {
|
---|
191 | return isPopupVisible(Browser._theBrowser.getLayeredPane());
|
---|
192 | }
|
---|
193 |
|
---|
194 | private static boolean isPopupVisible(Container parent) {
|
---|
195 |
|
---|
196 | for (Component c : parent.getComponents()) {
|
---|
197 |
|
---|
198 | if (c instanceof JPopupMenu && ((JPopupMenu) c).isVisible()) {
|
---|
199 | return true;
|
---|
200 |
|
---|
201 | } else if (c instanceof Container
|
---|
202 | && c != Browser._theBrowser.getContentPane()) {
|
---|
203 | if (isPopupVisible((Container) c))
|
---|
204 | return true;
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | return false;
|
---|
209 | }
|
---|
210 |
|
---|
211 | } |
---|