KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > events > PopupListener


1 /*
2  * PopupListener, general purpose popup trigger that connects JPopupMenus to mouse events.
3  * Copyright (C) Achim Westermann, created on 10.12.2004, 13:48:55
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  *
22  */

23 package info.monitorenter.gui.chart.events;
24
25 import java.awt.event.MouseAdapter JavaDoc;
26 import java.awt.event.MouseEvent JavaDoc;
27
28 import javax.swing.JPopupMenu JavaDoc;
29
30 /**
31  * A general purpose <code>PopupListener</code>.
32  * <p>
33  * It is used to connect <code>JPopupMenu</code> instances with the components
34  * retrieved from the factory methods (e.g.
35  * {@link info.monitorenter.gui.chart.layout.LayoutFactory#createContextMenuLable
36  * (info.monitorenter.gui.chart.Chart2D)}).
37  * <p>
38  *
39  * Note that instances have to be registered as a listener on components via
40  * {@link java.awt.Component#addMouseListener(java.awt.event.MouseListener)} to
41  * make it working.
42  * <p>
43  *
44  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
45  *
46  * @version $Revision: 1.1 $
47  */

48 public final class PopupListener extends MouseAdapter JavaDoc {
49   /** The popup to open. */
50   private JPopupMenu JavaDoc m_popup;
51
52   /**
53    * Creates an instance that will show the given popup upon a right mouse click
54    * on a {@link javax.swing.JComponent} this instance will be registered as
55    * listener to.
56    * <p>
57    *
58    * @param popup
59    * the popup to show upon a right mouse click on a
60    * {@link javax.swing.JComponent} this instance will be registered as
61    * listener to.
62    *
63    * @see java.awt.Component#addMouseListener(java.awt.event.MouseListener)
64    */

65   public PopupListener(final JPopupMenu JavaDoc popup) {
66     this.m_popup = popup;
67   }
68
69   /**
70    * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
71    */

72   public void mousePressed(final MouseEvent JavaDoc me) {
73     this.maybeShopwPopup(me);
74   }
75
76   /**
77    * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
78    */

79   public void mouseReleased(final MouseEvent JavaDoc me) {
80     this.maybeShopwPopup(me);
81   }
82
83   /**
84    * Helper that triggers the popup display in a system - dependant manner.
85    * <p>
86    *
87    * On windows a right mouse click will trigger the popup display.
88    * <p>
89    *
90    * @param me
91    * the mouse event fired.
92    */

93   private void maybeShopwPopup(final MouseEvent JavaDoc me) {
94     if (me.isPopupTrigger()) {
95       this.m_popup.show(me.getComponent(), me.getX(), me.getY());
96     }
97   }
98
99   /**
100    * @return the popup menue.
101    */

102   public final JPopupMenu JavaDoc getPopup() {
103     return this.m_popup;
104   }
105 }
106
Popular Tags