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