summaryrefslogtreecommitdiff
path: root/java/gjt/ImageButtonPanel.java
blob: 0d033b7967ba0ce427f9521b99cc1cdad787c9c2 (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
package gjt;

import java.awt.*;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * A panel which contains a collection of ImageButtons, 
 * arranged either horizontally or vertically.<p>
 *
 * Handling of mouse events is delegated to an image button 
 * panel controller.  By default, an image button panel is 
 * outfitted with an instance of RadioImageButtonPanelController
 * which implements mutually exclusive selection behavior.
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     ImageButton
 * @see     RadioImageButtonPanelController
 * @see     gjt.test.ToolbarTest
 */
public class ImageButtonPanel extends Panel {
    static private int _defaultGap = 5;

    private Hashtable nameAndButtonPairs = new Hashtable();
    private ImageButtonPanelController controller;

    public ImageButtonPanel(Orientation orient) { 
        this(orient, Orientation.CENTER, 
                     Orientation.CENTER, _defaultGap); 
    }
    public ImageButtonPanel(Orientation orient, int gap) {
        this(orient, Orientation.CENTER, 
                     Orientation.CENTER, gap);
    }
    public ImageButtonPanel(Orientation orient, 
                            Orientation horient, 
                            Orientation vorient, int gap) {
        Assert.notFalse(orient == Orientation.HORIZONTAL || 
                        orient == Orientation.VERTICAL);

        if(orient == Orientation.VERTICAL) 
            setLayout(new ColumnLayout(horient, vorient, gap));
        else                               
            setLayout(new RowLayout(horient, vorient, gap));

        setController(
            new RadioImageButtonPanelController(this));
    }
    public void setController(ImageButtonPanelController c) {
        this.controller = c;
    }
    public Insets insets() { return new Insets(10,10,10,10); }

    public ImageButton add(Image image, String name) {
        ImageButton button = new ImageButton(image);
        add(button);
        nameAndButtonPairs.put(name, button);
        return button;
    }   
    public ImageButton add(Image image) { 
        return add(image, "noname"); 
    }
    public void add(ImageButton button) { 
        add(button, "noname");       
    }
    public void add(ImageButton button, String name) {
        nameAndButtonPairs.put(name, button);
        super.add(button);
    }
    public ImageButton getButtonByName(String name) {
        return (ImageButton)nameAndButtonPairs.get(name);
    }
    public String getButtonName(ImageButton button) {
        Enumeration e = nameAndButtonPairs.keys();
        ImageButton nbutt;
        String nstr;

        while(e.hasMoreElements()) {
            nstr = (String)e.nextElement();
            nbutt = (ImageButton)nameAndButtonPairs.get(nstr);

            if(nbutt.equals(button))
              return nstr;
        }
        return null;
    }
    public void addSpacer(int sizeInPixels) {
        Assert.notFalse(sizeInPixels > 0);
        Canvas spacer = new Canvas();
        spacer.resize(sizeInPixels, sizeInPixels);
        add(spacer);
    }
    public boolean mouseDown(Event event, int x, int y) {
        return controller != null ? 
               controller.mouseDown(event,x,y) : false;
    }
    public boolean mouseDrag(Event event, int x, int y) {
        return controller != null ? 
               controller.mouseDrag(event,x,y) : false;
    }
    public boolean mouseUp(Event event, int x, int y) {
        return controller != null ? 
               controller.mouseUp(event,x,y) : false;
    }
}