summaryrefslogtreecommitdiff
path: root/java/gjt/test/ImageButtonTest.java
blob: 52df6efeba0610b993c97b8de8856efef68d7ed2 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package gjt.test;

import java.applet.Applet;
import java.awt.*;
import gjt.Box;
import gjt.ImageButton;
import gjt.ImageButtonEvent;
import gjt.SpringyImageButtonController;
import gjt.StickyImageButtonController;

/**
 * 2 ImageButtons, one springy and the other sticky, both 
 * crabby.<p>
 *
 * Both ImageButtons come with an awt.Button that is used to 
 * enable/disable the ImageButton it's associated with.<p>
 *
 * ImageButtonEvents, along with mouse enter and mouse exit
 * events for the two image buttons are printed out.<p>
 *
 * @version 1.0, Apr 25, 1996
 * @author  David Geary
 * @see     gjt.test.UnitTest
 * @see     gjt.ImageButton
 * @see     gjt.ImageButtonEvent
 * @see     gjt.SpringyImageButtonController
 * @see     gjt.StickyImageButtonController
 */
public class ImageButtonTest extends UnitTest {
    public String title() { 
        return "ImageButton Test"; 
    }
    public Panel centerPanel() { 
        return new ImageButtonTestPanel(this); 
    }
}

class ImageButtonTestPanel extends Panel {
    private ImageButton springyButton;
    private Button      springyButtonEnabler;
    private ImageButton stickyButton;
    private Button      stickyButtonEnabler;

    public ImageButtonTestPanel(Applet applet) {
        Image              image;
        Box                springyBox, stickyBox;
        GridBagLayout      gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        image = 
        applet.getImage(applet.getCodeBase(), "gifs/crab.gif");

        springyButton        = new ImageButton(image);
        springyButtonEnabler = new Button     ("Disable");
        stickyButton         = new ImageButton(image);
        stickyButtonEnabler  = new Button     ("Disable");

        stickyButton.setController(
			new StickyImageButtonController(stickyButton));

        setLayout(gbl);

        gbc.anchor    = GridBagConstraints.NORTH;
        springyBox    = new Box(springyButton, "Springy");
        gbc.insets    = new Insets(10,0,0,0);
        gbl.setConstraints(springyBox, gbc); add(springyBox);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.insets    = new Insets(45,10,0,0);
        gbl.setConstraints(springyButtonEnabler, gbc); 
        add(springyButtonEnabler);

        gbc.anchor    = GridBagConstraints.NORTH;
        gbc.gridwidth = 1;
        stickyBox     = new Box(stickyButton, "Sticky");
        gbc.insets    = new Insets(10,0,0,0);
        gbc.weighty   = 1.0;
        gbl.setConstraints(stickyBox, gbc); add(stickyBox);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.insets    = new Insets(45,10,0,0);
        gbl.setConstraints(stickyButtonEnabler, gbc); 
        add(stickyButtonEnabler);
    }
    public boolean action(Event event, Object what) {
        Button button = (Button)event.target;
        String label  = (String)what;

        if(button == stickyButtonEnabler) {
            if(label.equals("Disable")) stickyButton.disable();
            else                        stickyButton.enable();
        }
        else {
            if(label.equals("Disable")) springyButton.disable();
            else                        springyButton.enable();
        }
        if(label.equals("Disable")) button.setLabel("Enable");
        else                        button.setLabel("Disable");

        return true;
    }
    public boolean handleEvent(Event event) {
        boolean eventHandled = false;
        
        if(event instanceof ImageButtonEvent) {
            System.out.println("ImageButton " + event);
            eventHandled = true;
        }
        if(event.id == Event.MOUSE_ENTER) {
            if(event.target == stickyButton)
                System.out.println("Sticky Button Entered");

            else if(event.target == springyButton)
                System.out.println("Springy Button Entered");

            eventHandled = true;
        }
        if(event.id == Event.MOUSE_EXIT) {
            if(event.target == stickyButton)
                System.out.println("Sticky Button Exited");

            else if(event.target == springyButton)
                System.out.println("Springy Button Exited");

            eventHandled = true;
        }
        if(eventHandled) return true;
        else             return super.handleEvent(event);
    }
}