001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: /* Department, Ludwigsburg University of Education. All rights reserved.
025: *
026: * Redistribution and use in source and binary forms, with or without
027: * modification, are permitted provided that the following conditions
028: * are met:
029: *
030: * 1. Redistributions of source code must retain the above copyright notice,
031: * this list of conditions and the following disclaimer.
032: *
033: * 2. Redistributions in binary form must reproduce the above copyright
034: * notice, this list of conditions and the following disclaimer in the
035: * documentation and/or other materials provided with the distribution.
036: *
037: * 3. The end-user documentation included with the redistribution, if any,
038: * must include the following acknowledgment:
039: * "This product includes software developed by the Applied Computer
040: * Science Research Group, Darmstadt University of Technology
041: * (http://www.pi.informatik.tu-darmstadt.de), and the Mathematics &
042: * Computer Science Department, Ludwigsburg University of Education
043: * (http://www.ph-ludwigsburg.de/mathematik/)."
044: * Alternately, this acknowledgment may appear in the software itself,
045: * if and wherever such third-party acknowledgments normally appear.
046: *
047: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
048: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
049: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
050: * THE APPLIED COMPUTER SCIENCE RESEARCH GROUP, DARMSTADT UNIVERSITY OF
051: * TECHNOLOGY, AND THE MATHEMATICS & COMPUTER SCIENCE DEPARTMENT, LUDWIGSBURG
052: * UNIVERSITY OF EDUCATION, OR THEIR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
053: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
054: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
055: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
056: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
057: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
058: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
059: */
060: package jacareto.toolkit.swing;
061:
062: import java.awt.event.MouseAdapter;
063: import java.awt.event.MouseEvent;
064:
065: import javax.swing.JPopupMenu;
066:
067: /**
068: * A listener for popup menus.
069: *
070: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
071: * @version 1.0
072: */
073: public class PopupListener extends MouseAdapter {
074: /** The popup menu. */
075: private JPopupMenu popup;
076:
077: /**
078: * Creates a new Popup listener instance with the given popup menu
079: *
080: * @param popup the popup menu
081: */
082: public PopupListener(JPopupMenu popup) {
083: this .popup = popup;
084: }
085:
086: public void mousePressed(MouseEvent e) {
087: maybeShowPopup(e);
088: }
089:
090: public void mouseReleased(MouseEvent e) {
091: maybeShowPopup(e);
092: }
093:
094: private void maybeShowPopup(MouseEvent e) {
095: if (e.isPopupTrigger()) {
096: updateMenuItems(e);
097: popup.setLightWeightPopupEnabled(false);
098: popup.show(e.getComponent(), e.getX(), e.getY());
099: }
100: }
101:
102: /**
103: * This method can be overwritten in subclasses. It is called before the menu pops up.
104: *
105: * @param e the event
106: */
107: public void updateMenuItems(MouseEvent e) {
108: }
109: }
|