1 21 22 package org.armedbear.j; 23 24 import javax.swing.undo.CompoundEdit ; 25 26 public final class ChangeMarks implements Constants 27 { 28 public static void nextChange() 29 { 30 final Editor editor = Editor.currentEditor(); 31 if (editor.getDot() == null) 32 return; 33 Line line = editor.getDotLine(); 34 while (line.isModified()) { 36 line = line.next(); 37 if (line == null) 38 break; 39 } 40 if (line != null) { 41 Debug.assertTrue(!line.isModified()); 42 while (!line.isModified()) { 44 line = line.next(); 45 if (line == null) 46 break; 47 } 48 } 49 if (line != null) { 50 Debug.assertTrue(line.isModified()); 51 editor.moveDotTo(line, 0); 52 } else 53 editor.status("No more changes"); 54 } 55 56 public static void previousChange() 57 { 58 final Editor editor = Editor.currentEditor(); 59 if (editor.getDot() == null) 60 return; 61 Line line = editor.getDotLine(); 62 while (line.isModified()) { 64 line = line.previous(); 65 if (line == null) 66 break; 67 } 68 if (line != null) { 69 Debug.assertTrue(!line.isModified()); 70 while (!line.isModified()) { 72 line = line.previous(); 73 if (line == null) 74 break; 75 } 76 } 77 if (line != null) { 78 Debug.assertTrue(line.isModified()); 79 editor.moveDotTo(line, 0); 80 } else 81 editor.status("No more changes"); 82 } 83 84 public static void revertLines() 85 { 86 final Editor editor = Editor.currentEditor(); 87 final Position dot = editor.getDot(); if (dot == null) 89 return; 90 if (editor.isColumnSelection()) { 91 editor.notSupportedForColumnSelections(); 92 return; 93 } 94 final Position mark = editor.getMark(); final Line dotLine = editor.getDotLine(); 96 Line before, last; 97 if (mark != null) { 98 Region r = new Region(editor); 99 before = r.getBeginLine().previous(); 100 last = r.getEndLine(); 101 } else { 102 before = dotLine.previous(); 103 last = dotLine; 104 } 105 while (before != null && before.isModified()) 107 before = before.previous(); 108 for (Line line = last.next(); line != null; line = line.next()) { 110 if (line.isModified()) 111 last = line; 112 else 113 break; 114 } 115 boolean modified = false; 117 for (Line line = last; line != before; line = line.previous()) { 118 if (line.isModified()) { 119 modified = true; 120 break; 121 } 122 } 123 if (!modified) 124 return; CompoundEdit compoundEdit = editor.beginCompoundEdit(); 126 editor.addUndo(SimpleEdit.MOVE); 127 if (mark != null) { 128 editor.setMark(null); 129 editor.setUpdateFlag(REPAINT); 130 } 131 dot.moveTo(last, 0); 132 revertLine(editor, dot.getLine()); 133 while (dot.getPreviousLine() != before) { 134 editor.addUndo(SimpleEdit.MOVE); 135 dot.moveTo(dot.getPreviousLine(), 0); 136 revertLine(editor, dot.getLine()); 137 } 138 editor.moveCaretToDotCol(); 139 editor.endCompoundEdit(compoundEdit); 140 } 141 142 private static void revertLine(Editor editor, Line line) 143 { 144 if (!line.isModified()) 145 return; 146 final Buffer buffer = editor.getBuffer(); 147 try { 148 buffer.lockWrite(); 149 } 150 catch (InterruptedException e) { 151 Log.error(e); 152 return; 153 } 154 try { 155 if (line.isNew()) 156 revertNewLine(editor, line); 157 else 158 revertChangedLine(editor, line); 159 } 160 finally { 161 buffer.unlockWrite(); 162 } 163 } 164 165 private static void revertNewLine(Editor editor, final Line dotLine) 166 { 167 editor.adjustMarkers(dotLine); 168 final Line prev = dotLine.previous(); 169 final Line next = dotLine.next(); 170 if (prev == null && next == null) 171 return; 172 CompoundEdit compoundEdit = editor.beginCompoundEdit(); 173 compoundEdit.addEdit(new UndoMove(editor)); 174 final Position dot = editor.getDot(); boolean insertBefore; 176 if (next != null) { 177 dot.moveTo(next, 0); 178 insertBefore = true; 179 } else { 180 dot.moveTo(prev, 0); 181 insertBefore = false; 182 } 183 editor.moveCaretToDotCol(); 184 final Buffer buffer = editor.getBuffer(); 185 compoundEdit.addEdit(new UndoRemoveLine(editor, insertBefore)); 186 if (prev != null) 187 prev.setNext(next); 188 else 189 buffer.setFirstLine(next); 190 if (next != null) 191 next.setPrevious(prev); 192 buffer.renumber(); 193 buffer.modified(); 194 editor.endCompoundEdit(compoundEdit); 195 for (EditorIterator it = new EditorIterator(); it.hasNext();) { 196 Editor ed = it.nextEditor(); 197 if (ed.getTopLine() == dotLine) 198 ed.setTopLine(dot.getLine()); 199 } 200 buffer.repaint(); 201 } 202 203 private static void revertChangedLine(Editor editor, final Line dotLine) 204 { 205 final Buffer buffer = editor.getBuffer(); 206 final Position dot = editor.getDot(); final String originalText = dotLine.getOriginalText(); 208 if (originalText != null) { 209 CompoundEdit compoundEdit = editor.beginCompoundEdit(); 210 final int index = originalText.indexOf('\n'); 211 if (index >= 0) { 212 final Line begin = dotLine; 214 final Line end = dotLine.next(); 215 editor.addUndo(SimpleEdit.LINE_EDIT); 216 dotLine.setText(originalText.substring(0, index)); 217 buffer.modified(); 218 editor.addUndo(SimpleEdit.MOVE); 219 dot.setOffset(dotLine.length()); 220 editor.addUndo(SimpleEdit.INSERT_STRING); 221 buffer.insertString(dot, originalText.substring(index)); 222 for (Line line = begin; line != end; line = line.next()) 223 line.unmodified(); 224 } else { 225 editor.addUndo(SimpleEdit.LINE_EDIT); 227 dotLine.setText(originalText); 228 dotLine.unmodified(); 229 Editor.updateInAllEditors(dotLine); 230 buffer.modified(); 231 if (dot.getOffset() > dotLine.length()) { 232 editor.addUndo(SimpleEdit.MOVE); 233 dot.setOffset(dotLine.length()); 234 } 235 } 236 editor.moveCaretToDotCol(); 237 editor.endCompoundEdit(compoundEdit); 238 } 239 } 240 241 public static void changes() 242 { 243 final Editor editor = Editor.currentEditor(); 244 final Buffer buffer = editor.getBuffer(); 245 final File file = buffer.getFile(); 246 if (file == null || file.isRemote()) 247 return; 248 final File tempFile = Utilities.getTempFile(); 249 if (tempFile == null) 250 return; 251 if (buffer.writeFile(tempFile)) { 252 FastStringBuffer sb = new FastStringBuffer("diff -u "); 253 String name1 = file.canonicalPath(); 256 if (name1.indexOf(' ') >= 0) { 257 sb.append('"'); 258 sb.append(name1); 259 sb.append('"'); 260 } else 261 sb.append(name1); 262 sb.append(' '); 263 String name2 = tempFile.canonicalPath(); 264 if (name2.indexOf(' ') >= 0) { 265 sb.append('"'); 266 sb.append(name2); 267 sb.append('"'); 268 } else 269 sb.append(name2); 270 final String cmd = sb.toString(); 271 ShellCommand shellCommand = new ShellCommand(cmd); 272 shellCommand.run(); 273 for (BufferIterator it = new BufferIterator(); it.hasNext();) { 275 Buffer b = it.nextBuffer(); 276 if (b instanceof DiffOutputBuffer) { 277 if (((DiffOutputBuffer)b).getParentBuffer() == buffer) { 278 if (((DiffOutputBuffer)b).getVCType() == 0) { 279 b.kill(); 280 break; } 282 } 283 } 284 } 285 String output = shellCommand.getOutput(); 286 if (output.length() == 0) { 287 MessageDialog.showMessageDialog(editor, "No changes", 288 buffer.getFile().getName()); 289 } else { 290 DiffOutputBuffer outputBuffer = 291 new DiffOutputBuffer(buffer, output, 0); 292 sb.setLength(0); 293 sb.append("diff -ub "); 294 sb.append(file.getName()); 295 sb.append(' '); 296 sb.append(tempFile.canonicalPath()); 297 outputBuffer.setTitle(sb.toString()); 298 editor.makeNext(outputBuffer); 299 editor.activateInOtherWindow(outputBuffer); 300 } 301 } 302 if (tempFile.isFile()) 303 tempFile.delete(); 304 } 305 } 306 | Popular Tags |