001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package test.check;
031:
032: import java.awt.event.MouseAdapter;
033: import java.awt.event.MouseEvent;
034: import java.util.LinkedList;
035:
036: import javax.swing.*;
037: import javax.swing.event.PopupMenuEvent;
038: import javax.swing.event.PopupMenuListener;
039:
040: import test.Check;
041:
042: /**
043: * Popup listener for the test application.
044: *
045: * @author Kirill Grouchnikov
046: */
047: public class MousePopupListener extends MouseAdapter {
048: /**
049: * The owner component.
050: */
051: private JComponent owner;
052:
053: /**
054: * Creates a popup listener.
055: *
056: * @param owner
057: * The owner component.
058: */
059: public MousePopupListener(JComponent owner) {
060: this .owner = owner;
061: }
062:
063: /*
064: * (non-Javadoc)
065: *
066: * @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
067: */
068: public void mousePressed(MouseEvent e) {
069: checkPopup(e);
070: }
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
076: */
077: public void mouseClicked(MouseEvent e) {
078: checkPopup(e);
079: }
080:
081: /*
082: * (non-Javadoc)
083: *
084: * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
085: */
086: public void mouseReleased(MouseEvent e) {
087: checkPopup(e);
088: }
089:
090: /**
091: * Handles the mouse event, showing the popup menu as necessary.
092: *
093: * @param e
094: * Mouse event.
095: */
096: void checkPopup(MouseEvent e) {
097: if (e.isPopupTrigger()) {
098: JPopupMenu popup = new JPopupMenu();
099: // ActionListener menuListener = new ActionListener() {
100: // public void actionPerformed(ActionEvent event) {
101: // Check.out("Popup menu item [" + event.getActionCommand()
102: // + "] was pressed.");
103: // }
104: // };
105: int mcount = 0;
106: for (LinkedList<JMenuItem> miList : SampleMenuFactory
107: .getTestMenuItems()) {
108: if (mcount > 0) {
109: if (mcount % 2 == 0)
110: popup.addSeparator();
111: else
112: popup.add(new JSeparator());
113: }
114: for (JMenuItem menuItem : miList) {
115: popup.add(menuItem);
116: }
117: mcount++;
118: }
119: popup.addPopupMenuListener(new PopupPrintListener());
120:
121: popup.show(this .owner, e.getX(), e.getY());
122: }
123: }
124:
125: /**
126: * Custom popup listener.
127: *
128: * @author Kirill Grouchnikov
129: */
130: protected static class PopupPrintListener implements
131: PopupMenuListener {
132: /*
133: * (non-Javadoc)
134: *
135: * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent)
136: */
137: public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
138: Check.out("Popup menu will be visible!");
139: }
140:
141: /*
142: * (non-Javadoc)
143: *
144: * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent)
145: */
146: public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
147: Check.out("Popup menu will be invisible!");
148: }
149:
150: /*
151: * (non-Javadoc)
152: *
153: * @see javax.swing.event.PopupMenuListener#popupMenuCanceled(javax.swing.event.PopupMenuEvent)
154: */
155: public void popupMenuCanceled(PopupMenuEvent e) {
156: Check.out("Popup menu is hidden!");
157: }
158: }
159: }
|