OSDN Git Service

refs #344 インストールのとき,依存ライブラリも一緒にプラグインディレクトリへコピーするように変更した.
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / ui / swing / PopupButton.java
1 package jp.sourceforge.stigmata.ui.swing;
2
3 import java.awt.GridBagConstraints;
4 import java.awt.GridBagLayout;
5 import java.awt.Insets;
6 import java.awt.Point;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9
10 import javax.swing.JButton;
11 import javax.swing.JMenuItem;
12 import javax.swing.JPanel;
13 import javax.swing.JPopupMenu;
14 import javax.swing.plaf.metal.MetalComboBoxIcon;
15
16 /**
17  * Popup button.
18  * 
19  * @author tamada
20  */
21 public class PopupButton extends JPanel{
22     private static final long serialVersionUID = -4428839967597028837L;
23
24     private JButton button;
25     private JPopupMenu popup;
26     private JButton arrowButton;
27
28     public PopupButton(JButton initButton){
29         button = initButton;
30         arrowButton = new JButton(new MetalComboBoxIcon());
31         popup = new JPopupMenu();
32
33         arrowButton.addActionListener(new ActionListener(){
34             @Override
35             public void actionPerformed(ActionEvent e){
36                 Point p = button.getLocation();
37                 popup.show(PopupButton.this, p.x, button.getHeight());
38             }
39         });
40         Insets insets = arrowButton.getMargin();
41         arrowButton.setMargin(new Insets(insets.top, 1, insets.bottom, 1));
42
43         setLayout(new GridBagLayout());
44         GridBagConstraints gbc = new GridBagConstraints();
45         gbc.weightx = 0d; gbc.weighty = 0d;
46         gbc.gridx = 0;    gbc.gridy = 0;
47         gbc.fill = GridBagConstraints.BOTH;
48
49         add(button, gbc);
50
51         gbc.weightx = 0d;
52         gbc.gridx = 1;
53         add(arrowButton, gbc);
54
55         updateUI();
56     }
57
58     @Override
59     public void setEnabled(boolean enabled){
60         super.setEnabled(enabled);
61         button.setEnabled(enabled);
62         arrowButton.setEnabled(enabled);
63     }
64
65     public JButton getButton(){
66         return button;
67     }
68
69     public JMenuItem addMenuItem(JMenuItem menuItem){
70         return popup.add(menuItem);
71     }
72 }