File: ModalButton.java

package info (click to toggle)
libtoolbutton-java 0.2-2
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 100 kB
  • ctags: 126
  • sloc: java: 508; makefile: 59
file content (60 lines) | stat: -rw-r--r-- 1,943 bytes parent folder | download
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
/*
 * PopupToolBoxButton.java
 *
 * Created on 15 February 2003, 20:27
 */

package org.tigris.toolbutton;

import javax.swing.Action;
import javax.swing.JButton;

/** An extension of JButton which can optionally act in a similar manner
 * to a ToggleButton.
 * @author Bob Tarling
 */
public class ModalButton extends ToolButton {

    private ToolButtonAction stickyAction;
    private String tooltip;
    
    /** Creates a new instance of PopupToolboxButton
     * @param a The default action when pressing this button
     * @param rows The number of rows of buttons to display in the popup toolbox
     * @param cols The number of columns of buttons to display in the popup toolbox
     */
    public ModalButton(Action a) {
        super(a);
        setAction(a);
    }

    /** Provide a new default action for this button
     * @param a The new default action
     */
    public void setAction(Action a) {
        super.setAction(a);
        // Create an invisible button to contain the new action.
        // We can use this button to find out various info for the
        // current plaf (eg preferred button size) so we can emulate
        // whatever plaf the user is set to.
        _button = new JButton(a);
        tooltip = _button.getToolTipText();
        if (tooltip == null || tooltip.trim().length() == 0) {
            tooltip = _button.getText();
        }
        setText(null);
        if (a instanceof ModalAction) {
            // Remove any knowledge of the action to perform from the ancestor
            // we take control of performing the action and displaying the icon.
            super.setAction(new ToolButtonAction());
            setIcon(_button.getIcon());
            setToolTipText(tooltip);
        }
    }
    
    
    public Action getRealAction() {
        if (_button == null) return null;
        return _button.getAction();
    }
}