1 21 22 package org.armedbear.j; 23 24 import javax.swing.undo.AbstractUndoableEdit ; 25 import javax.swing.undo.UndoableEdit ; 26 27 public final class UndoDeleteLineSeparator extends AbstractUndoableEdit 28 implements Constants, UndoableEdit 29 { 30 private PreState preState; 31 private PostState postState; 32 33 public UndoDeleteLineSeparator(Editor editor) 34 { 35 preState = new PreState(editor); 36 } 37 38 public void undo() 39 { 40 super.undo(); 41 final Editor editor = Editor.currentEditor(); 42 final Buffer buffer = editor.getBuffer(); 43 postState = new PostState(editor); 44 preState.restoreState(editor); 45 editor.setUpdateFlag(REFRAME); 46 47 if (postState.modificationCount != preState.modificationCount) { 48 buffer.invalidate(); 50 Sidebar.setUpdateFlagInAllFrames(SIDEBAR_MODIFIED_BUFFER_COUNT | 51 SIDEBAR_REPAINT_BUFFER_LIST); 52 Sidebar.repaintBufferListInAllFrames(); 53 } 54 } 55 56 public void redo() 57 { 58 super.redo(); 59 final Editor editor = Editor.currentEditor(); 60 final Buffer buffer = editor.getBuffer(); 61 postState.restoreState(editor); 62 editor.setUpdateFlag(REFRAME); 63 64 if (postState.modificationCount != preState.modificationCount) { 65 buffer.invalidate(); 67 Sidebar.setUpdateFlagInAllFrames(SIDEBAR_MODIFIED_BUFFER_COUNT | 68 SIDEBAR_REPAINT_BUFFER_LIST); 69 Sidebar.repaintBufferListInAllFrames(); 70 } 71 } 72 73 private static class PreState 74 { 75 final int dotLineNumber; 76 final int dotOffset; 77 final int absCaretCol; 78 final int modificationCount; 79 final boolean modified; 80 final Line first; 81 final Line second; 82 83 PreState(Editor editor) 84 { 85 final Line dotLine = editor.getDotLine(); 86 dotLineNumber = dotLine.lineNumber(); 87 dotOffset = editor.getDotOffset(); 88 absCaretCol = editor.getAbsoluteCaretCol(); 89 final Buffer buffer = editor.getBuffer(); 90 modificationCount = buffer.getModCount(); 91 modified = buffer.isModified(); 92 first = dotLine.copy(); 93 second = dotLine.next().copy(); 94 } 95 96 void restoreState(Editor editor) 98 { 99 final Buffer buffer = editor.getBuffer(); 100 101 Line before = editor.getDotLine().previous(); 102 Line after = editor.getDotLine().next(); 103 first.setPrevious(before); 104 if (before != null) 105 before.setNext(first); 106 else 107 buffer.setFirstLine(first); 108 first.setNext(second); 109 second.setPrevious(first); 110 second.setNext(after); 111 if (after != null) 112 after.setPrevious(second); 113 115 buffer.setModCount(modificationCount); 116 117 buffer.needsRenumbering = true; 118 buffer.renumber(); 119 120 editor.setDot(first, dotOffset); 121 final Display display = editor.getDisplay(); 122 display.setCaretCol(absCaretCol - display.getShift()); 123 display.setUpdateFlag(REPAINT); 124 } 125 } 126 127 private static class PostState 128 { 129 final int dotLineNumber; 130 final int dotOffset; 131 final int absCaretCol; 132 final int modificationCount; 133 final boolean modified; 134 final Line line; 135 136 PostState(Editor editor) 137 { 138 final Line dotLine = editor.getDotLine(); 139 dotLineNumber = dotLine.lineNumber(); 140 dotOffset = editor.getDotOffset(); 141 absCaretCol = editor.getAbsoluteCaretCol(); 142 final Buffer buffer = editor.getBuffer(); 143 modificationCount = buffer.getModCount(); 144 modified = buffer.isModified(); 145 line = dotLine.copy(); 146 } 147 148 void restoreState(Editor editor) 150 { 151 final Buffer buffer = editor.getBuffer(); 152 153 final Line before = editor.getDotLine().previous(); 154 final Line after = editor.getDotLine().next().next(); 155 final Line restored = line.copy(); 156 restored.setPrevious(before); 157 if (before != null) 158 before.setNext(restored); 159 else 160 buffer.setFirstLine(restored); 161 restored.setNext(after); 162 if (after != null) 163 after.setPrevious(restored); 164 166 buffer.setModCount(modificationCount); 167 168 buffer.needsRenumbering = true; 169 buffer.renumber(); 170 171 editor.setDot(restored, dotOffset); 172 final Display display = editor.getDisplay(); 173 display.setCaretCol(absCaretCol - display.getShift()); 174 display.setUpdateFlag(REPAINT); 175 } 176 } 177 } 178 | Popular Tags |