package tianma.swing.example.models;

import java.awt.Container;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.DefaultButtonModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 * Use our own button model, compared to {@link ButtonModelSample}
 * 
 */
@SuppressWarnings("serial")
public class CustomButtonModelSample extends JFrame {
	private JButton okBtn;
	private JLabel enabledLbl;
	private JLabel pressedLbl;
	private JLabel armedLbl;
	private JCheckBox checkBox;

	public CustomButtonModelSample() {
		initUI();
	}

	private void initUI() {
		Container pane = getContentPane();
		GroupLayout gl = new GroupLayout(pane);
		pane.setLayout(gl);


		checkBox = new JCheckBox();
		checkBox.setAction(new CheckBoxAction());

		enabledLbl = new JLabel("Enabled: true");
		pressedLbl = new JLabel("Pressed: false");
		armedLbl = new JLabel("Armed: false");

		okBtn = new JButton("OK");
		// okBtn.addChangeListener(new DisabledChangeListener());
		CustomButtonModel buttonModel = new CustomButtonModel();
		okBtn.setModel(buttonModel);

		gl.setAutoCreateContainerGaps(true);
		gl.setAutoCreateGaps(true);

		gl.setHorizontalGroup(gl
				.createSequentialGroup()
				.addGroup(
						gl.createParallelGroup().addComponent(okBtn)
								.addComponent(enabledLbl)
								.addComponent(pressedLbl)
								.addComponent(armedLbl)).addGap(80)
				.addComponent(checkBox));

		gl.setVerticalGroup(gl
				.createParallelGroup()
				.addGroup(
						gl.createSequentialGroup().addComponent(okBtn)
								.addGap(40).addComponent(enabledLbl)
								.addComponent(pressedLbl)
								.addComponent(armedLbl)).addComponent(checkBox));

		pack();

		setTitle("Custom Button Model Sample");
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

	}

	private class CustomButtonModel extends DefaultButtonModel {

		@Override
		public void setEnabled(boolean b) {
			if (b) {
				enabledLbl.setText("Enabled: true");
			} else {
				enabledLbl.setText("Enabled: false");
			}
			super.setEnabled(b);
		}

		@Override
		public void setArmed(boolean b) {
			if (b) {
				armedLbl.setText("Armed: true");
			} else {
				armedLbl.setText("Armed: false");
			}
			super.setArmed(b);
		}

		@Override
		public void setPressed(boolean b) {
			if (b) {
				pressedLbl.setText("Pressed: true");
			} else {
				pressedLbl.setText("Pressed: false");
			}
			super.setPressed(b);
		}

	}

	// private class DisabledChangeListener implements ChangeListener {
	//
	// @Override
	// public void stateChanged(ChangeEvent e) {
	// DefaultButtonModel buttonModel = (DefaultButtonModel) okBtn
	// .getModel();
	//
	// if (buttonModel.isEnabled()) {
	// enabledLbl.setText("Enabled: true");
	// } else {
	// enabledLbl.setText("Enabled: false");
	// }
	//
	// if (buttonModel.isPressed()) {
	// pressedLbl.setText("Pressed: true");
	// } else {
	// pressedLbl.setText("Pressed: false");
	// }
	//
	// if (buttonModel.isArmed()) {
	// armedLbl.setText("Armed: true");
	// } else {
	// armedLbl.setText("Armed: false");
	// }
	// }
	//
	// }

	private class CheckBoxAction extends AbstractAction {

		public CheckBoxAction() {
			super("Disabled");
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			okBtn.setEnabled(!okBtn.isEnabled());
		}

	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				CustomButtonModelSample sample = new CustomButtonModelSample();
				sample.setVisible(true);
			}
		});
	}
}
