View Javadoc

1   package org.devaki.nextobjects.ui.components;
2   /*
3   
4   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
5   
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by the
8   Free Software Foundation; either version 2 of the License, or (at your
9   option) any later version.
10  
11  This program is distributed in the hope that it will
12  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc., 59
18  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  
20  */
21  import java.awt.GridBagConstraints;
22  import java.awt.GridBagLayout;
23  import java.awt.Insets;
24  import java.awt.Point;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import javax.swing.JButton;
28  import javax.swing.JMenuItem;
29  import javax.swing.JPanel;
30  import javax.swing.JPopupMenu;
31  import org.devaki.nextobjects.NextObjects;
32  /***
33   * This class i sresponsible for a part of the drop menu
34   * @author     <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
35   */
36  public class CustomButtonMenu extends JPanel
37  {
38      /***
39       * Internal popupmenu needed to make the button drop
40       */
41      private JPopupMenu jPopupMenu = new JPopupMenu();
42      /***
43       * Internal drop button
44       */
45      private CustomButton jDropButton =
46          new CustomButton(
47              NextObjects.getReference().loadIconResource("dropMenu.gif"),
48              "",
49              " ",
50              true);
51      /***
52       * Construct a new 'CustomButtonMenu' object
53       * @param defaultButton the default button
54       * @param pMenuItems the menu item
55       */
56      public CustomButtonMenu(
57          final JButton defaultButton,
58          final JMenuItem[] pMenuItems)
59      {
60          // Components
61          // Panels
62          this.setLayout(new GridBagLayout());
63          // Pop-up Menus
64          this.jPopupMenu.setInvoker(this);
65          for (int i = 0; i < pMenuItems.length; i++)
66          {
67              this.jPopupMenu.add(pMenuItems[i]);
68          }
69          // Listeners
70          this.jDropButton.addActionListener(new ActionListener()
71          {
72              public final void actionPerformed(final ActionEvent ae)
73              {
74                  Point p = getLocationOnScreen();
75                  p.y += getHeight();
76                  jPopupMenu.setLocation(p);
77                  jPopupMenu.setVisible(true);
78              }
79          });
80          // Assembling components
81          this.add(
82              defaultButton,
83              new GridBagConstraints(
84                  0,
85                  0,
86                  1,
87                  1,
88                  1.0,
89                  1.0,
90                  GridBagConstraints.CENTER,
91                  GridBagConstraints.BOTH,
92                  new Insets(0, 0, 0, 0),
93                  0,
94                  0));
95          this.add(
96              jDropButton,
97              new GridBagConstraints(
98                  1,
99                  0,
100                 1,
101                 1,
102                 0.0,
103                 1.0,
104                 GridBagConstraints.CENTER,
105                 GridBagConstraints.VERTICAL,
106                 new Insets(0, 0, 0, 0),
107                 0,
108                 0));
109     }
110 }