View Javadoc
1   /*
2   
3       dsh-identify  Lightweight components for identifiable beans.
4       Copyright (c) 2003-2019 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   */
39  public final class ContextMenuButton
40      extends JButton
41  {
42      /** Context menu. */
43      private final JPopupMenu contextMenu;
44  
45      /** Mouse listener. */
46      private final MouseListener listener = new MouseAdapter()
47          {
48              @Override
49              public void mousePressed(final MouseEvent event)
50              {
51                  contextMenu.show(event.getComponent(), 0, event.getComponent().getHeight());
52              }
53          };
54  
55      /** Default image icon. */
56      private static final ImageIcon DEFAULT_ICON = new ImageIcon(ContextMenuButton.class.getResource("context-menu.png"));
57  
58      /** GTK look and feel image icon. */
59      private static final ImageIcon GTK_ICON = new ImageIcon(ContextMenuButton.class.getResource("context-menu-gtk.png"));
60  
61  
62      /**
63       * Create a new context menu button with the specified context menu.
64       *
65       * @param contextMenu context menu, must not be null
66       */
67      public ContextMenuButton(final JPopupMenu contextMenu)
68      {
69          super();
70          if (contextMenu == null)
71          {
72              throw new IllegalArgumentException("contextMenu must not be null");
73          }
74          this.contextMenu = contextMenu;
75          setIcon(IdentifyUtils.isGTKLookAndFeel() ? GTK_ICON : DEFAULT_ICON);
76          setToolTipText("View menu");
77          addMouseListener(listener);
78      }
79  
80  
81      /**
82       * Release the resources consumed by this context menu button so that it may eventually be garbage collected.
83       */
84      public void dispose()
85      {
86          removeMouseListener(listener);
87      }
88  }