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.Button;
6   import java.awt.Component;
7   import java.awt.Container;
8   import java.awt.EventQueue;
9   import java.awt.Frame;
10  import java.awt.MenuItem;
11  import java.awt.Panel;
12  import java.awt.PopupMenu;
13  import java.awt.TextField;
14  import java.awt.Toolkit;
15  import java.awt.event.AWTEventListener;
16  import java.awt.event.ContainerEvent;
17  import java.awt.event.ContainerListener;
18  import java.awt.event.MouseEvent;
19  import java.awt.event.MouseListener;
20  
21  import javax.swing.JButton;
22  import javax.swing.JFrame;
23  import javax.swing.JMenuItem;
24  import javax.swing.JPanel;
25  import javax.swing.JPopupMenu;
26  import javax.swing.JTextField;
27  
28  /***
29   * * Components that wish to intercept popup events * should implement this
30   * class.
31   */
32  interface PopupIntercepter {
33  
34      void popupEvent(MouseEvent event);
35  }
36  
37  class Utils {
38  
39      /*** Dispatches popup events to ancestor implementing ExpPopupIntercepter. */
40      public static boolean dispatchPopup(AWTEvent event) {
41          if (!(event instanceof MouseEvent) || event instanceof ActiveEvent) { return false; }
42          MouseEvent mouseEvent = (MouseEvent) event;
43          if (!mouseEvent.isPopupTrigger()) { return false; }
44          Object src = event.getSource();
45          if (!(src instanceof Component)) { return false; }
46          Component component = (Component) src;
47          while (component != null) {
48              if (component instanceof PopupIntercepter) {
49                  PopupIntercepter intercepter = (PopupIntercepter) component;
50                  intercepter.popupEvent(mouseEvent);
51                  mouseEvent.consume();
52                  return true;
53              }
54              component = component.getParent();
55          }
56          return false;
57      }
58  }
59  
60  class JPopupablePane extends JFrame implements PopupIntercepter {
61  
62      private final JPopupMenu popup;
63  
64      public JPopupablePane(String title) {
65          super(title);
66          popup = new JPopupMenu();
67          popup.add(new JMenuItem("Dogs"));
68          popup.add(new JMenuItem("Cats"));
69          popup.add(new JMenuItem("Mass Hysteria"));
70      }
71  
72      public void popupEvent(MouseEvent event) {
73          System.out.println("it's a popup");
74          popup.show(event.getComponent(), event.getX(), event.getY());
75      }
76  
77      public static JFrame create() {
78          final JFrame frame = new JPopupablePane("Right Click Test");
79          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
80          JButton button = new JButton("this is a button");
81          JTextField tf = new JTextField("this is a textfield");
82          JPanel panel = new JPanel();
83          panel.add(button);
84          panel.add(tf);
85          frame.getContentPane().add(panel); // pack and show the frame
86          frame.pack();
87          frame.setSize(400, 200);
88          frame.setVisible(true);
89          return frame;
90      }
91  }
92  
93  class PopupablePane extends Frame implements PopupIntercepter {
94  
95      private final PopupMenu popup;
96  
97      public PopupablePane(String title) {
98          super(title);
99          popup = new PopupMenu();
100         popup.add(new MenuItem("Dogs"));
101         popup.add(new MenuItem("Cats"));
102         popup.add(new MenuItem("Mass Hysteria"));
103         add(popup);
104     }
105 
106     public void popupEvent(MouseEvent event) {
107         System.out.println("it's a popup");
108         popup.show(event.getComponent(), event.getX(), event.getY());
109     }
110 
111     public static Frame create() {
112         final Frame frame = new PopupablePane("Right Click Test");
113         Button button = new Button("this is a button");
114         TextField tf = new TextField("this is a textfield");
115         Panel panel = new Panel();
116         panel.add(button);
117         panel.add(tf);
118         frame.add(panel); // pack and show the frame
119         frame.pack();
120         frame.setSize(400, 200);
121         frame.setVisible(true);
122         return frame;
123     }
124 }
125 
126 class NoRightClick {
127 
128     public static void main(String[] args) {
129         final JFrame light = JPopupablePane.create();
130         final Frame heavy = PopupablePane.create();
131     }
132 }
133 
134 public class MoreRightClick {
135 
136     public static void main(String[] args) {
137         Toolkit.getDefaultToolkit().getSystemEventQueue().push(new EventQueue() {
138 
139             protected void dispatchEvent(AWTEvent event) {
140                 if (!Utils.dispatchPopup(event)) {
141                     super.dispatchEvent(event);
142                 }
143             }
144         });
145         final JFrame light = JPopupablePane.create();
146         final Frame heavy = PopupablePane.create();
147     }
148 }
149 
150 class DispatchRightClick {
151 
152     public static void main(String[] args) {
153         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
154 
155             public void eventDispatched(AWTEvent event) {
156                 Utils.dispatchPopup(event);
157             }
158         }, AWTEvent.MOUSE_EVENT_MASK);
159         final JFrame light = JPopupablePane.create();
160         final Frame heavy = PopupablePane.create();
161     }
162 }
163 
164 class QuietRightClick {
165 
166     private MouseListener mouseListener = new MouseListener() {
167 
168         public void mouseClicked(MouseEvent event) {
169             mouseEvent(event);
170         }
171 
172         public void mouseEntered(MouseEvent event) {
173             mouseEvent(event);
174         }
175 
176         public void mouseExited(MouseEvent event) {
177             mouseEvent(event);
178         }
179 
180         public void mousePressed(MouseEvent event) {
181             mouseEvent(event);
182         }
183 
184         public void mouseReleased(MouseEvent event) {
185             mouseEvent(event);
186         }
187 
188         private void mouseEvent(MouseEvent event) {
189             Utils.dispatchPopup(event);
190         }
191     };
192 
193     private ContainerListener containerListener = new ContainerListener() {
194 
195         public void componentAdded(ContainerEvent event) {
196             setListen(event.getChild(), true);
197         }
198 
199         public void componentRemoved(ContainerEvent event) {
200             setListen(event.getChild(), false);
201         }
202     };
203 
204     public static void main(String[] args) {
205         final JFrame light = JPopupablePane.create();
206         final Frame heavy = PopupablePane.create();
207         QuietRightClick quiet = new QuietRightClick();
208         quiet.setListen(light, true);
209         quiet.setListen(heavy, true);
210     }
211 
212     private void setListen(Component c, boolean enable) {
213         if (c == null) { return; }
214         if (enable) {
215             c.addMouseListener(mouseListener);
216         } else {
217             c.removeMouseListener(mouseListener);
218         }
219         if (!(c instanceof Container)) { return; }
220         Container container = (Container) c; // Checks for components added or
221                                              // removed.
222         if (enable) {
223             container.addContainerListener(containerListener);
224         } else {
225             container.removeContainerListener(containerListener);
226         } // Listen to existing children! 
227         Component[] children =  container.getComponents(); 
228         final int num = children.length; 
229         //for   (int ct=0; ct.
230     }
231 }