Source code: com/yaftp/utils/SwingComponentPopup.java
1 /**
2 *
3 * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package com.yaftp.utils;
21
22 import java.awt.* ;
23 import java.awt.event.* ;
24 import javax.swing.* ;
25
26
27 /**
28
29 Adapt Lightweight popup SWING stuff
30 at construction time bind given component with
31 new created popup and accept later submemus
32 dynamically thru addMenuItem later on
33
34 @Author Jean-Yves MENGANT
35 CopyRights Jean-Yves MENGANT 1998,1999,2000
36
37 */
38
39 public class SwingComponentPopup {
40
41
42 private transient JPopupMenu _menu = new JPopupMenu() ;
43 private transient MouseListener _mouseListener ;
44 private transient JComponent _component ;
45 private transient boolean _firstItem = true ;
46 private transient boolean _activated ;
47
48 /** POPUP memu displayed when righ button entered */
49 class _POPUP_ENTERED_ extends MouseAdapter {
50 public void mousePressed( MouseEvent evt )
51 {
52 if (evt.isPopupTrigger() ) {
53 _menu.show( _component , evt.getX(), evt.getY());
54 }
55 }
56
57 public void mouseReleased( MouseEvent evt )
58 {
59 if (evt.isPopupTrigger()) {
60 _menu.show( _component , evt.getX(), evt.getY());
61 }
62 }
63 }
64
65 private void menuItemAdd( JMenuItem menuItem , ActionListener action )
66 {
67 if ( action != null )
68 menuItem.addActionListener( action ) ;
69 //if ( ! _firstItem )
70 // _menu.addSeparator() ; now made independent adding the addSeparator method
71 // _firstItem = false ;
72 _menu.add(menuItem) ;
73 }
74
75 /** add a new menuItem associated with an action */
76 public JMenuItem addMenuItem ( String label , ActionListener action )
77 {
78 JMenuItem menuItem = new JMenuItem( label ) ;
79 menuItemAdd( menuItem , action ) ;
80 return menuItem ;
81 }
82
83 /** add a new menuItem associated with an action and an icon */
84 public JMenuItem addMenuItem ( String label ,
85 ActionListener action ,
86 Icon icon
87 )
88 {
89 JMenuItem menuItem = new JMenuItem( label , icon ) ;
90 menuItemAdd( menuItem , action ) ;
91 return menuItem ;
92 }
93
94 public void addSeparator()
95 { _menu.addSeparator() ; }
96
97 public void cleanup()
98 { _menu.removeAll() ; }
99
100 public SwingComponentPopup( JComponent parent )
101 {
102 _component = parent ;
103 _mouseListener = new _POPUP_ENTERED_() ;
104 _component.addMouseListener( _mouseListener ) ;
105 }
106
107 public static void main ( String arg[] )
108 {
109
110 // Exit the debug window frame
111 Frame f = new Frame("Testing Popup Labels") ;
112 f.setForeground(Color.black) ;
113 f.setBackground(Color.lightGray) ;
114
115 f.addWindowListener(
116 new WindowAdapter() {
117 public void windowClosing( WindowEvent e )
118 { System.exit(0) ; }
119 }
120 ) ;
121
122 JLabel l = new JLabel("Push right") ;
123 SwingComponentPopup pop = new SwingComponentPopup( l ) ;
124
125 pop.addMenuItem( "Select for action",
126 new ActionListener() {
127 public void actionPerformed( ActionEvent e )
128 {
129 System.out.println("ACTION entered ") ;
130 }
131 }
132 ) ;
133
134 f.add("Center", l ) ;
135 f.pack() ;
136 f.setVisible(true) ;
137 }
138 }