import javax.swing.*;
import javax.swing.event.*;
import javax.swing.undo.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

import Composants.*;


class TogglePanel extends JPanel implements ActionListener, StateEditable {
	StateEdit etat;
	JButton undoButton, redoButton, chooseButton, endButton;
	JRadioButton gras, ital, soul;
	ButtonGroup polices;
		
  public TogglePanel() {

	  gras = new JRadioButton("gras", true);
		ital = new JRadioButton("italique");
		soul = new JRadioButton("souligné");
				
		polices = new ButtonGroup();
		polices.add(gras);
		polices.add(ital);
		polices.add(soul);
		gras.addActionListener(this);
		ital.addActionListener(this);
		soul.addActionListener(this);

		undoButton = new JButton("Undo");
		redoButton = new JButton("Redo");
		chooseButton = new JButton("Choose");
		endButton = new JButton("Ok");
		undoButton.setEnabled(false);
		redoButton.setEnabled(false);
		chooseButton.setEnabled(true);
		endButton.setEnabled(false);

		chooseButton.addActionListener(new ChooseIt());
		endButton.addActionListener(new EndIt());
		undoButton.addActionListener(new UndoIt());
		redoButton.addActionListener(new RedoIt());

		JPanel acteursBox = makeBox(gras, ital, soul);
		JPanel faireDefaireBox = makeBox(chooseButton, endButton, 
																				 undoButton, redoButton);
		setLayout(new BorderLayout());
		add(acteursBox, BorderLayout.CENTER);
		add(faireDefaireBox, BorderLayout.SOUTH);		
	}
	
  JPanel makeBox(JComponent gras, JComponent ital, JComponent soul) {
	  Box buttonBox = new Box(BoxLayout.Y_AXIS);
		buttonBox.add(gras);
		buttonBox.add(ital);
		buttonBox.add(soul);
		JPanel superBox = new JPanel();
		superBox.setLayout(new BoxLayout(superBox, BoxLayout.X_AXIS));
		superBox.add(Box.createGlue());
		superBox.add(buttonBox);
		superBox.add(Box.createGlue());
		superBox.setBorder(BorderFactory.createTitledBorder(" acteurs "));
		return superBox;
	}
		
	JPanel makeBox(JComponent chooseButton, JComponent endButton, 
		JComponent undoButton, JComponent redoButton) {
		JPanel undoRedoBox = new JPanel();
		undoRedoBox.setLayout(new BoxLayout(undoRedoBox, BoxLayout.X_AXIS));
		undoRedoBox.add(Box.createGlue());
		undoRedoBox.add(chooseButton);
		undoRedoBox.add(Box.createHorizontalStrut(3));
		undoRedoBox.add(endButton);
		undoRedoBox.add(Box.createHorizontalStrut(3));
		undoRedoBox.add(undoButton);
		undoRedoBox.add(Box.createHorizontalStrut(3));
		undoRedoBox.add(redoButton);
		undoRedoBox.add(Box.createGlue());
		undoRedoBox.setBorder(
			BorderFactory.createTitledBorder(" défaire/refaire "));
		return undoRedoBox;		
	}

	public void actionPerformed(ActionEvent ev) {
		undoButton.setEnabled(false);
		redoButton.setEnabled(false);
	}

	public void storeState(Hashtable h) {
		h.put(polices, polices.getSelection());
	}
	public void restoreState(Hashtable h) {
		ButtonModel b = (ButtonModel) h.get(polices);
		b.setSelected (true);
	}
		
  class ChooseIt implements ActionListener {
		public void actionPerformed(ActionEvent ev) {
			etat = new StateEdit(TogglePanel.this);
			chooseButton.setEnabled(false);
			endButton.setEnabled(true);
			undoButton.setEnabled(false);
			redoButton.setEnabled(false);
		}
	}
	class EndIt implements ActionListener {
		public void actionPerformed(ActionEvent ev) {
			etat.end();
			chooseButton.setEnabled(true);
			endButton.setEnabled(false);
			updateButtons();
		}
	}	
	class UndoIt implements ActionListener {
		public void actionPerformed(ActionEvent ev) {
			try {
				etat.undo();
			} catch (CannotUndoException ex) { ex.printStackTrace(); }
			updateButtons();
		}
	}
	class RedoIt implements ActionListener {
		public void actionPerformed(ActionEvent ev) {
			try {
				etat.redo();
			} catch (CannotRedoException ex) { ex.printStackTrace(); }
			updateButtons();
		}
	}	

	private void updateButtons() {
		undoButton.setEnabled(etat.canUndo());
		redoButton.setEnabled(etat.canRedo());
	}
}

public class TestStateEditToggle {
	public static void main(String[] args) {
		JFrame f = new JFrame();
		f.addWindowListener(new Fermeur());
		f.setContentPane(new TogglePanel());
		f.pack();//setSize(400,200);
		f.setVisible(true);
	}
}
