import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.undo.*;
import javax.swing.event.*;

import Composants.*;
public class UndoRedoTextApp {
		public static void main(String[] args) {
				JFrame f = new JFrame("Text Demo");
				f.addWindowListener(new Fermeur());
				f.setContentPane(new UndoRedoText());
				f.setSize(400,300);
				f.setVisible(true);
		}
}
class UndoRedoText extends JPanel {
  protected JTextArea editor = new JTextArea();
  protected UndoManager manager = new UndoManager();
  protected JButton undoButton = new JButton("Undo");
  protected JButton redoButton = new JButton("Redo");

  public UndoRedoText() {
    JPanel buttonPanel = new JPanel(new GridLayout());
    buttonPanel.add(undoButton);
    buttonPanel.add(redoButton);
editor.setText("C'est très simple d'implémenter le \"undo\" sur des\n"+  
"textes. En effet, toute modfication donne\n"+  
"naissance à un UndoableEdit qui est lancé\n"+   
"par le document. Le \"undomanager\" n'a plus qu'à\n"+   
"faire la mise à jour des boutons .");

    JScrollPane scroller = new JScrollPane(editor);
		setLayout(new BorderLayout());
    add(buttonPanel, BorderLayout.NORTH);
    add(scroller, BorderLayout.CENTER);

    undoButton.setEnabled(false);
    redoButton.setEnabled(false);
    editor.getDocument().addUndoableEditListener(new ManageIt());
    undoButton.addActionListener(new UndoIt());
    redoButton.addActionListener(new RedoIt());
  }
	class UndoIt implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      try { manager.undo(); }
			catch (CannotRedoException cre) { cre.printStackTrace(); }
			updateButtons();
		}
	}
	class RedoIt implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      try { manager.redo(); }
			catch (CannotRedoException cre) { cre.printStackTrace(); }
			updateButtons();
		}
	}
	class ManageIt implements UndoableEditListener {
    public void undoableEditHappened(UndoableEditEvent e) {
      manager.undoableEditHappened(e);
      updateButtons();
    }
  }
  public void updateButtons() {
    undoButton.setText(manager.getUndoPresentationName());
    redoButton.setText(manager.getRedoPresentationName());
    undoButton.setEnabled(manager.canUndo());
    redoButton.setEnabled(manager.canRedo());
  }
}


