1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
// $Id: PopupButton.java,v 1.1 2004/11/03 18:30:05 bobtarling Exp $
package org.tigris.swidgets;
import java.awt.Component;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import org.tigris.toolbar.toolbutton.ModalButton;
/**
* A button that displays a popup when pressed. The popup can contain
* any Component.
*
* @author Jeremy Jones
*/
public class PopupButton extends ModalButton {
/**
* The popup component.
*/
private Component component = null;
/**
* Constructs a new PopupButton with no popup assigned.
*/
public PopupButton() {
super(null);
setAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
showPopup();
}
});
String javaVersion = System.getProperties().getProperty(
"java.specification.version");
if (javaVersion.equals("1.3")) {
setBorderPainted(false);
// This is needed specifically for JDK1.3 on Windows & Motif
setMargin(new Insets(0, 0, 0, 0));
}
}
/**
* Constructs a new PopupButton with the specified popup component
* and button icon
*
* @param c the component to popup
* @param icon the button icon
*/
public PopupButton(Component c, Icon icon) {
this();
component = c;
setIcon(icon);
}
/**
* Returns the component that is displayed within the popup.
*
* @return the popup component
*/
public Component getPopupComponent() {
return component;
}
/**
* Sets the component to be displayed within the popup.
*
* @param c the new popup component
*/
public void setPopupComponent(Component c) {
component = c;
}
/**
* Sets the button icon. Overridden to automatically add a drop down icon
* to the right of the icon image.
*
* @param icon the new button icon
*/
public final void setIcon(Icon icon) {
if (icon instanceof ImageIcon) {
super.setIcon(new DropDownIcon((ImageIcon) icon));
}
else {
super.setIcon(icon);
}
}
/**
* Called when the user clicks the button. This method will show the
* popup component in a popup menu. Subclasses can override this
* method to perform actions before the popup is shown.
*/
protected void showPopup() {
if (component != null) {
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(component);
popupMenu.pack();
popupMenu.show(this, 0, getHeight());
}
}
}
|