001: /*
002: */
003: package gruntspud;
004:
005: import gruntspud.event.GruntspudCVSAdapter;
006:
007: import java.io.File;
008: import java.io.FileOutputStream;
009: import java.io.IOException;
010: import java.io.PrintWriter;
011:
012: import javax.swing.JFileChooser;
013: import javax.swing.JOptionPane;
014:
015: import org.netbeans.lib.cvsclient.command.Command;
016: import org.netbeans.lib.cvsclient.event.MessageEvent;
017:
018: /**
019: * @author magicthize
020: *
021: * To change the template for this generated type comment go to
022: * Window - Preferences - Java - Code Generation - Code and Comments
023: */
024: public class FileGruntspudCVSListener extends GruntspudCVSAdapter {
025: private FileOutputStream out;
026: private File file;
027: private GruntspudContext context;
028: private PrintWriter writer;
029: private StringBuffer taggedLine = new StringBuffer();
030:
031: public FileGruntspudCVSListener(GruntspudContext context) {
032: this .context = context;
033: }
034:
035: /* (non-Javadoc)
036: * @see gruntspud.event.GruntspudCVSListener#getShortName()
037: */
038: public String getShortName() {
039: return "File";
040: }
041:
042: /* (non-Javadoc)
043: * @see org.netbeans.lib.cvsclient.event.CVSListener#messageSent(org.netbeans.lib.cvsclient.event.MessageEvent)
044: */
045: public void messageSent(MessageEvent evt) {
046: String line = evt.getMessage();
047: String mesg = evt.isTagged() ? evt.parseTaggedMessage(
048: taggedLine, evt.getMessage()) : line;
049: if (writer != null && mesg != null && mesg.length() > 0) {
050: if (!evt.isError()) {
051: writer.println(mesg);
052: }
053: }
054: }
055:
056: /* (non-Javadoc)
057: * @see gruntspud.event.GruntspudCVSListener#isOptionalListener()
058: */
059: public boolean isOptionalListener() {
060: return true;
061: }
062:
063: /* (non-Javadoc)
064: * @see gruntspud.event.GruntspudCVSListener#commandUnitFinished()
065: */
066: public void commandUnitFinished() {
067: if (writer != null) {
068: writer.flush();
069: }
070: GruntspudUtil.closeStream(out);
071: out = null;
072: file = null;
073: writer = null;
074: }
075:
076: /* (non-Javadoc)
077: * @see gruntspud.event.GruntspudCVSListener#commandUnitStarted(org.netbeans.lib.cvsclient.command.Command)
078: */
079: public void commandUnitStarted(Command command) {
080: File f = new File(context.getHost().getProperty(
081: Constants.LAST_SAVE_TO_FILE,
082: System.getProperty("user.home") + File.separator
083: + "cvs-console-output.log"));
084: JFileChooser chooser = new JFileChooser(f.getParent());
085: chooser.setSelectedFile(f);
086: chooser.setDialogTitle("Choose file to save output to");
087: if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
088: file = chooser.getSelectedFile();
089: if (file.exists()
090: && JOptionPane.showConfirmDialog(null,
091: "File exists, are you sure?",
092: "Confirm overwrite",
093: JOptionPane.YES_NO_OPTION,
094: JOptionPane.WARNING_MESSAGE) == JOptionPane.NO_OPTION) {
095: return;
096: }
097: context.getHost().setProperty(Constants.LAST_SAVE_TO_FILE,
098: file.getAbsolutePath());
099: try {
100: out = new FileOutputStream(file);
101: writer = new PrintWriter(out);
102: taggedLine.setLength(0);
103: } catch (IOException ioe) {
104: Constants.IO_LOG.error(ioe);
105: }
106: }
107: }
108:
109: /* (non-Javadoc)
110: * @see gruntspud.event.GruntspudCVSListener#isSelectedByDefault()
111: */
112: public boolean isSelectedByDefault() {
113: return false;
114: }
115: }
|