/*
*
* Perforce/JBuilder Opentool
* Copyright (C) 2001 David Freels
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package com.dafreels.opentools.actions;
//JBuilder
import com.borland.primetime.*;
import com.borland.primetime.actions.*;
import com.borland.primetime.node.*;
import com.borland.jbuilder.*;
import com.borland.primetime.ide.*;
//Java
import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;
//JBuilder OT
import com.dafreels.opentools.actions.ui.SubmitDialog;
import com.dafreels.opentools.properties.*;
import com.dafreels.opentools.*;
//Perforce VCS
import com.dafreels.vcs.command.*;
import com.dafreels.vcs.util.ActionImages;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company: DF Systems
* @author David Freels
* @version 1.0
*/
public class SubmitAction extends BrowserAction
{
private final static String newLine = System.getProperty("line.separator");
private SubmitDialog _sd;
public SubmitAction()
{
super("Submit");
setLongText("Submit Changelist");
super.setSmallIcon(ActionImages.P4_SUBMIT);
}
public void actionPerformed(Browser browser)
{
Node[] nodes = browser.getProjectView().getSelectedNodes(browser.getActiveProject());
//Get a list of the selected nodes
if ( submit(null) )
{
//Force JBuilder to check the file status
Main.refreshNodes(nodes);
}
}
public boolean submit(String changeNumber)
{
return submit(changeNumber, null);
}
public boolean submit(String changeNumber, java.util.List filesToSubmit)
{
//Get the change list
StringBuffer changeList = new StringBuffer("change -o ");
if ( changeNumber != null )
{
//changeList.append(" -c");
changeList.append(changeNumber);
}
else
{ // default change list
//changeList.append(" -c");
//changeList.append("default");
}
CommandTool.runCommand(changeList.toString(), false, false, false, Main.m_props);
String cl = CommandTool.getOutput();
MessageFormatter formatter = MessageFormatter.getInstance();
String message;
/*
while( (message = formatter.getNextMessage()) != null)
{
Browser.getActiveBrowser().getMessageView().addMessage(Main.PERFORCEMESSAGES,
new Message(message));
}
*/
//Browser.getActiveBrowser().getMessageView().addMessage(Main.PERFORCEMESSAGES,
// new Message(cl));
java.util.Vector data = new java.util.Vector();
java.util.Vector files = new java.util.Vector();
java.util.Vector dataInteg = new java.util.Vector();
//Create a file list
String tmp = "";
//while(cl.indexOf(newLine) != -1)
//Get a list of files
int idx;
StringBuffer desc = new StringBuffer();
while( (tmp = formatter.getNextMessage()) != null)
{
data = new java.util.Vector();
//tmp = cl.substring(0, cl.indexOf(newLine));
//cl = cl.substring(cl.indexOf(newLine) + newLine.length());
if ( tmp.startsWith("Description:"))
{
while ( (tmp = formatter.getNextMessage()) != null )
{
if ( tmp.startsWith("Files:"))
{
break;
}
desc.append(tmp);
desc.append('\n');
}
continue;
}
tmp = tmp.trim();
if((idx = tmp.lastIndexOf("//")) != -1)
{
tmp = tmp.substring(idx);//tmp.lastIndexOf("//"));
}
if((idx = tmp.indexOf("#")) > -1)
{
tmp = tmp.substring(0, idx); //tmp.indexOf("#"));
}
tmp = tmp.trim();
if(!dataInteg.contains(tmp) && tmp.indexOf("//") == 0)
{
if ( filesToSubmit != null )
{
if ( filesToSubmit.contains(tmp))
{
data.add(new Boolean("true"));
}
else
{
data.add(new Boolean("false"));
}
}
else
{
data.add(new Boolean("true"));
}
tmp = tmp.trim();
data.add(tmp);
dataInteg.add(tmp);
files.add(data);
}
}
//This is no longer needed
dataInteg = null;
tmp = null;
if ( _sd == null )
{
_sd= new SubmitDialog(Browser.getActiveBrowser(), "Perforce Change Specification", false);
}
//Displat the dialog
_sd.showSubmit(changeNumber, files, desc.toString().trim());
//Return if the user clicks cancel
if(!_sd.getState())
{
return false;
}
// should we reopen the files? do we relock them? how to efficiently?
boolean reopenFiles = _sd.getReopenFiles();
//Create the changeList
changeList.setLength(0);
changeList.append("Change: ");
changeList.append(_sd.getChangeNumber());
changeList.append(newLine);
changeList.append(" "+newLine);
changeList.append("Client: "+PerforceGroup.CLIENTSPEC.getValue(Browser.getActiveBrowser().getActiveProject())+newLine);
changeList.append(" "+newLine);
changeList.append("User: "+PerforceGroup.USERNAME.getValue(Browser.getActiveBrowser().getActiveProject())+newLine);
changeList.append(" "+newLine);
changeList.append("Status: ");
changeList.append(_sd.getStatus());
changeList.append(newLine);
changeList.append(" "+newLine);
changeList.append("Description: "+newLine);
//Add the description given by the user
changeList.append(_sd.getDescription());
changeList.append(" "+newLine);
changeList.append("Files: "+newLine);
//Get the files list from the dialog
files = _sd.getFiles();
Vector v;
//Add the files to the changeList
for(int i = 0; i < files.size(); i++)
{
v = (Vector) files.elementAt(i);
changeList.append("\t"+v.elementAt(1)+newLine);
}
//Create the command
StringBuffer sb = new StringBuffer("submit -i");
//Run the command
CommandTool.runCommand(sb.toString(), true, true, false, Main.m_props);
while(!CommandTool.isStreamReady())
{
//do nothing
}
System.out.println(changeList.toString());
CommandTool.writeToOut(changeList.toString());
//Output the p4 messages
boolean errors = MessageFormatter.getInstance().getErrorMessageCount() > 0;
MessageWriter.outputErrorMessages(MessageFormatter.getInstance());
//if ( Main.m_props.showOutput())
if ( errors || Main.m_props.showOutput())
{
MessageWriter.outputMessages(MessageFormatter.getInstance());
}
if ( reopenFiles )
{
com.dafreels.vcs.command.Command cmd =
new com.dafreels.vcs.command.Command(com.dafreels.vcs.command.Command.EDIT);
for(int i = 0; i < files.size(); i++)
{
v = (Vector) files.elementAt(i);
cmd.addPath(v.elementAt(1).toString());
}
CommandTool.runCommand(cmd, Main.m_props);
MessageWriter.outputErrorMessages(MessageFormatter.getInstance());
if ( Main.m_props.showOutput())
{
MessageWriter.outputMessages(MessageFormatter.getInstance());
}
}
return true;
}
}