OSDN Git Service

remove Revision tag
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / ui / swing / PopupButton.java
1 package jp.sourceforge.stigmata.ui.swing;
2
3 /*
4  * $Id$
5  */
6
7 import java.awt.GridBagConstraints;
8 import java.awt.GridBagLayout;
9 import java.awt.Insets;
10 import java.awt.Point;
11 import java.awt.event.ActionEvent;
12 import java.awt.event.ActionListener;
13
14 import javax.swing.JButton;
15 import javax.swing.JMenuItem;
16 import javax.swing.JPanel;
17 import javax.swing.JPopupMenu;
18 import javax.swing.plaf.metal.MetalComboBoxIcon;
19
20 /**
21  * Popup button.
22  * 
23  * @author tamada
24  */
25 public class PopupButton extends JPanel{
26     private static final long serialVersionUID = -4428839967597028837L;
27
28     private JButton button;
29     private JPopupMenu popup;
30     private JButton arrowButton;
31
32     public PopupButton(JButton initButton){
33         button = initButton;
34         arrowButton = new JButton(new MetalComboBoxIcon());
35         popup = new JPopupMenu();
36
37         arrowButton.addActionListener(new ActionListener(){
38             @Override
39             public void actionPerformed(ActionEvent e){
40                 Point p = button.getLocation();
41                 popup.show(PopupButton.this, p.x, button.getHeight());
42             }
43         });
44         Insets insets = arrowButton.getMargin();
45         arrowButton.setMargin(new Insets(insets.top, 1, insets.bottom, 1));
46
47         setLayout(new GridBagLayout());
48         GridBagConstraints gbc = new GridBagConstraints();
49         gbc.weightx = 0d; gbc.weighty = 0d;
50         gbc.gridx = 0;    gbc.gridy = 0;
51         gbc.fill = GridBagConstraints.BOTH;
52
53         add(button, gbc);
54
55         gbc.weightx = 0d;
56         gbc.gridx = 1;
57         add(arrowButton, gbc);
58
59         updateUI();
60     }
61
62     @Override
63     public void setEnabled(boolean enabled){
64         super.setEnabled(enabled);
65         button.setEnabled(enabled);
66         arrowButton.setEnabled(enabled);
67     }
68
69     public JButton getButton(){
70         return button;
71     }
72
73     public JMenuItem addMenuItem(JMenuItem menuItem){
74         return popup.add(menuItem);
75     }
76 }