package utilities.swing;

import java.awt.Color;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Element;

@SuppressWarnings("serial")
public class LinedTextArea extends JScrollPane {

	private final JTextArea lines;

	public LinedTextArea(final JTextArea textArea) {
		lines = new JTextArea("1");
		lines.setBackground(Color.LIGHT_GRAY);
		lines.setEditable(false);

		textArea.getDocument().addDocumentListener(new DocumentListener(){
			private String getText() {
				int caretPosition = textArea.getDocument().getLength();
				Element root = textArea.getDocument().getDefaultRootElement();
				StringBuilder text = new StringBuilder("1" + System.getProperty("line.separator"));
				for(int i = 2; i < root.getElementIndex(caretPosition) + 2; i++){
					text.append(i + System.getProperty("line.separator"));
				}
				return text.toString();
			}
			@Override
			public void changedUpdate(DocumentEvent de) {
				lines.setText(getText());
			}

			@Override
			public void insertUpdate(DocumentEvent de) {
				lines.setText(getText());
			}

			@Override
			public void removeUpdate(DocumentEvent de) {
				lines.setText(getText());
			}

		});

		getViewport().add(textArea);
		setRowHeaderView(lines);
		setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	}


}

@@ -42,16 +42,208 @@
 .Color;%0A
+import java.awt.Toolkit;%0Aimport java.awt.event.ActionEvent;%0Aimport java.awt.event.KeyEvent;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0Aimport javax.swing.AbstractAction;
 %0Aimport 
@@ -297,16 +297,46 @@
 xtArea;%0A
+import javax.swing.KeyStroke;%0A
 import j
@@ -433,280 +433,2154 @@
 ing.
-text.Element;%0A%0A@SuppressWarnings(%22serial%22)%0Apublic class LinedTextArea extends JScrollPane %7B%0A%0A%09private final JTextArea lines;%0A%0A%09public LinedTextArea(final JTextArea textArea) %7B%0A%09%09lines = new JTextArea(%221%22);%0A%09%09lines.setBackground(Color.LIGHT_GRAY);%0A%09%09lines.setEditable(false
+event.UndoableEditEvent;%0Aimport javax.swing.event.UndoableEditListener;%0Aimport javax.swing.text.Document;%0Aimport javax.swing.text.Element;%0Aimport javax.swing.undo.CannotRedoException;%0Aimport javax.swing.undo.CannotUndoException;%0Aimport javax.swing.undo.UndoManager;%0A%0A@SuppressWarnings(%22serial%22)%0Apublic class LinedTextArea extends JScrollPane %7B%0A%0A%09private static final Logger LOGGER = Logger.getLogger(LinedTextArea.class.getName());%0A%09private static final int MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();%0A%09private final JTextArea lines;%0A%0A%09public LinedTextArea(final JTextArea textArea) %7B%0A%09%09lines = new JTextArea(%221%22);%0A%09%09lines.setBackground(Color.LIGHT_GRAY);%0A%09%09lines.setEditable(false);%0A%0A%09%09final UndoManager undoManager = new UndoManager();%0A%09%09Document document = textArea.getDocument();%0A%09%09document.addUndoableEditListener(new UndoableEditListener() %7B%0A            @Override%0A            public void undoableEditHappened(UndoableEditEvent e) %7B%0A                undoManager.addEdit(e.getEdit());%0A//                System.out.println(e);%0A            %7D%0A        %7D);%0A        textArea.getActionMap().put(%22Undo%22, new AbstractAction(%22Undo%22) %7B%0A            @Override%0A            public void actionPerformed(ActionEvent evt) %7B%0A                try %7B%0A                    if (undoManager.canUndo()) %7B%0A                        undoManager.undo();%0A                    %7D%0A                %7D catch (CannotUndoException e) %7B%0A                %09LOGGER.log(Level.WARNING, %22Unable to undo%22, e);%0A                %7D%0A            %7D%0A        %7D);%0A        textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, MASK), %22Undo%22);%0A        textArea.getActionMap().put(%22Redo%22, new AbstractAction(%22Redo%22) %7B%0A            @Override%0A            public void actionPerformed(ActionEvent evt) %7B%0A                try %7B%0A                    if (undoManager.canRedo()) %7B%0A                        undoManager.redo();%0A                    %7D%0A                %7D catch (CannotRedoException e) %7B%0A                %09LOGGER.log(Level.WARNING, %22Unable to redo%22, e);%0A                %7D%0A            %7D%0A        %7D);%0A        textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Y,MASK), %22Redo%22
 );%0A%0A

Undo feature