1 21 22 package org.armedbear.j; 23 24 import java.util.List ; 25 import javax.swing.SwingUtilities ; 26 import javax.swing.undo.CompoundEdit ; 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 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 name = buffer.getFile().getName(); 46 FastStringBuffer sb = new FastStringBuffer("cvs "); 47 sb.append(args); 48 if (!args.trim().startsWith("-H ")) { 50 sb.append(' '); 51 if (name.indexOf(' ') >= 0) { 52 sb.append('"'); 55 sb.append(name); 56 sb.append('"'); 57 } else 58 sb.append(name); 59 } 60 final String cmd = sb.toString(); 61 Runnable commandRunnable = new Runnable () { 62 public void run() 63 { 64 final String output = 65 command(cmd, buffer.getCurrentDirectory()); 66 Runnable completionRunnable = new Runnable () { 67 public void run() 68 { 69 cvsCompleted(editor, buffer, cmd, output); 70 } 71 }; 72 SwingUtilities.invokeLater(completionRunnable); 73 } 74 }; 75 new Thread (commandRunnable).start(); 76 } 77 78 private static void cvsCompleted(Editor editor, Buffer buffer, String cmd, 79 String 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 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 name = buffer.getFile().getName(); 112 FastStringBuffer sb = new FastStringBuffer("cvs add "); 113 if (name.indexOf(' ') >= 0) { 114 sb.append('"'); 117 sb.append(name); 118 sb.append('"'); 119 } else 120 sb.append(name); 121 final String cmd = sb.toString(); 122 Runnable commandRunnable = new Runnable () { 123 public void run() 124 { 125 final String output = 126 command(cmd, buffer.getCurrentDirectory()); 127 Runnable completionRunnable = new Runnable () { 128 public void run() 129 { 130 addCompleted(editor, buffer, cmd, output); 131 } 132 }; 133 SwingUtilities.invokeLater(completionRunnable); 134 } 135 }; 136 new Thread (commandRunnable).start(); 137 } 138 139 private static void addCompleted(Editor editor, Buffer buffer, 140 String cmd, String 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 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 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 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 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 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 sb.append('"'); 231 sb.append(name); 232 sb.append('"'); 233 } else 234 sb.append(name); 235 final String 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 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; } 262 } 263 } 264 final Buffer finalParentBuffer = parentBuffer; 265 Runnable commandRunnable = new Runnable () { 266 public void run() 267 { 268 final String output = 269 command(cmd, finalParentBuffer.getCurrentDirectory()); 270 Runnable completionRunnable = new Runnable () { 271 public void run() 272 { 273 diffCompleted(editor, finalParentBuffer, cmd, 274 output); 275 } 276 }; 277 SwingUtilities.invokeLater(completionRunnable); 278 } 279 }; 280 new Thread (commandRunnable).start(); 281 } 282 } 283 284 private static void diffCompleted(Editor editor, Buffer parentBuffer, 285 String cmd, String 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 cmd = "cvs diff -u"; 312 final File directory = buffer.getCurrentDirectory(); 313 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; } 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 commandRunnable = new Runnable () { 331 public void run() 332 { 333 final String output = command(cmd, directory); 334 Runnable completionRunnable = new Runnable () { 335 public void run() 336 { 337 diffDirCompleted(buf, output); 338 } 339 }; 340 SwingUtilities.invokeLater(completionRunnable); 341 } 342 }; 343 new Thread (commandRunnable).start(); 344 } 345 346 private static void diffDirCompleted(Buffer buffer, String 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 comment) 362 { 363 if (!(editor.getBuffer() instanceof CheckinBuffer)) { 364 Debug.bug(); 365 return; 366 } 367 final CheckinBuffer buffer = (CheckinBuffer) editor.getBuffer(); 368 String oldComment = extractComment(buffer); 369 if (oldComment.equals(comment)) 370 return; 371 insertComment(editor, comment); 372 } 373 374 public static String extractComment(final CheckinBuffer buffer) 375 { 376 return buffer.getText(); 377 } 378 379 private static void insertComment(final Editor editor, final String comment) 380 { 381 final CheckinBuffer buffer = (CheckinBuffer) editor.getBuffer(); 382 try { 383 buffer.lockWrite(); 384 } 385 catch (InterruptedException e) { 386 Log.error(e); 387 return; 388 } 389 try { 390 CompoundEdit 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()); 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 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 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 cmd = sb.toString(); 437 editor.setWaitCursor(); 438 checkinBuffer.setBusy(true); 439 parentBuffer.setBusy(true); 440 Runnable commandRunnable = new Runnable () { 441 public void run() 442 { 443 final CvsCommand cvsCommand = new CvsCommand(cmd, 444 parentBuffer.getCurrentDirectory()); 445 cvsCommand.run(); 446 tempFile.delete(); 447 Runnable completionRunnable = new Runnable () { 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 (commandRunnable).start(); 458 } 459 460 private static void finishCompleted(Editor editor, Buffer checkinBuffer, 461 Buffer parentBuffer, CvsCommand cvsCommand, String name, File tempFile) 462 { 463 checkinBuffer.setBusy(false); 464 if (cvsCommand.exitValue() != 0) { 465 OutputBuffer buf = OutputBuffer.getOutputBuffer(cvsCommand.getOutput()); 467 buf.setTitle("cvs commit ".concat(name)); 468 editor.makeNext(buf); 469 editor.activate(buf); 470 } else { 471 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; } 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 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 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 args) 513 { 514 boolean useCurrentFile = true; 515 List list = Utilities.tokenize(args); 516 for (int i = 0; i < list.size(); i++) { 517 String arg = (String ) list.get(i); 518 if (arg.charAt(0) != '-') { 519 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 name = parentBuffer.getFile().getName(); 532 sb.append(' '); 533 if (name.indexOf(' ') >= 0) { 534 sb.append('"'); 537 sb.append(name); 538 sb.append('"'); 539 } else 540 sb.append(name); 541 } 542 final String cmd = sb.toString(); 543 editor.setWaitCursor(); 544 final String 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 private static final String command(String 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 cmd; 563 final private File workingDirectory; 564 private ShellCommand shellCommand; 565 566 public CvsCommand(String 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 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 |