
package org.openspml.browser;

import java.util.ArrayList;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * A panel combining a label and a checkbox.
 * This turns out to be awkward if you want to use action listeners
 * since the event source is the inner checkbox which the container
 * doesn't know about.  So you have to call the isSource method.
 * I played around with trying to handle the event for the inner
 * checkbox and firing our own events, but fireActionPerformed
 * is protected.
 */
public class LabeledCheckbox extends JPanel 
  implements ActionListener, ChangeListener {

    JLabel _label;
    JCheckBox _checkbox;
    ActionListener _listener;

    public LabeledCheckbox(String label) {
	this(label, false);
    }

    public LabeledCheckbox(String label, boolean checked) {

	setLayout(new LinearLayout(0));
	
	_label = new JLabel(label);
	_checkbox = new JCheckBox();
	_checkbox.setSelected(checked);
        _checkbox.addActionListener(this);

        // don't need this, it adds a lot of spurious events
        // _checkbox.addChangeListener(this);

	add(_checkbox);
	add(_label);
    }

    public void addActionListener(ActionListener l) {
	_listener = l;
    }

    public void actionPerformed(ActionEvent e) {
        // mutate it into an event on ourslves
        // actually it doesn't really matter at the moment,
        // any event will cause the parent panels to regen the XML
        if (_listener != null) {
            e = new ActionEvent(this, 0, null);
            _listener.actionPerformed(e);
        }
    }

    public void stateChanged(ChangeEvent e) {
        // mutate it into an event on ourslves?
        if (_listener != null) {
            ActionEvent ae = new ActionEvent(this, 0, null);
            _listener.actionPerformed(ae);
        }
    }

    public boolean isSelected() {

	return _checkbox.isSelected();
    }

    public void setSelected(boolean b) {
	_checkbox.setSelected(b);
    }

}
