OSDN Git Service

プラットフォームごとのStigmataのホームディレクトリの場所を返す一連のクラスをリファクタリングした.
[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  * @version $Revision$ 
25  */
26 public class PopupButton extends JPanel{
27     private static final long serialVersionUID = -4428839967597028837L;
28
29     private JButton button;
30     private JPopupMenu popup;
31     private JButton arrowButton;
32
33     public PopupButton(JButton initButton){
34         button = initButton;
35         arrowButton = new JButton(new MetalComboBoxIcon());
36         popup = new JPopupMenu();
37
38         arrowButton.addActionListener(new ActionListener(){
39             @Override
40             public void actionPerformed(ActionEvent e){
41                 Point p = button.getLocation();
42                 popup.show(PopupButton.this, p.x, button.getHeight());
43             }
44         });
45         Insets insets = arrowButton.getMargin();
46         arrowButton.setMargin(new Insets(insets.top, 1, insets.bottom, 1));
47
48         setLayout(new GridBagLayout());
49         GridBagConstraints gbc = new GridBagConstraints();
50         gbc.weightx = 0d; gbc.weighty = 0d;
51         gbc.gridx = 0;    gbc.gridy = 0;
52         gbc.fill = GridBagConstraints.BOTH;
53
54         add(button, gbc);
55
56         gbc.weightx = 0d;
57         gbc.gridx = 1;
58         add(arrowButton, gbc);
59
60         updateUI();
61     }
62
63     @Override
64     public void setEnabled(boolean enabled){
65         super.setEnabled(enabled);
66         button.setEnabled(enabled);
67         arrowButton.setEnabled(enabled);
68     }
69
70     public JButton getButton(){
71         return button;
72     }
73
74     public JMenuItem addMenuItem(JMenuItem menuItem){
75         return popup.add(menuItem);
76     }
77 }