001package com.ganteater.ae.desktop.editor;
002
003import java.awt.event.KeyAdapter;
004import java.awt.event.KeyEvent;
005
006import javax.swing.JTextArea;
007import javax.swing.text.Document;
008import javax.swing.undo.UndoManager;
009
010import org.apache.commons.lang.StringUtils;
011
012public class TextEditor extends JTextArea {
013
014        private static final long serialVersionUID = -298735473495909314L;
015        private final UndoManager undo = new UndoManager();
016
017        public TextEditor() {
018                super();
019                setTabSize(4);
020                Document doc = getDocument();
021                doc.addUndoableEditListener((evt) -> undo.addEdit(evt.getEdit()));
022                addKeyListener(new KeyAdapter() {
023                        @Override
024                        public void keyPressed(KeyEvent e) {
025                                if (e.getKeyCode() == KeyEvent.VK_Z && e.isControlDown()) {
026                                        undo();
027                                } else if (e.getKeyCode() == KeyEvent.VK_Y && e.isControlDown()) {
028                                        redo();
029                                }
030                        }
031                });
032
033        }
034
035        public void redo() {
036                try {
037                        int caretPosition = getCaretPosition();
038                        if (undo.canRedo()) {
039                                undo.redo();
040                                if (getDocument().getLength() == 0) {
041                                        if (undo.canRedo()) {
042                                                undo.redo();
043                                        } else if (undo.canUndo()) {
044                                                undo.undo();
045                                        }
046                                }
047                        }
048                        setCaretPosition(caretPosition);
049                } catch (Exception e) {
050                        // no nothing.
051                }
052        }
053
054        public void undo() {
055                try {
056                        int caretPosition = getCaretPosition();
057                        if (undo.canUndo()) {
058                                undo.undo();
059                                if (getDocument().getLength() == 0) {
060                                        if (undo.canUndo()) {
061                                                undo.undo();
062                                        } else if (undo.canRedo()) {
063                                                undo.redo();
064                                        }
065                                }
066                        }
067                        setCaretPosition(caretPosition);
068                } catch (Exception e) {
069                        // no nothing.
070                }
071        }
072
073        public void removeLine() {
074                int caretPosition = getCaretPosition();
075                String text = getText();
076                int start = StringUtils.lastIndexOf(text, "\n", caretPosition - 1);
077                start = start < 0 ? 0 : start + 1;
078                int end = StringUtils.indexOf(text, "\n", caretPosition);
079                end = end < 0 ? text.length() - 1 : end + 1;
080                start = start < end ? start : end;
081                replaceRange("", start, end);
082                setCaretPosition(start == 0 ? 0 : start);
083        }
084
085        public void setOriginalText(String xmlText) {
086                setText(xmlText);
087        }
088
089}