package example;
//-*- mode:java; encoding:utf8n; coding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.undo.*;

public class MainPanel extends JPanel {
    private final JTextField tf = new JTextField(24);
    private final JTextField field = new JTextField(24);
    private final UndoManager um = new UndoManager();
    private final UndoManager undoManager = new UndoManager();
    private final Document doc = new PlainDocument() {
        @Override public void replace(int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
             undoManager.undoableEditHappened(new UndoableEditEvent(this, new ReplaceUndoableEdit(offset, length, text)));
             replaceIgnoringUndo(offset, length, text, attrs);
         }
        private void replaceIgnoringUndo(int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            for(UndoableEditListener uel: getUndoableEditListeners()) {
                removeUndoableEditListener(uel);
            }
            super.replace(offset, length, text, attrs);
            for(UndoableEditListener uel: getUndoableEditListeners()) {
                addUndoableEditListener(uel);
            }
        }
        class ReplaceUndoableEdit extends AbstractUndoableEdit {
            private final String oldValue;
            private final String newValue;
            private int offset;
            public ReplaceUndoableEdit(int offset, int length, String newValue) {
                String txt;
                try{
                    txt = getText(offset, length);
                }catch(BadLocationException e) {
                    txt = null;
                }
                this.oldValue = txt;
                this.newValue = newValue;
                this.offset = offset;
            }
            @Override public void undo() throws CannotUndoException {
                 try{
                     replaceIgnoringUndo(offset, newValue.length(), oldValue, null);
                 }catch(BadLocationException ex) {
                     throw new CannotUndoException();
                 }
             }
            @Override public void redo() throws CannotRedoException {
                 try{
                     replaceIgnoringUndo(offset, oldValue.length(), newValue, null);
                 }catch(BadLocationException ex) {
                     throw new CannotUndoException();
                 }
             }
            @Override public boolean canUndo() {
                return true;
            }
            @Override public boolean canRedo() {
                return true;
            }
        }
    };
    public MainPanel() {
        super(new BorderLayout());

        tf.getDocument().addUndoableEditListener(um);

        doc.addUndoableEditListener(undoManager);
        field.setDocument(doc);

        field.setText("init");
        tf.setText("default");

        JPanel p = new JPanel();
        p.add(new JButton(new AbstractAction("undo") {
            @Override public void actionPerformed(ActionEvent e) {
                try{
                    undoManager.undo();
                    um.undo();
                }catch(Exception ex) {
                    java.awt.Toolkit.getDefaultToolkit().beep();
                }
            }
        }));
        p.add(new JButton(new AbstractAction("redo") {
            @Override public void actionPerformed(ActionEvent e) {
                try{
                    undoManager.redo();
                    um.redo();
                }catch(Exception ex) {
                    java.awt.Toolkit.getDefaultToolkit().beep();
                }
            }
        }));
        p.add(new JButton(new AbstractAction("setText(new Date())") {
            @Override public void actionPerformed(ActionEvent e) {
                String str = new Date().toString();
                tf.setText(str);
                field.setText(str);
            }
        }));

        Box box = Box.createVerticalBox();
        box.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        box.add(makePanel("Default", tf));
        box.add(Box.createVerticalStrut(5));
        box.add(makePanel("add Image(JLabel)", field));

        add(box, BorderLayout.NORTH);
        add(p, BorderLayout.SOUTH);

        setPreferredSize(new Dimension(320, 240));
    }
    private static JPanel makePanel(String title, JComponent c) {
        JPanel p = new JPanel(new BorderLayout());
        p.setBorder(BorderFactory.createTitledBorder(title));
        p.add(c);
        return p;
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        try{
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }catch(Exception e) {
            e.printStackTrace();
        }
        JFrame frame = new JFrame("@title@");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

@@ -728,33 +728,32 @@
 n %7B%0A            
- 
 undoManager.undo
@@ -850,33 +850,32 @@
 ));%0A            
- 
 replaceIgnoringU
@@ -900,33 +900,32 @@
 , text, attrs);%0A
- 
         %7D%0A      
@@ -1038,32 +1038,47 @@
 tionException %7B%0A
+            /*%0A
             for(
@@ -1376,32 +1376,224 @@
 ;%0A            %7D%0A
+            /*/%0A            removeUndoableEditListener(undoManager);%0A            super.replace(offset, length, text, attrs);%0A            addUndoableEditListener(undoManager);%0A            //*/%0A
         %7D%0A      
@@ -2259,39 +2259,37 @@
                 
- 
 try%7B%0A
- 
                 
@@ -2348,33 +2348,32 @@
 ldValue, null);%0A
- 
                 
@@ -2418,33 +2418,32 @@
                 
- 
 throw new Cannot
@@ -2467,36 +2467,34 @@
                 
- 
 %7D%0A
- 
             %7D%0A  
@@ -2569,39 +2569,37 @@
                 
- 
 try%7B%0A
- 
                 
@@ -2658,33 +2658,32 @@
 ewValue, null);%0A
- 
                 
@@ -2728,33 +2728,32 @@
                 
- 
 throw new Cannot
@@ -2777,36 +2777,34 @@
                 
- 
 %7D%0A
- 
             %7D%0A  
@@ -3228,12 +3228,19 @@
 xt(%22
-init
+aaaaaaaaaaa
 %22);%0A

indent cleanup