KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > ChangeMarks


1 /*
2  * ChangeMarks.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: ChangeMarks.java,v 1.1.1.1 2002/09/24 16:09:20 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import javax.swing.undo.CompoundEdit JavaDoc;
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         // Advance to first unmodified line.
35
while (line.isModified()) {
36             line = line.next();
37             if (line == null)
38                 break;
39         }
40         if (line != null) {
41             Debug.assertTrue(!line.isModified());
42             // Advance to next modified line.
43
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         // Go back to last unmodified line.
63
while (line.isModified()) {
64             line = line.previous();
65             if (line == null)
66                 break;
67         }
68         if (line != null) {
69             Debug.assertTrue(!line.isModified());
70             // Go back to last modified line.
71
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(); // Alias.
88
if (dot == null)
89             return;
90         if (editor.isColumnSelection()) {
91             editor.notSupportedForColumnSelections();
92             return;
93         }
94         final Position mark = editor.getMark(); // Alias.
95
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         // Find last unmodified line above dot or beginning of marked region.
106
while (before != null && before.isModified())
107             before = before.previous();
108         // Find last modified line in current group of changed lines.
109
for (Line line = last.next(); line != null; line = line.next()) {
110             if (line.isModified())
111                 last = line;
112             else
113                 break;
114         }
115         // Make sure at least one of the lines in question is in fact modified.
116
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; // Nothing to revert.
125
CompoundEdit JavaDoc 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 JavaDoc 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 JavaDoc compoundEdit = editor.beginCompoundEdit();
173         compoundEdit.addEdit(new UndoMove(editor));
174         final Position dot = editor.getDot(); // Alias.
175
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(); // Alias.
207
final String JavaDoc originalText = dotLine.getOriginalText();
208         if (originalText != null) {
209             CompoundEdit JavaDoc compoundEdit = editor.beginCompoundEdit();
210             final int index = originalText.indexOf('\n');
211             if (index >= 0) {
212                 // Multi-line change.
213
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                 // Single line change.
226
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             // Enclose filenames in double quotes if they contain embedded
254
// spaces.
255
String JavaDoc 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 JavaDoc 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 JavaDoc cmd = sb.toString();
271             ShellCommand shellCommand = new ShellCommand(cmd);
272             shellCommand.run();
273             // Kill existing diff output buffer if any for same parent buffer.
274
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; // There should be one at most.
281
}
282                     }
283                 }
284             }
285             String JavaDoc 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