01: /*
02: * DropDownButton.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.components;
13:
14: import java.awt.Color;
15: import java.awt.event.ActionEvent;
16: import java.awt.event.ActionListener;
17: import javax.swing.Action;
18: import javax.swing.BorderFactory;
19: import javax.swing.Icon;
20: import javax.swing.JPopupMenu;
21: import javax.swing.border.Border;
22: import javax.swing.border.CompoundBorder;
23: import javax.swing.event.PopupMenuEvent;
24: import javax.swing.event.PopupMenuListener;
25: import javax.swing.plaf.basic.BasicBorders;
26:
27: /**
28: * @author support@sql-workbench.net
29: */
30: public class DropDownButton extends WbButton implements ActionListener,
31: PopupMenuListener {
32: private JPopupMenu popup;
33: private boolean popupVisible = false;
34: private Border menuBorder = BorderFactory.createLineBorder(
35: Color.GRAY, 1);
36:
37: public DropDownButton(String label) {
38: super (label);
39: init();
40: }
41:
42: public DropDownButton(Icon i) {
43: super (i);
44: init();
45: }
46:
47: private void init() {
48: setFocusable(false);
49: addActionListener(this );
50: enableToolbarRollover();
51: }
52:
53: public void setDropDownMenu(JPopupMenu menu) {
54: if (this .popup != null) {
55: this .popup.removePopupMenuListener(this );
56: this .popup.setVisible(false);
57: this .popup.removeAll();
58: }
59: popup = menu;
60: popup.setBorder(this .menuBorder);
61: popup.addPopupMenuListener(this );
62: }
63:
64: public void actionPerformed(ActionEvent evt) {
65: if (this .popup == null)
66: return;
67:
68: if (popupVisible) {
69: popup.setVisible(false);
70: popupVisible = false;
71: } else {
72: popup.show(this , 0, getHeight() - 2);
73: popupVisible = true;
74: }
75: }
76:
77: public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
78: popupVisible = true;
79: }
80:
81: public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
82: popupVisible = false;
83: }
84:
85: public void popupMenuCanceled(PopupMenuEvent e) {
86: popupVisible = false;
87: }
88: }
|