KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CVS.java
3  *
4  * Copyright (C) 1998-2004 Peter Graves
5  * $Id: CVS.java,v 1.5 2004/08/08 00:54:04 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 java.util.List JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
26 import javax.swing.undo.CompoundEdit JavaDoc;
27
28 public final class CVS implements Constants
29 {
30     public static void cvs()
31     {
32         MessageDialog.showMessageDialog(
33             "The command \"cvs\" requires an argument",
34             "Error");
35     }
36
37     public static void cvs(String JavaDoc args)
38     {
39         final Editor editor = Editor.currentEditor();
40         final Buffer buffer = editor.getBuffer();
41         if (buffer.getFile() == null)
42             return;
43         buffer.setBusy(true);
44         editor.setWaitCursor();
45         final String JavaDoc name = buffer.getFile().getName();
46         FastStringBuffer sb = new FastStringBuffer("cvs ");
47         sb.append(args);
48         // "cvs -H" doesn't need a filename.
49
if (!args.trim().startsWith("-H ")) {
50             sb.append(' ');
51             if (name.indexOf(' ') >= 0) {
52                 // Enclose filename in double quotes since it contains an
53
// embedded space.
54
sb.append('"');
55                 sb.append(name);
56                 sb.append('"');
57             } else
58                 sb.append(name);
59         }
60         final String JavaDoc cmd = sb.toString();
61         Runnable JavaDoc commandRunnable = new Runnable JavaDoc() {
62             public void run()
63             {
64                 final String JavaDoc output =
65                     command(cmd, buffer.getCurrentDirectory());
66                 Runnable JavaDoc completionRunnable = new Runnable JavaDoc() {
67                     public void run()
68                     {
69                         cvsCompleted(editor, buffer, cmd, output);
70                     }
71                 };
72                 SwingUtilities.invokeLater(completionRunnable);
73             }
74         };
75         new Thread JavaDoc(commandRunnable).start();
76     }
77
78     private static void cvsCompleted(Editor editor, Buffer buffer, String JavaDoc cmd,
79         String JavaDoc output)
80     {
81         if (output != null && output.length() > 0) {
82             Buffer buf;
83             if (cmd.startsWith("cvs diff"))
84                 buf = new DiffOutputBuffer(buffer, output, VC_CVS);
85             else
86                 buf = OutputBuffer.getOutputBuffer(output);
87             buf.setTitle(cmd);
88             editor.makeNext(buf);
89             editor.activateInOtherWindow(buf);
90         }
91         buffer.checkCVS();
92         buffer.setBusy(false);
93         for (EditorIterator it = new EditorIterator(); it.hasNext();) {
94             Editor ed = it.nextEditor();
95             if (ed.getBuffer() == buffer) {
96                 ed.setDefaultCursor();
97                 // Update CVS information in status bar.
98
ed.getFrame().repaintStatusBar();
99             }
100         }
101     }
102
103     public static void add()
104     {
105         final Editor editor = Editor.currentEditor();
106         final Buffer buffer = editor.getBuffer();
107         if (buffer.getFile() == null)
108             return;
109         buffer.setBusy(true);
110         editor.setWaitCursor();
111         final String JavaDoc name = buffer.getFile().getName();
112         FastStringBuffer sb = new FastStringBuffer("cvs add ");
113         if (name.indexOf(' ') >= 0) {
114             // Enclose filename in double quotes since it contains an embedded
115
// space.
116
sb.append('"');
117             sb.append(name);
118             sb.append('"');
119         } else
120             sb.append(name);
121         final String JavaDoc cmd = sb.toString();
122         Runnable JavaDoc commandRunnable = new Runnable JavaDoc() {
123             public void run()
124             {
125                 final String JavaDoc output =
126                     command(cmd, buffer.getCurrentDirectory());
127                 Runnable JavaDoc completionRunnable = new Runnable JavaDoc() {
128                     public void run()
129                     {
130                         addCompleted(editor, buffer, cmd, output);
131                     }
132                 };
133                 SwingUtilities.invokeLater(completionRunnable);
134             }
135         };
136         new Thread JavaDoc(commandRunnable).start();
137     }
138
139     private static void addCompleted(Editor editor, Buffer buffer,
140         String JavaDoc cmd, String JavaDoc output)
141     {
142         OutputBuffer buf = OutputBuffer.getOutputBuffer(output);
143         buf.setTitle(cmd);
144         editor.makeNext(buf);
145         editor.activateInOtherWindow(buf);
146         buffer.checkCVS();
147         buffer.setBusy(false);
148         for (EditorIterator it = new EditorIterator(); it.hasNext();) {
149             Editor ed = it.nextEditor();
150             if (ed.getBuffer() == buffer) {
151                 ed.setDefaultCursor();
152                 // Update CVS information in status bar.
153
ed.getFrame().repaintStatusBar();
154             }
155         }
156     }
157
158     public static void commit()
159     {
160         final Editor editor = Editor.currentEditor();
161         Buffer parentBuffer = editor.getBuffer();
162         if (parentBuffer instanceof DiffOutputBuffer)
163             parentBuffer = parentBuffer.getParentBuffer();
164         if (parentBuffer == null)
165             return;
166         if (parentBuffer.getFile() == null)
167             return;
168         final String JavaDoc title =
169             "cvs commit ".concat(parentBuffer.getFile().getName());
170         boolean save = false;
171         if (parentBuffer.isModified()) {
172             int response =
173                 ConfirmDialog.showConfirmDialogWithCancelButton(editor,
174                     CHECK_SAVE_PROMPT, title);
175             switch (response) {
176                 case RESPONSE_YES:
177                     save = true;
178                     break;
179                 case RESPONSE_NO:
180                     break;
181                 case RESPONSE_CANCEL:
182                     return;
183             }
184             editor.repaintNow();
185         }
186         if (!save || parentBuffer.save()) {
187             // Look for existing checkin buffer before making a new one.
188
CheckinBuffer checkinBuffer = null;
189             for (BufferIterator it = new BufferIterator(); it.hasNext();) {
190                 Buffer buf = it.nextBuffer();
191                 if (buf instanceof CheckinBuffer) {
192                     if (buf.getParentBuffer() == parentBuffer) {
193                         checkinBuffer = (CheckinBuffer) buf;
194                         break;
195                     }
196                 }
197             }
198             if (checkinBuffer == null) {
199                 checkinBuffer = new CheckinBuffer(parentBuffer, VC_CVS);
200                 checkinBuffer.setFormatter(new PlainTextFormatter(checkinBuffer));
201                 checkinBuffer.setTitle(title);
202             }
203             editor.makeNext(checkinBuffer);
204             editor.activateInOtherWindow(checkinBuffer);
205         }
206     }
207
208     public static void diff()
209     {
210         diff(null);
211     }
212
213     public static void diff(String JavaDoc args)
214     {
215         final Editor editor = Editor.currentEditor();
216         Buffer parentBuffer = editor.getBuffer();
217         if (parentBuffer instanceof CheckinBuffer)
218             parentBuffer = parentBuffer.getParentBuffer();
219         if (parentBuffer.getFile() == null)
220             return;
221         final String JavaDoc name = parentBuffer.getFile().getName();
222         if (args == null)
223             args = "-u";
224         FastStringBuffer sb = new FastStringBuffer("cvs diff ");
225         sb.append(args);
226         sb.append(' ');
227         if (name.indexOf(' ') >= 0) {
228             // Enclose filename in double quotes since it contains an embedded
229
// space.
230
sb.append('"');
231             sb.append(name);
232             sb.append('"');
233         } else
234             sb.append(name);
235         final String JavaDoc cmd = sb.toString();
236         boolean save = false;
237         if (parentBuffer.isModified()) {
238             int response =
239                 ConfirmDialog.showConfirmDialogWithCancelButton(editor,
240                     CHECK_SAVE_PROMPT, cmd);
241             switch (response) {
242                 case RESPONSE_YES:
243                     save = true;
244                     break;
245                 case RESPONSE_NO:
246                     break;
247                 case RESPONSE_CANCEL:
248                     return;
249             }
250             editor.repaintNow();
251         }
252         parentBuffer.setBusy(true);
253         if (!save || parentBuffer.save()) {
254             // Kill existing diff output buffer if any for same parent buffer.
255
for (BufferIterator it = new BufferIterator(); it.hasNext();) {
256                 Buffer b = it.nextBuffer();
257                 if (b instanceof DiffOutputBuffer) {
258                     if (b.getParentBuffer() == parentBuffer) {
259                         b.kill();
260                         break; // There should be one at most.
261
}
262                 }
263             }
264             final Buffer finalParentBuffer = parentBuffer;
265             Runnable JavaDoc commandRunnable = new Runnable JavaDoc() {
266                 public void run()
267                 {
268                     final String JavaDoc output =
269                         command(cmd, finalParentBuffer.getCurrentDirectory());
270                     Runnable JavaDoc completionRunnable = new Runnable JavaDoc() {
271                         public void run()
272                         {
273                             diffCompleted(editor, finalParentBuffer, cmd,
274                                 output);
275                         }
276                     };
277                     SwingUtilities.invokeLater(completionRunnable);
278                 }
279             };
280             new Thread JavaDoc(commandRunnable).start();
281         }
282     }
283
284     private static void diffCompleted(Editor editor, Buffer parentBuffer,
285         String JavaDoc cmd, String JavaDoc output)
286     {
287         if (output.length() == 0) {
288             parentBuffer.setBusy(false);
289             MessageDialog.showMessageDialog(editor,
290                 "No changes since latest version",
291                 parentBuffer.getFile().getName());
292         } else {
293             DiffOutputBuffer buf =
294                 new DiffOutputBuffer(parentBuffer, output, VC_CVS);
295             buf.setTitle(cmd);
296             editor.makeNext(buf);
297             editor.activateInOtherWindow(buf);
298             parentBuffer.setBusy(false);
299             for (EditorIterator it = new EditorIterator(); it.hasNext();) {
300                 Editor ed = it.nextEditor();
301                 if (ed.getBuffer() == parentBuffer)
302                     ed.setDefaultCursor();
303             }
304         }
305     }
306
307     public static void diffDir()
308     {
309         final Editor editor = Editor.currentEditor();
310         final Buffer buffer = editor.getBuffer();
311         final String JavaDoc cmd = "cvs diff -u";
312         final File directory = buffer.getCurrentDirectory();
313         // Kill existing diff output buffer if any for same directory.
314
for (BufferIterator it = new BufferIterator(); it.hasNext();) {
315             Buffer b = it.nextBuffer();
316             if (b instanceof DiffOutputBuffer) {
317                 if (directory.equals(((DiffOutputBuffer)b).getDirectory())) {
318                     b.kill();
319                     break; // There should be one at most.
320
}
321             }
322         }
323         final DiffOutputBuffer buf =
324             new DiffOutputBuffer(directory, null, VC_CVS);
325         buf.setTitle(cmd);
326         editor.makeNext(buf);
327         Editor ed = editor.activateInOtherWindow(buf);
328         ed.setWaitCursor();
329         buf.setBusy(true);
330         Runnable JavaDoc commandRunnable = new Runnable JavaDoc() {
331             public void run()
332             {
333                 final String JavaDoc output = command(cmd, directory);
334                 Runnable JavaDoc completionRunnable = new Runnable JavaDoc() {
335                     public void run()
336                     {
337                         diffDirCompleted(buf, output);
338                     }
339                 };
340                 SwingUtilities.invokeLater(completionRunnable);
341             }
342         };
343         new Thread JavaDoc(commandRunnable).start();
344     }
345
346     private static void diffDirCompleted(Buffer buffer, String JavaDoc output)
347     {
348         buffer.setText(output);
349         buffer.setBusy(false);
350         for (EditorIterator it = new EditorIterator(); it.hasNext();) {
351             Editor ed = it.nextEditor();
352             if (ed.getBuffer() == buffer) {
353                 ed.setDot(buffer.getFirstLine(), 0);
354                 ed.setTopLine(buffer.getFirstLine());
355                 ed.setUpdateFlag(REPAINT);
356                 ed.updateDisplay();
357             }
358         }
359     }
360
361     public static void replaceComment(final Editor editor, final String JavaDoc comment)
362     {
363         if (!(editor.getBuffer() instanceof CheckinBuffer)) {
364             Debug.bug();
365             return;
366         }
367         final CheckinBuffer buffer = (CheckinBuffer) editor.getBuffer();
368         String JavaDoc oldComment = extractComment(buffer);
369         if (oldComment.equals(comment))
370             return;
371         insertComment(editor, comment);
372     }
373
374     public static String JavaDoc extractComment(final CheckinBuffer buffer)
375     {
376         return buffer.getText();
377     }
378
379     private static void insertComment(final Editor editor, final String JavaDoc comment)
380     {
381         final CheckinBuffer buffer = (CheckinBuffer) editor.getBuffer();
382         try {
383             buffer.lockWrite();
384         }
385         catch (InterruptedException JavaDoc e) {
386             Log.error(e);
387             return;
388         }
389         try {
390             CompoundEdit JavaDoc compoundEdit = editor.beginCompoundEdit();
391             editor.selectAll();
392             editor.deleteRegion();
393             editor.insertString(comment);
394             editor.endCompoundEdit(compoundEdit);
395             buffer.modified();
396         }
397         finally {
398             buffer.unlockWrite();
399         }
400         final Position end = buffer.getEnd();
401         for (EditorIterator it = new EditorIterator(); it.hasNext();) {
402             Editor ed = it.nextEditor();
403             if (ed.getBuffer() == buffer) {
404                 ed.setTopLine(buffer.getFirstLine());
405                 ed.setDot(end.copy()); // No undo.
406
ed.moveCaretToDotCol();
407                 ed.setUpdateFlag(REPAINT);
408                 ed.updateDisplay();
409             }
410         }
411     }
412
413     public static void finish(final Editor editor, final CheckinBuffer checkinBuffer)
414     {
415         final Buffer parentBuffer = checkinBuffer.getParentBuffer();
416         if (parentBuffer.getFile() == null)
417             return;
418         final String JavaDoc name = parentBuffer.getFile().getName();
419         final File tempFile = Utilities.getTempFile();
420         if (!checkinBuffer.writeFile(tempFile)) {
421             MessageDialog.showMessageDialog(editor,
422                 "Unable to write temporary file ".concat(tempFile.canonicalPath()),
423                 "Error");
424             return;
425         }
426         FastStringBuffer sb = new FastStringBuffer("cvs commit -F ");
427         // Enclose both filenames in double quotes in case they contain
428
// embedded spaces.
429
sb.append('"');
430         sb.append(tempFile.canonicalPath());
431         sb.append('"');
432         sb.append(' ');
433         sb.append('"');
434         sb.append(name);
435         sb.append('"');
436         final String JavaDoc cmd = sb.toString();
437         editor.setWaitCursor();
438         checkinBuffer.setBusy(true);
439         parentBuffer.setBusy(true);
440         Runnable JavaDoc commandRunnable = new Runnable JavaDoc() {
441             public void run()
442             {
443                 final CvsCommand cvsCommand = new CvsCommand(cmd,
444                     parentBuffer.getCurrentDirectory());
445                 cvsCommand.run();
446                 tempFile.delete();
447                 Runnable JavaDoc completionRunnable = new Runnable JavaDoc() {
448                     public void run()
449                     {
450                         finishCompleted(editor, checkinBuffer, parentBuffer,
451                             cvsCommand, name, tempFile);
452                     }
453                 };
454                 SwingUtilities.invokeLater(completionRunnable);
455             }
456         };
457         new Thread JavaDoc(commandRunnable).start();
458     }
459
460     private static void finishCompleted(Editor editor, Buffer checkinBuffer,
461         Buffer parentBuffer, CvsCommand cvsCommand, String JavaDoc name, File tempFile)
462     {
463         checkinBuffer.setBusy(false);
464         if (cvsCommand.exitValue() != 0) {
465             // Error.
466
OutputBuffer buf = OutputBuffer.getOutputBuffer(cvsCommand.getOutput());
467             buf.setTitle("cvs commit ".concat(name));
468             editor.makeNext(buf);
469             editor.activate(buf);
470         } else {
471             // Success. Kill old diff buffer, if any; its contents are no
472
// longer correct.
473
for (BufferIterator it = new BufferIterator(); it.hasNext();) {
474                 Buffer b = it.nextBuffer();
475                 if (b instanceof DiffOutputBuffer) {
476                     if (b.getParentBuffer() == parentBuffer) {
477                         b.kill();
478                         break; // There should be one at most.
479
}
480                 }
481             }
482             if (Editor.getBufferList().contains(checkinBuffer))
483                 checkinBuffer.kill();
484             if (editor.getOtherEditor() != null) {
485                 editor.otherWindow();
486                 editor.unsplitWindow();
487             } else
488                 editor.updateDisplay();
489         }
490         // The source file may have been modified by the checkin process.
491
editor.reactivate(parentBuffer);
492         parentBuffer.checkCVS();
493         parentBuffer.setBusy(false);
494         for (EditorIterator it = new EditorIterator(); it.hasNext();) {
495             Editor ed = it.nextEditor();
496             if (ed.getBuffer().isBusy())
497                 ed.setWaitCursor();
498             else
499                 ed.setDefaultCursor();
500             // Update CVS information in status bar.
501
if (ed.getBuffer() == parentBuffer)
502                 ed.getFrame().repaintStatusBar();
503         }
504         Editor.restoreFocus();
505     }
506
507     public static void log()
508     {
509         log("");
510     }
511
512     public static void log(String JavaDoc args)
513     {
514         boolean useCurrentFile = true;
515         List JavaDoc list = Utilities.tokenize(args);
516         for (int i = 0; i < list.size(); i++) {
517             String JavaDoc arg = (String JavaDoc) list.get(i);
518             if (arg.charAt(0) != '-') {
519                 // Must be a filename.
520
useCurrentFile = false;
521                 break;
522             }
523         }
524         final Editor editor = Editor.currentEditor();
525         final Buffer parentBuffer = editor.getBuffer();
526         FastStringBuffer sb = new FastStringBuffer("cvs log ");
527         sb.append(args);
528         if (useCurrentFile) {
529             if (parentBuffer.getFile() == null)
530                 return;
531             final String JavaDoc name = parentBuffer.getFile().getName();
532             sb.append(' ');
533             if (name.indexOf(' ') >= 0) {
534                 // Enclose filename in double quotes since it contains an
535
// embedded space.
536
sb.append('"');
537                 sb.append(name);
538                 sb.append('"');
539             } else
540                 sb.append(name);
541         }
542         final String JavaDoc cmd = sb.toString();
543         editor.setWaitCursor();
544         final String JavaDoc output = command(cmd, parentBuffer.getCurrentDirectory());
545         OutputBuffer buf = OutputBuffer.getOutputBuffer(output);
546         buf.setTitle(cmd);
547         editor.makeNext(buf);
548         editor.activateInOtherWindow(buf);
549         editor.setDefaultCursor();
550     }
551
552     // Implementation.
553
private static final String JavaDoc command(String JavaDoc cmd, File workingDirectory)
554     {
555         CvsCommand cvsCommand = new CvsCommand(cmd, workingDirectory);
556         cvsCommand.run();
557         return cvsCommand.getOutput();
558     }
559
560     private static final class CvsCommand
561     {
562         final private String JavaDoc cmd;
563         final private File workingDirectory;
564         private ShellCommand shellCommand;
565
566         public CvsCommand(String JavaDoc cmd, File workingDirectory)
567         {
568             this.cmd = cmd;
569             this.workingDirectory = workingDirectory;
570         }
571
572         public void run()
573         {
574             shellCommand = new ShellCommand(cmd, workingDirectory);
575             shellCommand.run();
576         }
577
578         public final String JavaDoc getOutput()
579         {
580             Debug.assertTrue(shellCommand != null);
581             return shellCommand.getOutput();
582         }
583
584         public final int exitValue()
585         {
586             Debug.assertTrue(shellCommand != null);
587             return shellCommand.exitValue();
588         }
589     }
590 }
591
Popular Tags