View Javadoc

1   package com.explosion.expf.menusandtools.menu.popup;
2   
3   import java.awt.AWTEvent;
4   import java.awt.ActiveEvent;
5   import java.awt.Component;
6   import java.awt.event.MouseEvent;
7   import java.awt.event.MouseListener;
8   
9   
10  /* =============================================================================
11   *       
12   *     Copyright 2004 Stephen Cowx
13   *
14   *     Licensed under the Apache License, Version 2.0 (the "License");
15   *     you may not use this file except in compliance with the License.
16   *     You may obtain a copy of the License at
17   *
18   *     http://www.apache.org/licenses/LICENSE-2.0
19   *
20   *     Unless required by applicable law or agreed to in writing, software
21   *     distributed under the License is distributed on an "AS IS" BASIS,
22   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23   *     See the License for the specific language governing permissions and
24   *     limitations under the License.
25   * 
26   * =============================================================================
27   */
28  
29  /***
30   * @author Stephen Cowx
31   * Created on 04-Mar-2005
32   */
33  public class ExpPopupMouseListener implements MouseListener
34  {
35  
36      public void mouseClicked(MouseEvent event) {
37          mouseEvent(event);
38      }
39  
40      public void mouseEntered(MouseEvent event) {
41          mouseEvent(event);
42      }
43  
44      public void mouseExited(MouseEvent event) {
45          mouseEvent(event);
46      }
47  
48      public void mousePressed(MouseEvent event) {
49          mouseEvent(event);
50      }
51  
52      public void mouseReleased(MouseEvent event) {
53          mouseEvent(event);
54      }
55  
56      private void mouseEvent(MouseEvent event) {
57          dispatchPopup(event);
58      }
59      
60      /*** Dispatches popup events to ancestor implementing ExpPopupIntercepter. */
61      public static boolean dispatchPopup(AWTEvent event) {
62          if (!(event instanceof MouseEvent) || event instanceof ActiveEvent) { return false; }
63          MouseEvent mouseEvent = (MouseEvent) event;
64          if (!mouseEvent.isPopupTrigger()) { return false; }
65          Object src = event.getSource();
66          if (!(src instanceof Component)) { return false; }
67          Component component = (Component) src;
68          while (component != null) {
69              if (component.isFocusOwner() && component instanceof ExpPopupIntercepter) {
70                  ExpPopupIntercepter intercepter = (ExpPopupIntercepter) component;
71                  intercepter.popupEvent(mouseEvent);
72                  mouseEvent.consume();
73                  return true;
74              }
75              component = component.getParent();
76          }
77          return false;
78      }
79  
80  }