001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: PopupListener.java$
021: * $FileID: 4009$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:10 PM$
026: * $Comment: Replaced GPL header with LGPL header.$
027: */
028:
029: package org.sourcejammer.client.gui.listener;
030:
031: import java.awt.event.MouseAdapter;
032: import java.awt.event.MouseEvent;
033: import javax.swing.JPopupMenu;
034: import java.awt.Component;
035:
036: /**
037: * Title: $FileName: PopupListener.java$
038: * @version $VerNum: 2$
039: * @author $AuthorName: Rob MacGrogan$<br><br>
040: *
041: * $Description: $<br>
042: * $KeyWordsOff: $
043: */
044: public class PopupListener extends MouseAdapter {
045:
046: private JPopupMenu mMenu = null;
047: private Component mComponent = null;
048: private boolean mbFireMousePressOnPopup = false;
049:
050: public PopupListener() {
051: super ();
052: }
053:
054: public PopupListener(JPopupMenu menu, Component component) {
055: this (menu, component, true);
056: }
057:
058: public PopupListener(JPopupMenu menu, Component component,
059: boolean fireMousePressOnPopup) {
060: mMenu = menu;
061: mComponent = component;
062: mbFireMousePressOnPopup = fireMousePressOnPopup;
063: }
064:
065: public void mousePressed(MouseEvent ev) {
066: showPopup(ev);
067: }
068:
069: public void mouseClicked(MouseEvent ev) {
070: showPopup(ev);
071: }
072:
073: public void mouseReleased(MouseEvent ev) {
074: showPopup(ev);
075: }
076:
077: protected void showPopup(MouseEvent ev) {
078: if (ev.isPopupTrigger()) {
079: if (mbFireMousePressOnPopup) {
080: //This fires the mouse press event to select whatever is under the
081: //popup trigger click.
082: MouseEvent evNew = new MouseEvent(getComponent(),
083: MouseEvent.MOUSE_PRESSED, new java.util.Date()
084: .getTime(), MouseEvent.BUTTON1_MASK, ev
085: .getX(), ev.getY(), 1, false);
086: getComponent().dispatchEvent(evNew);
087: }
088: getMenu().show(getComponent(), ev.getX(), ev.getY());
089: } else {
090: //Seems to screw up under Linux, so I'm removing it.
091: //getMenu().setVisible(false);
092: }
093: }
094:
095: public static void main(String[] args) {
096: try {
097: System.out.println("Button 1:" + MouseEvent.BUTTON1_MASK);
098: System.out.println("Button 2:" + MouseEvent.BUTTON2_MASK);
099: System.out.println("Button 3:" + MouseEvent.BUTTON3_MASK);
100: System.out.println("Click:" + MouseEvent.MOUSE_CLICKED);
101: System.out.println("Dragged:" + MouseEvent.MOUSE_DRAGGED);
102: System.out.println("Moved:" + MouseEvent.MOUSE_MOVED);
103: System.out.println("Pressed:" + MouseEvent.MOUSE_PRESSED);
104: } catch (Exception ex) {
105: ex.printStackTrace();
106: }
107: }
108:
109: /**
110: * Returns the mbFireMousePressOnPopup.
111: * @return boolean
112: */
113: public boolean isFireMousePressOnPopup() {
114: return mbFireMousePressOnPopup;
115: }
116:
117: /**
118: * Returns the Component.
119: * @return Component
120: */
121: public Component getComponent() {
122: return mComponent;
123: }
124:
125: /**
126: * Returns the Menu.
127: * @return JPopupMenu
128: */
129: public JPopupMenu getMenu() {
130: return mMenu;
131: }
132:
133: /**
134: * Sets the FireMousePressOnPopup.
135: * @param fireMousePressOnPopup The mbFireMousePressOnPopup to set
136: */
137: public void setFireMousePressOnPopup(boolean fireMousePressOnPopup) {
138: this .mbFireMousePressOnPopup = mbFireMousePressOnPopup;
139: }
140:
141: /**
142: * Sets the mjComponent.
143: * @param Component The Component to set
144: */
145: public void setComponent(Component component) {
146: this .mComponent = mComponent;
147: }
148:
149: /**
150: * Sets the Menu.
151: * @param menu The menu to set
152: */
153: public void setMenu(JPopupMenu menu) {
154: this.mMenu = mMenu;
155: }
156:
157: }
|