001    package swingui;
002    
003    import java.awt.event.ActionEvent;
004    import java.awt.event.ActionListener;
005    import java.util.HashMap;
006    import java.util.Map;
007    
008    import javax.swing.JButton;
009    import javax.swing.JLabel;
010    import javax.swing.JPanel;
011    
012    import physics3d.Vect3;
013    
014    import components.Absorber;
015    import components.Ball;
016    import components.GameObject;
017    
018    /** AbsorberPanel is a mutable class that extends JPanel and contains the GUI tools for
019     * adding/removing balls from an absorber.
020     */
021    public class AbsorberPanel extends JPanel implements ActionListener {
022            /** the absorber we're interested in **/
023            private Absorber abs; 
024            
025            //fields
026            private JButton addButton;
027            
028            /** create a new absorber panel. is initially empty. **/
029            public AbsorberPanel() {
030                    addButton = new JButton("Add Ball");
031                    addButton.addActionListener(this);
032                    //don't add to gui -- will be added in update()
033            }
034            
035            /** set the object (absorber) for this panel 
036             * @modifies this.abs 
037             * @throws IllegalArgumentException if obj is not an Absorber **/
038            public void setObject(GameObject obj) {
039                    if (obj instanceof Absorber) {
040                            this.abs = (Absorber)obj;
041                            updatePanel();
042                    }
043                    else
044                            throw new IllegalArgumentException("obj for AbsorberPanel must be an Absorber");
045            }
046    
047            /** update button/label on panel 
048             * @effects changes the panel to either display what is currently held or
049             *          a button to add a ball into the absorber **/
050            private void updatePanel() {
051                    clearPanel();
052                    if (abs.isFull()) {
053                            add(new JLabel("Holding: "));
054                            BallButton ball = new BallButton((Ball)abs.getObject());
055                            ball.addActionListener(this);
056                            add(ball);
057                    } else {
058                            add(new JLabel("Empty: "));
059                            add(addButton);
060                    }
061                    this.validate();
062                    this.repaint();
063            }
064            
065            /** clear the panel 
066             * @effects removes all components from panel **/
067            private void clearPanel() {
068                    this.removeAll();
069            }
070            
071            /** either adds a ball or deletes the currently held one 
072             * @effects abs **/
073            public void actionPerformed(ActionEvent e) {
074                    if (e.getSource() == addButton) {
075                            //make a new ball
076                            Map<String, String> props = new HashMap<String, String>();
077                            GameObject.putInVect3(Vect3.ZERO, props, "", false); //dummy loc
078                            props.put("name", AddPanel.randomName());
079                            Ball ball = new Ball(props, abs.getGameSpace());
080                            abs.getGameSpace().add(ball);
081                            
082                            //add it to absorber
083                            abs.hold(ball);
084                            updatePanel();
085                    } else { //remove the ball
086                            BallButton b = (BallButton)e.getSource();
087                            abs.clearObject();
088                            updatePanel();
089                    }
090            }
091            
092            /** name of this panel **/
093            public String getName() { return "Contains"; }
094            
095            /** a button that represents a ball **/
096            private class BallButton extends JButton {
097                    private Ball ball;
098                    
099                    public BallButton(Ball ball) {
100                            this.ball = ball;
101                            //set text
102                            this.setText(ball.getName() + "  \u2717");
103                    }
104                    
105                    public Ball getBall() {
106                            return ball;
107                    }
108            }
109    }