KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * P4.java
3  *
4  * Copyright (C) 1998-2004 Peter Graves
5  * $Id: P4.java,v 1.17 2004/09/17 18:30:07 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 gnu.regexp.RE;
25 import gnu.regexp.REMatch;
26 import gnu.regexp.UncheckedRE;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31 import javax.swing.undo.CompoundEdit JavaDoc;
32
33 public class P4 implements Constants
34 {
35     public static void p4()
36     {
37         if (!checkP4Installed())
38             return;
39         MessageDialog.showMessageDialog("The command \"p4\" requires an argument.",
40                                         "Error");
41     }
42
43     public static void p4(String JavaDoc s)
44     {
45         if (!checkP4Installed())
46             return;
47         List JavaDoc args = Utilities.tokenize(s);
48         if (args.size() == 0)
49             return;
50         String JavaDoc command = (String JavaDoc) args.get(0);
51         if (command.equals("submit")) {
52             MessageDialog.showMessageDialog("Use \"p4Submit\".", "Error");
53             return;
54         }
55         if (command.equals("change")) {
56             MessageDialog.showMessageDialog("Use \"p4Change\".", "Error");
57             return;
58         }
59         final Editor editor = Editor.currentEditor();
60         editor.setWaitCursor();
61         FastStringBuffer sb = new FastStringBuffer("p4 ");
62         for (Iterator JavaDoc it = args.iterator(); it.hasNext();) {
63             String JavaDoc arg = (String JavaDoc) it.next();
64             if (arg.equals("%")) {
65                 File file = editor.getBuffer().getFile();
66                 if (file != null)
67                     arg = file.canonicalPath();
68             }
69             sb.append(maybeQuote(arg));
70             sb.append(' ');
71         }
72         final String JavaDoc cmd = sb.toString().trim();
73         final Buffer parentBuffer = editor.getBuffer();
74         Runnable JavaDoc commandRunnable = new Runnable JavaDoc() {
75             public void run()
76             {
77                 final String JavaDoc output =
78                     command(cmd, editor.getCurrentDirectory());
79                 Runnable JavaDoc completionRunnable = new Runnable JavaDoc() {
80                     public void run()
81                     {
82                         p4Completed(editor, parentBuffer, cmd, output);
83                     }
84                 };
85                 SwingUtilities.invokeLater(completionRunnable);
86             }
87         };
88         new Thread JavaDoc(commandRunnable).start();
89     }
90
91     private static void p4Completed(Editor editor, Buffer parentBuffer,
92         String JavaDoc cmd, String JavaDoc output)
93     {
94         if (output != null && output.length() > 0) {
95             Buffer buf;
96             if (cmd.startsWith("p4 diff"))
97                 buf = new DiffOutputBuffer(parentBuffer, output, VC_P4);
98             else
99                 buf = OutputBuffer.getOutputBuffer(output);
100             buf.setTitle(cmd);
101             editor.makeNext(buf);
102             editor.activateInOtherWindow(buf);
103         }
104     }
105
106     public static void add()
107     {
108         if (!checkP4Installed())
109             return;
110         final Editor editor = Editor.currentEditor();
111         final Buffer buffer = editor.getBuffer();
112         if (buffer.getFile() == null)
113             return;
114         editor.setWaitCursor();
115         final String JavaDoc name = buffer.getFile().getName();
116         FastStringBuffer sb = new FastStringBuffer("p4 add ");
117         sb.append(maybeQuote(name));
118         final String JavaDoc cmd = sb.toString();
119         final String JavaDoc output = command(cmd, buffer.getCurrentDirectory());
120         OutputBuffer buf = OutputBuffer.getOutputBuffer(output);
121         buf.setTitle(cmd);
122         editor.makeNext(buf);
123         editor.activateInOtherWindow(buf);
124         editor.setDefaultCursor();
125     }
126
127     public static void edit()
128     {
129         if (!checkP4Installed())
130             return;
131         final Editor editor = Editor.currentEditor();
132         final Buffer buffer = editor.getBuffer();
133         final File file = buffer.getFile();
134         if (file == null)
135             return;
136         buffer.setBusy(true);
137         editor.setWaitCursor();
138         FastStringBuffer sb = new FastStringBuffer("p4 edit ");
139         sb.append(maybeQuote(file.canonicalPath()));
140         final String JavaDoc cmd = sb.toString();
141         Runnable JavaDoc commandRunnable = new Runnable JavaDoc() {
142             public void run()
143             {
144                 final String JavaDoc output = command(cmd, null);
145                 Runnable JavaDoc completionRunnable = new Runnable JavaDoc() {
146                     public void run()
147                     {
148                         editCompleted(editor, buffer, cmd, output);
149                     }
150                 };
151                 SwingUtilities.invokeLater(completionRunnable);
152             }
153         };
154         new Thread JavaDoc(commandRunnable).start();
155     }
156
157     private static void editCompleted(Editor editor, Buffer buffer,
158         String JavaDoc cmd, String JavaDoc output)
159     {
160         // Don't bother with output buffer unless there's an error.
161
if (output.trim().endsWith(" - opened for edit")) {
162             editor.status("File opened for edit");
163         } else {
164             OutputBuffer buf = OutputBuffer.getOutputBuffer(output);
165             buf.setTitle(cmd);
166             editor.makeNext(buf);
167             editor.activateInOtherWindow(buf);
168         }
169         // Update read-only status.
170
if (editor.reactivate(buffer))
171             Sidebar.repaintBufferListInAllFrames();
172         buffer.setBusy(false);
173         EditorIterator iter = new EditorIterator();
174         while (iter.hasNext()) {
175             Editor ed = iter.nextEditor();
176             if (ed.getBuffer() == buffer)
177                 ed.setDefaultCursor();
178         }
179     }
180
181     // For Editor.checkReadOnly(). Displays output buffer if necessary.
182
public static boolean autoEdit(Editor editor)
183     {
184         if (editor == null)
185             return false;
186         final Buffer buffer = editor.getBuffer();
187         final File file = buffer.getFile();
188         String JavaDoc output = _autoEdit(file);
189         if (output == null)
190             return false;
191         FastStringBuffer sb = new FastStringBuffer("p4 edit ");
192         sb.append(maybeQuote(file.canonicalPath()));
193         editCompleted(editor, buffer, sb.toString(), output);
194         return !buffer.isReadOnly();
195     }
196
197     // For replaceInFiles(). Returns false if there are any complications.
198
public static boolean autoEdit(File file)
199     {
200         final String JavaDoc output = _autoEdit(file);
201         if (output == null)
202             return false;
203         return output.trim().endsWith(" - opened for edit");
204     }
205
206     // Returns output from "p4 edit" command or null if error.
207
private static String JavaDoc _autoEdit(File file)
208     {
209         if (file == null)
210             return null;
211         if (file.isRemote())
212             return null;
213         if (!haveP4())
214             return null;
215         FastStringBuffer sb = new FastStringBuffer("p4 edit ");
216         sb.append(maybeQuote(file.canonicalPath()));
217         return command(sb.toString(), null);
218     }
219
220     public static void revert()
221     {
222         if (!checkP4Installed())
223             return;
224         final Editor editor = Editor.currentEditor();
225         final Buffer buffer = editor.getBuffer();
226         final File file = buffer.getFile();
227         if (file == null)
228             return;
229         if (buffer.isModified()) {
230             String JavaDoc prompt =
231                 "Discard changes to " + maybeQuote(file.getName()) + "?";
232             if (!editor.confirm("Revert Buffer", prompt))
233                 return;
234         }
235         final String JavaDoc cmd = "p4 revert " + maybeQuote(file.canonicalPath());
236         String JavaDoc output = command(cmd, null);
237         if (output.trim().endsWith(" - was edit, reverted")) {
238             editor.status("File reverted");
239         } else {
240             OutputBuffer buf = OutputBuffer.getOutputBuffer(output);
241             buf.setTitle(cmd);
242             editor.makeNext(buf);
243             editor.activateInOtherWindow(buf);
244         }
245         editor.reload(buffer);
246         // Update read-only status.
247
if (editor.reactivate(buffer))
248             Sidebar.repaintBufferListInAllFrames();
249     }
250
251     public static void diff()
252     {
253         if (!checkP4Installed())
254             return;
255         final Editor editor = Editor.currentEditor();
256         Buffer parentBuffer = editor.getBuffer();
257         if (parentBuffer instanceof CheckinBuffer)
258             parentBuffer = parentBuffer.getParentBuffer();
259         final File file = parentBuffer.getFile();
260         if (file == null)
261             return;
262         final String JavaDoc baseCmd = "p4 diff -f -du ";
263         final String JavaDoc name = file.getName();
264         final String JavaDoc title = baseCmd + maybeQuote(name);
265         boolean save = false;
266         if (parentBuffer.isModified()) {
267             int response = ConfirmDialog.showConfirmDialogWithCancelButton(
268                 editor, CHECK_SAVE_PROMPT, "P4 diff");
269             switch (response) {
270                 case RESPONSE_YES:
271                     save = true;
272                     break;
273                 case RESPONSE_NO:
274                     break;
275                 case RESPONSE_CANCEL:
276                     return;
277             }
278             editor.repaintNow();
279         }
280         editor.setWaitCursor();
281         if (!save || parentBuffer.save()) {
282             // Kill existing diff output buffer if any for same parent buffer.
283
for (BufferIterator it = new BufferIterator(); it.hasNext();) {
284                 Buffer b = it.nextBuffer();
285                 if (b instanceof DiffOutputBuffer) {
286                     if (b.getParentBuffer() == parentBuffer) {
287                         editor.maybeKillBuffer(b);
288                         break; // There should be one at most.
289
}
290                 }
291             }
292             final String JavaDoc cmd = baseCmd + maybeQuote(file.canonicalPath());
293             final String JavaDoc output = command(cmd, null);
294             DiffOutputBuffer buf = new DiffOutputBuffer(parentBuffer, output, VC_P4);
295             buf.setTitle(title);
296             Editor otherEditor = editor.getOtherEditor();
297             if (otherEditor != null)
298                 buf.setUnsplitOnClose(otherEditor.getBuffer().unsplitOnClose());
299             else
300                 buf.setUnsplitOnClose(true);
301             editor.makeNext(buf);
302             editor.activateInOtherWindow(buf);
303             editor.setDefaultCursor();
304         }
305     }
306
307     public static void diffDir()
308     {
309         if (!checkP4Installed())
310             return;
311         final Editor editor = Editor.currentEditor();
312         final Buffer buffer = editor.getBuffer();
313         editor.setWaitCursor();
314         final String JavaDoc cmd = "p4 diff -du";
315         final File directory = buffer.getCurrentDirectory();
316         // Kill existing diff output buffer if any for same directory.
317
for (BufferIterator it = new BufferIterator(); it.hasNext();) {
318             Buffer b = it.nextBuffer();
319             if (b instanceof DiffOutputBuffer) {
320                 if (directory.equals(((DiffOutputBuffer) b).getDirectory())) {
321                     editor.maybeKillBuffer(b);
322                     break; // There should be one at most.
323
}
324             }
325         }
326         final String JavaDoc output = command(cmd, directory);
327         DiffOutputBuffer buf = new DiffOutputBuffer(directory, output, VC_P4);
328         if (buf != null) {
329             buf.setTitle(cmd);
330             editor.makeNext(buf);
331             editor.activateInOtherWindow(buf);
332         }
333         editor.setDefaultCursor();
334     }
335
336     public static void log()
337     {
338         log(null);
339     }
340
341     public static void log(String JavaDoc args)
342     {
343         final Editor editor = Editor.currentEditor();
344         final Buffer parentBuffer = editor.getBuffer();
345         boolean useCurrentFile = true;
346         List JavaDoc list = Utilities.tokenize(args);
347         for (int i = 0; i < list.size(); i++) {
348             String JavaDoc arg = (String JavaDoc) list.get(i);
349             if (arg.charAt(0) != '-') {
350                 // Must be a filename. Use its canonical path.
351
File file =
352                     File.getInstance(parentBuffer.getCurrentDirectory(), arg);
353                 list.set(i, file.canonicalPath());
354                 useCurrentFile = false;
355             } else if (arg.equals("-l")) {
356                 // We use this option by default.
357
list.set(i, "");
358             }
359         }
360         final String JavaDoc baseCmd = "p4 filelog -l ";
361         final String JavaDoc title;
362         FastStringBuffer sb = new FastStringBuffer(baseCmd);
363         for (int i = 0; i < list.size(); i++) {
364             sb.append((String JavaDoc)list.get(i));
365             sb.append(' ');
366         }
367         if (useCurrentFile) {
368             File file = parentBuffer.getFile();
369             if (file == null)
370                 return;
371             sb.append(maybeQuote(file.canonicalPath()));
372             title = baseCmd + maybeQuote(file.getName());
373         } else
374             title = sb.toString();
375         final String JavaDoc cmd = sb.toString();
376         editor.setWaitCursor();
377         final String JavaDoc output = command(cmd, null);
378         OutputBuffer buf = OutputBuffer.getOutputBuffer(output);
379         buf.setTitle(title);
380         editor.makeNext(buf);
381         editor.activateInOtherWindow(buf);
382         editor.setDefaultCursor();
383     }
384
385     public static void change(String JavaDoc arg)
386     {
387         arg = arg.trim();
388         try {
389             // Make sure arg is a number.
390
Integer.parseInt(arg);
391             _change(arg);
392         }
393         catch (NumberFormatException JavaDoc e) {
394             MessageDialog.showMessageDialog(
395                 "Argument must be a changelist number.",
396                 "Error");
397         }
398     }
399
400     public static void change()
401     {
402         _change(null);
403     }
404
405     // arg must be a changelist number or null.
406
private static void _change(String JavaDoc arg)
407     {
408         if (!checkP4Installed())
409             return;
410         final Editor editor = Editor.currentEditor();
411         Buffer parentBuffer = editor.getBuffer();
412         if (parentBuffer instanceof DiffOutputBuffer) {
413             Log.debug("parentBuffer is DiffOutputBuffer");
414             parentBuffer = parentBuffer.getParentBuffer();
415             Log.debug("==> parentBuffer is " + parentBuffer);
416         }
417         if (parentBuffer == null)
418             return;
419         if (parentBuffer.getFile() == null)
420             return;
421         FastStringBuffer sb = new FastStringBuffer("p4 change");
422         if (arg != null) {
423             sb.append(' ');
424             sb.append(arg);
425         }
426         final String JavaDoc title = sb.toString();
427         CheckinBuffer checkinBuffer = null;
428         for (BufferIterator it = new BufferIterator(); it.hasNext();) {
429             Buffer buf = it.nextBuffer();
430             if (buf instanceof CheckinBuffer) {
431                 if (title.equals(buf.getTitle())) {
432                     checkinBuffer = (CheckinBuffer) buf;
433                     break;
434                 }
435             }
436         }
437         if (checkinBuffer == null) {
438             checkinBuffer = new CheckinBuffer(parentBuffer, VC_P4, true);
439             checkinBuffer.setProperty(Property.USE_TABS, true);
440             checkinBuffer.setFormatter(new P4ChangelistFormatter(checkinBuffer));
441             checkinBuffer.setTitle(title);
442             sb.setText("p4 change -o");
443             if (arg != null) {
444                 sb.append(' ');
445                 sb.append(arg);
446             }
447             ShellCommand shellCommand = new ShellCommand(sb.toString());
448             shellCommand.run();
449             checkinBuffer.setText(shellCommand.getOutput());
450             Position dot = findStartOfComment(checkinBuffer);
451             if (dot != null) {
452                 Position mark = findEndOfComment(checkinBuffer, dot);
453                 View view = new View();
454                 view.setDot(dot);
455                 view.setCaretCol(checkinBuffer.getCol(dot));
456                 if (mark != null)
457                     view.setMark(mark);
458                 checkinBuffer.setLastView(view);
459             }
460         }
461         editor.makeNext(checkinBuffer);
462         editor.activateInOtherWindow(checkinBuffer);
463     }
464
465     public static void submit(String JavaDoc args)
466     {
467         String JavaDoc message = null;
468         List JavaDoc list = Utilities.tokenize(args);
469         if (list.size() == 2) {
470             String JavaDoc arg = (String JavaDoc) list.get(0);
471             if (arg.equals("-c")) {
472                 arg = (String JavaDoc) list.get(1);
473                 try {
474                     Integer.parseInt(arg);
475                     // Success!
476
_submit(arg);
477                     return;
478                 }
479                 catch (NumberFormatException JavaDoc e) {
480                     message = "Invalid changelist number";
481                 }
482             }
483         }
484         if (message == null) {
485             FastStringBuffer sb =
486                 new FastStringBuffer("Unrecognized argument \"");
487             sb.append(args.trim());
488             sb.append('"');
489             message = sb.toString();
490         }
491         MessageDialog.showMessageDialog(message, "Error");
492     }
493
494     public static void submit()
495     {
496         _submit(null);
497     }
498
499     // arg must be a changelist number or null.
500
private static void _submit(String JavaDoc arg)
501     {
502         if (!checkP4Installed())
503             return;
504         final Editor editor = Editor.currentEditor();
505         Buffer parentBuffer = editor.getBuffer();
506         if (parentBuffer instanceof DiffOutputBuffer)
507             parentBuffer = parentBuffer.getParentBuffer();
508         FastStringBuffer sb = new FastStringBuffer("p4 submit");
509         if (arg != null) {
510             sb.append(" -c ");
511             sb.append(arg);
512         }
513         final String JavaDoc title = sb.toString();
514         boolean save = false;
515         List JavaDoc list = getModifiedBuffers();
516         if (list != null && list.size() > 0) {
517             int response =
518                 ConfirmDialog.showConfirmDialogWithCancelButton(editor,
519                     "Save modified buffers first?", title);
520             switch (response) {
521                 case RESPONSE_YES:
522                     save = true;
523                     break;
524                 case RESPONSE_NO:
525                     break;
526                 case RESPONSE_CANCEL:
527                     return;
528             }
529             editor.repaintNow();
530         }
531         if (!save || saveModifiedBuffers(editor, list)) {
532             // Look for existing checkin buffer before making a new one.
533
CheckinBuffer checkinBuffer = null;
534             for (BufferIterator it = new BufferIterator(); it.hasNext();) {
535                 Buffer buf = it.nextBuffer();
536                 if (buf instanceof CheckinBuffer) {
537                     if (title.equals(buf.getTitle())) {
538                         checkinBuffer = (CheckinBuffer) buf;
539                         break;
540                     }
541                 }
542             }
543             if (checkinBuffer == null) {
544                 checkinBuffer = new CheckinBuffer(parentBuffer, VC_P4);
545                 checkinBuffer.setProperty(Property.USE_TABS, true);
546                 checkinBuffer.setFormatter(new P4ChangelistFormatter(checkinBuffer));
547                 checkinBuffer.setTitle(title);
548                 sb.setText("p4 change -o");
549                 if (arg != null) {
550                     sb.append(' ');
551                     sb.append(arg);
552                 }
553                 ShellCommand shellCommand = new ShellCommand(sb.toString());
554                 shellCommand.run();
555                 checkinBuffer.setText(shellCommand.getOutput());
556                 Position dot = findStartOfComment(checkinBuffer);
557                 if (dot != null) {
558                     Position mark = findEndOfComment(checkinBuffer, dot);
559                     View view = new View();
560                     view.setDot(dot);
561                     view.setCaretCol(checkinBuffer.getCol(dot));
562                     if (mark != null)
563                         view.setMark(mark);
564                     checkinBuffer.setLastView(view);
565                 }
566             }
567             editor.makeNext(checkinBuffer);
568             editor.activate(checkinBuffer);
569         }
570     }
571
572     private static List JavaDoc getModifiedBuffers()
573     {
574         ArrayList JavaDoc list = null;
575         for (BufferIterator it = new BufferIterator(); it.hasNext();) {
576             Buffer buf = it.nextBuffer();
577             if (!buf.isModified())
578                 continue;
579             if (buf.isUntitled())
580                 continue;
581             final int modeId = buf.getModeId();
582             if (modeId == SEND_MAIL_MODE)
583                 continue;
584             if (modeId == CHECKIN_MODE)
585                 continue;
586             if (buf.getFile() != null && buf.getFile().isLocal()) {
587                 if (list == null)
588                     list = new ArrayList JavaDoc();
589                 list.add(buf);
590             }
591         }
592         return list;
593     }
594
595     private static boolean saveModifiedBuffers(Editor editor, List JavaDoc list)
596     {
597         editor.setWaitCursor();
598         int numErrors = 0;
599         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
600             Buffer buf = (Buffer) it.next();
601             if (buf.getFile() != null && buf.getFile().isLocal()) {
602                 editor.status("Saving modified buffers...");
603                 if (buf.getBooleanProperty(Property.REMOVE_TRAILING_WHITESPACE))
604                     buf.removeTrailingWhitespace();
605                 if (!buf.save())
606                     ++numErrors;
607             }
608         }
609         editor.setDefaultCursor();
610         if (numErrors == 0) {
611             editor.status("Saving modified buffers...done");
612             return true;
613         }
614         // User will already have seen detailed error information from Buffer.save().
615
editor.status("");
616         return false;
617     }
618
619     public static void replaceComment(final Editor editor, final String JavaDoc comment)
620     {
621         if (!(editor.getBuffer() instanceof CheckinBuffer)) {
622             Debug.bug();
623             return;
624         }
625         final CheckinBuffer buffer = (CheckinBuffer) editor.getBuffer();
626         String JavaDoc oldComment = extractComment(buffer);
627         if (oldComment.equals(comment))
628             return;
629         insertComment(editor, comment);
630     }
631
632     public static String JavaDoc extractComment(final CheckinBuffer buffer)
633     {
634         Position begin = findStartOfComment(buffer);
635         if (begin != null) {
636             Position end = findEndOfComment(buffer, begin);
637             if (end != null) {
638                 int offset1 = buffer.getAbsoluteOffset(begin);
639                 int offset2 = buffer.getAbsoluteOffset(end);
640                 if (offset1 >= 0 && offset2 > offset1) {
641                     String JavaDoc s = buffer.getText().substring(offset1, offset2);
642                     if (!s.equals("<enter description here>"))
643                         return s;
644                 }
645             }
646         }
647         return "";
648     }
649
650     private static void insertComment(final Editor editor, final String JavaDoc comment)
651     {
652         final CheckinBuffer buffer = (CheckinBuffer) editor.getBuffer();
653         Position dot = findStartOfComment(buffer);
654         if (dot == null)
655             return;
656         Position mark = findEndOfComment(buffer, dot);
657         if (mark == null)
658             return;
659         try {
660             buffer.lockWrite();
661         }
662         catch (InterruptedException JavaDoc e) {
663             Log.error(e);
664             return;
665         }
666         try {
667             CompoundEdit JavaDoc compoundEdit = editor.beginCompoundEdit();
668             editor.moveDotTo(dot);
669             editor.setMark(mark);
670             editor.deleteRegion();
671             editor.insertString(comment);
672             editor.endCompoundEdit(compoundEdit);
673             buffer.modified();
674         }
675         finally {
676             buffer.unlockWrite();
677         }
678         final Position end = findEndOfComment(buffer, null);
679         for (EditorIterator it = new EditorIterator(); it.hasNext();) {
680             Editor ed = it.nextEditor();
681             if (ed.getBuffer() == buffer) {
682                 ed.setTopLine(buffer.getFirstLine());
683                 ed.setDot(end.copy()); // No undo.
684
ed.moveCaretToDotCol();
685                 ed.setUpdateFlag(REPAINT);
686                 ed.updateDisplay();
687             }
688         }
689     }
690
691     private static Position findStartOfComment(CheckinBuffer buffer)
692     {
693         String JavaDoc s = buffer.getText();
694         String JavaDoc lookFor = "\nDescription:\n\t";
695         RE re = new UncheckedRE(lookFor);
696         REMatch match = re.getMatch(s);
697         if (match != null)
698             return buffer.getPosition(match.getStartIndex() + lookFor.length());
699         return null;
700     }
701
702     private static Position findEndOfComment(CheckinBuffer buffer, Position start)
703     {
704         String JavaDoc s = buffer.getText();
705         String JavaDoc lookFor = "\n\nFiles:\n\t";
706         RE re = new UncheckedRE(lookFor);
707         int offset = -1;
708         if (start != null)
709             offset = buffer.getAbsoluteOffset(start);
710         if (offset < 0)
711             offset = 0;
712         REMatch match = re.getMatch(s, offset);
713         if (match != null)
714             return buffer.getPosition(match.getStartIndex());
715         return null;
716     }
717
718     public static void finish(Editor editor, CheckinBuffer checkinBuffer)
719     {
720         final Buffer parentBuffer = checkinBuffer.getParentBuffer();
721         editor.getFrame().setWaitCursor();
722         final boolean editOnly = checkinBuffer.isEditOnly();
723         final String JavaDoc cmd;
724         final String JavaDoc title;
725         if (editOnly) {
726             cmd = "p4 change -i";
727             title = "Output from p4 change";
728         } else {
729             cmd = "p4 submit -i";
730             title = "Output from p4 submit";
731         }
732         final String JavaDoc input = checkinBuffer.getText();
733         ShellCommand shellCommand = new ShellCommand(cmd, null, input);
734         shellCommand.run();
735         if (shellCommand.exitValue() != 0) {
736             // Error.
737
Log.error("P4.finish input = |" + input + "|");
738             Log.error("P4.finish exit value = " + shellCommand.exitValue());
739             OutputBuffer buf = null;
740             // Re-use existing output buffer if possible.
741
for (BufferIterator it = new BufferIterator(); it.hasNext();) {
742                 Buffer b = it.nextBuffer();
743                 if (b instanceof OutputBuffer) {
744                     if (title.equals(b.getTitle())) {
745                         buf = (OutputBuffer) b;
746                         break; // There should be one at most.
747
}
748                 }
749             }
750             if (buf != null)
751                 buf.setText(shellCommand.getOutput());
752             else
753                 buf = OutputBuffer.getOutputBuffer(shellCommand.getOutput());
754             buf.setTitle(title);
755             editor.makeNext(buf);
756             editor.displayInOtherWindow(buf);
757         } else {
758             // Success. Kill old diff and output buffers, if any: their
759
// contents are no longer correct.
760
if (!editOnly && parentBuffer != null) {
761                 for (BufferIterator it = new BufferIterator(); it.hasNext();) {
762                     Buffer b = it.nextBuffer();
763                     if (b instanceof DiffOutputBuffer) {
764                         if (b.getParentBuffer() == parentBuffer) {
765                             Debug.assertTrue(Editor.getBufferList().contains(b));
766                             Log.debug("P4.finish killing diff output buffer");
767                             b.kill();
768                             Debug.assertFalse(Editor.getBufferList().contains(b));
769                             Debug.assertTrue(editor.getBuffer() != b);
770                             Editor otherEditor = editor.getOtherEditor();
771                             if (otherEditor != null)
772                                 Debug.assertTrue(otherEditor.getBuffer() != b);
773                             break; // There should be one at most.
774
}
775                     }
776                 }
777             }
778             for (BufferIterator it = new BufferIterator(); it.hasNext();) {
779                 Buffer b = it.nextBuffer();
780                 if (b instanceof OutputBuffer) {
781                     if (title.equals(b.getTitle())) {
782                         editor.maybeKillBuffer(b);
783                         break; // One at most.
784
}
785                 }
786             }
787             if (!editOnly)
788                 // Read-only status of some buffers may have changed.
789
editor.getFrame().reactivate();
790             editor.otherWindow();
791             editor.unsplitWindow();
792             checkinBuffer.kill();
793         }
794         editor.getFrame().setDefaultCursor();
795     }
796
797     public static String JavaDoc getStatusString(File file)
798     {
799         if (file != null && haveP4()) {
800             FastStringBuffer sb = null;
801             String JavaDoc output =
802                 command("p4 fstat ".concat(file.canonicalPath()), null);
803             String JavaDoc HAVE_REV = "... haveRev ";
804             int begin = output.indexOf(HAVE_REV);
805             if (begin >= 0) {
806                 begin += HAVE_REV.length();
807                 int end = output.indexOf('\n', begin);
808                 if (end > begin) {
809                     if (sb == null)
810                         sb = new FastStringBuffer("Perforce");
811                     sb.append(" revision ");
812                     sb.append(output.substring(begin, end).trim());
813                 }
814             }
815             String JavaDoc ACTION = "... action ";
816             begin = output.indexOf(ACTION);
817             if (begin >= 0) {
818                 begin += ACTION.length();
819                 int end = output.indexOf('\n', begin);
820                 if (end > begin) {
821                     if (sb == null)
822                         sb = new FastStringBuffer("Perforce");
823                     sb.append(" (opened for ");
824                     sb.append(output.substring(begin, end).trim());
825                     sb.append(')');
826                 }
827             }
828             if (sb != null)
829                 return sb.toString();
830         }
831         return null;
832     }
833
834     // Implementation.
835
private static final String JavaDoc command(String JavaDoc cmd, File workingDirectory)
836     {
837         ShellCommand shellCommand = new ShellCommand(cmd, workingDirectory);
838         shellCommand.run();
839         return shellCommand.getOutput();
840     }
841
842     private static boolean checkP4Installed()
843     {
844         if (haveP4())
845             return true;
846         MessageDialog.showMessageDialog(
847             "The Perforce command-line client does not appear to be in your PATH.",
848             "Error");
849         return false;
850     }
851
852     private static int haveP4 = -1;
853
854     private static boolean haveP4()
855     {
856         if (haveP4 > 0)
857             return true;
858         if (Utilities.have("p4")) {
859             haveP4 = 1; // Cache positive result.
860
return true;
861         }
862         return false;
863     }
864
865     // Enclose string in quotes if it contains any embedded spaces.
866
private static String JavaDoc maybeQuote(String JavaDoc s)
867     {
868         if (s.indexOf(' ') < 0)
869             return s;
870         FastStringBuffer sb = new FastStringBuffer('"');
871         sb.append(s);
872         sb.append('"');
873         return sb.toString();
874     }
875 }
876
Popular Tags