View Javadoc

1   /*
2   
3       dsh-identify  Lightweight components for identifiable beans.
4       Copyright (c) 2003-2012 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.identify;
25  
26  import java.awt.event.MouseAdapter;
27  import java.awt.event.MouseEvent;
28  import java.awt.event.MouseListener;
29  
30  import javax.swing.ImageIcon;
31  import javax.swing.JButton;
32  import javax.swing.JPopupMenu;
33  
34  /**
35   * Context menu button.
36   *
37   * @author  Michael Heuer
38   * @version $Revision$ $Date$
39   */
40  public final class ContextMenuButton
41      extends JButton
42  {
43      /** Context menu. */
44      private final JPopupMenu contextMenu;
45  
46      /** Mouse listener. */
47      private final MouseListener listener = new MouseAdapter()
48          {
49              /** {@inheritDoc} */
50              public void mousePressed(final MouseEvent event)
51              {
52                  contextMenu.show(event.getComponent(), 0, event.getComponent().getHeight());
53              }
54          };
55  
56      /** Default image icon. */
57      private static final ImageIcon DEFAULT_ICON = new ImageIcon(ContextMenuButton.class.getResource("context-menu.png"));
58  
59      /** GTK look and feel image icon. */
60      private static final ImageIcon GTK_ICON = new ImageIcon(ContextMenuButton.class.getResource("context-menu-gtk.png"));
61  
62  
63      /**
64       * Create a new context menu button with the specified context menu.
65       *
66       * @param contextMenu context menu, must not be null
67       */
68      public ContextMenuButton(final JPopupMenu contextMenu)
69      {
70          super();
71          if (contextMenu == null)
72          {
73              throw new IllegalArgumentException("contextMenu must not be null");
74          }
75          this.contextMenu = contextMenu;
76          setIcon(IdentifyUtils.isGTKLookAndFeel() ? GTK_ICON : DEFAULT_ICON);
77          setToolTipText("View menu");
78          addMouseListener(listener);
79      }
80  
81  
82      /**
83       * Release the resources consumed by this context menu button so that it may eventually be garbage collected.
84       */
85      public void dispose()
86      {
87          removeMouseListener(listener);
88      }
89  }