/*
*
* 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.ui;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import java.util.Date;
import java.text.SimpleDateFormat;
import com.borland.primetime.ide.Browser;
import com.dafreels.opentools.properties.PerforceGroup;
import com.dafreels.opentools.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.dafreels.opentools.actions.ui.FileModel;
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
* @version 1.1 Mark Ackerman - Fixed stretching of dialog. Added Popup menu.
*/
public final class SubmitDialog extends JDialog implements ActionListener
{
public static final String ENTER_DESC_HERE= "<enter description here>";
private static String[] TOGGLE_BUTTON_TEXT = {
"Deselect All Files",
"Select All Files"
};
JPanel infoPanel = new JPanel();
GridBagLayout gridBagLayout1 = new GridBagLayout();
GridBagLayout tableLayout = new GridBagLayout();
GridLayout buttonLayout = new GridLayout();
GridBagLayout gridBagLayout2 = new GridBagLayout();
GridBagLayout gridBagLayout3 = new GridBagLayout();
GridBagLayout gridBagLayout4 = new GridBagLayout();
JPanel buttonPanel = new JPanel();
JPanel descriptionPanel = new JPanel();
JPanel filesPanel = new JPanel();
JButton submit = new JButton("Submit");
JButton cancel = new JButton("Cancel");
JScrollPane descSP = new JScrollPane();
JTextArea descTA = new JTextArea()
{
public Dimension getPreferredScrollableViewportSize()
{
Dimension d = super.getPreferredScrollableViewportSize();
d.height = 400;
return d;
}
};
JScrollPane filesSP = new JScrollPane();
JLabel fileLabel = new JLabel("Files:");
JLabel descLabel = new JLabel("Description:");
JLabel changeLabel = new JLabel("Change:");
JLabel dateLabel = new JLabel("Date:");
JLabel clientLabel = new JLabel("Client:");
JLabel userLabel = new JLabel("User:");
JLabel statusLabel = new JLabel("Status:");
JTextField changeTF = new JTextField();
JTextField dateTF = new JTextField();
JTextField clientTF = new JTextField();
JTextField userTF = new JTextField();
JTextField statusTF = new JTextField();
JButton toggleSelectionButton = new JButton();
JCheckBox reopenCB = new JCheckBox();
private boolean _submitVal = false;
private FileModel _dtm = new FileModel();
private final static String newLine = System.getProperty("line.separator");
JPanel tablePanel = new JPanel();
JTable fileTable = new JTable()
{
public Dimension getPreferredSize()
{
Dimension d = super.getPreferredSize();
d.height = getRowCount()*getRowHeight()+10;
return d;
}
/*
public Dimension getPreferredScrollableViewportSize()
{
Dimension d = super.getPreferredScrollableViewportSize();
d.height = 400;
return d;
}
public int getColumnCount()
{
return 0;
}
*/
};
JPopupMenu _fileTablePopup;
public SubmitDialog(Frame frame, String title, boolean modal)
{
super(frame, title, modal);
this.adjustDialog();
try
{
jbInit();
//pack();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
private void adjustDialog()
{
//Set the size of the dialog
this.setSize(550,600);
//Position variables
double x = 0;
double y = 0;
double width = 0;
double height = 0;
//Get the position variables from the parent
if(this.getParent() != null)
{
width = this.getParent().getWidth() + this.getParent().getLocation().getX();
height = this.getParent().getHeight(); // - this.getParent().getLocation().getY();
} else //Make up the position variables
{
width = this.getToolkit().getDefaultToolkit().getScreenSize().getWidth();
height = this.getToolkit().getDefaultToolkit().getScreenSize().getHeight();
}
x = (width / 2) + (this.getSize().getWidth() / 2);
y = (height / 2) + (this.getSize().getHeight() / 2);
this.setLocation((int)x, (int)y);
setLocationRelativeTo(this.getParent());
}
public SubmitDialog()
{
this(null, "", false);
}
public void showSubmit(String changeNumber, Vector files, String description)
{
Vector columns = new Vector();
columns.add("submit");
columns.add("");
if ( description == null )
{
descTA.setText(ENTER_DESC_HERE);
descTA.selectAll();
}
else
{
descTA.setText(description);
}
//descTA.select(0, 24);
//Reset the values in the form
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
dateTF.setText(sdf.format(new Date()));
if ( changeNumber != null )
{
changeTF.setText(changeNumber);
statusTF.setText("pending");
}
else
{
changeTF.setText("new");
statusTF.setText("new");
}
clientTF.setText(PerforceGroup.CLIENTSPEC.getValue(Browser.getActiveBrowser().getActiveProject()));
userTF.setText(PerforceGroup.USERNAME.getValue(Browser.getActiveBrowser().getActiveProject()));
if(files == null)
{
_submitVal = false;
this.setModal(false);
return;
}
_dtm.setDataVector(files, columns);
tablePanel.doLayout();
fileTable.doLayout();
filesSP.doLayout();
//Resize the checkbox column
fileTable.getColumnModel().getColumn(0).setMinWidth(15);
fileTable.getColumnModel().getColumn(0).setMaxWidth(15);
setModal(true);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
descTA.requestFocus();
if ( ENTER_DESC_HERE.equals(descTA.getText()))
{
descTA.selectAll();
}
}
});
setVisible(true);
}
public boolean getReopenFiles()
{
return reopenCB.isSelected();
}
public Vector getFiles()
{
//Get the data from the table
Vector files = _dtm.getDataVector();
//Count backwards, as Vector shifts data as it is removed
for(int i = files.size() - 1; i >= 0; i--)
{
if(! ((Boolean)((Vector)files.elementAt(i)).elementAt(0)).booleanValue())
{
files.removeElementAt(i);
}
}
return files;
}
public String getDescription()
{
int lines = descTA.getLineCount();
String change = descTA.getText();
StringBuffer changeList = new StringBuffer();
//Parse the description and make sure it is tabbed
for(int i = 0; i < lines; i++)
{
if(change.indexOf("\n") != -1)
{
changeList.append("\t"+change.substring(0,change.indexOf("\n"))+newLine);
change = change.substring(change.indexOf("\n") + 1);
} else if(change.indexOf("\r\n") != -1)
{
changeList.append("\t"+change.substring(0,change.indexOf("\r\n"))+newLine);
change = change.substring(change.indexOf("\r\n") + 1);
} else
{
changeList.append("\t"+change+newLine);
}
}
return changeList.toString();
}
/**
* get the change number
* @return the change Number
*/
public String getChangeNumber()
{
return changeTF.getText();
}
/**
* get the change list status
* @return the change list status
*/
public String getStatus()
{
return statusTF.getText();
}
public boolean getState()
{
return _submitVal;
}
void jbInit() throws Exception
{
infoPanel.setLayout(gridBagLayout4);
this.getContentPane().setLayout(gridBagLayout1);
Font f = descLabel.getFont();
descLabel.setFont(new Font(f.getName(), Font.BOLD, f.getSize()));
descriptionPanel.setLayout(gridBagLayout2);
filesPanel.setLayout(gridBagLayout3);
buttonPanel.setLayout(buttonLayout);
submit.setActionCommand("submit");
submit.addActionListener(this);
cancel.setActionCommand("cancel");
cancel.addActionListener(this);
buttonLayout.setRows(3);
buttonLayout.setColumns(1);
buttonLayout.setHgap(5);
buttonLayout.setVgap(5);
descSP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
descSP.setMaximumSize(new Dimension(400, 70));
descTA.setLineWrap(true);
descTA.setWrapStyleWord(true);
descTA.setToolTipText("Comments about the ChangeList. Required");
filesSP.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
filesSP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
filesSP.setAutoscrolls(true);
filesSP.setMaximumSize(new Dimension(1000, 1000));
changeTF.setEditable(false);
changeTF.setBorder(null);
changeTF.setToolTipText("The change number. 'new' on a new changelist.");
dateTF.setEditable(false);
dateTF.setBorder(null);
dateTF.setToolTipText("The date this specification was last modified.");
clientTF.setEditable(false);
clientTF.setBorder(null);
clientTF.setToolTipText("The client on which this changelist was created");
userTF.setEditable(false);
userTF.setBorder(null);
userTF.setToolTipText("The user who created this changelist");
statusTF.setEditable(false);
statusTF.setBorder(null);
statusTF.setToolTipText("Either 'pending' or 'submitted'");
fileTable.setShowHorizontalLines(false);
fileTable.setShowVerticalLines(false);
fileTable.setPreferredSize(new Dimension(1000, 1000));
fileTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
fileTable.setCellSelectionEnabled(true);
fileTable.setColumnSelectionAllowed(true);
fileTable.setPreferredScrollableViewportSize(new Dimension(1000, 1000));
fileTable.setEditingColumn(0);
fileTable.setModel(_dtm);
fileTable.setRowSelectionAllowed(true);
fileTable.sizeColumnsToFit(1);
tablePanel.setLayout(tableLayout);
toggleSelectionButton.setText(TOGGLE_BUTTON_TEXT[0]);
reopenCB.setText("Reopen");
getContentPane().add(descriptionPanel, new GridBagConstraints(0, 1, 2, 1, 1.0, 1.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0,0));
descriptionPanel.add(descLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
descriptionPanel.add(descSP, new GridBagConstraints(0, 1, 2, 1, 1.0, 1.0
,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
descSP.getViewport().add(descTA, null);
getContentPane().add(filesPanel, new GridBagConstraints(0, 2, 3, 1, 1.0, 2.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
filesPanel.add(filesSP, new GridBagConstraints(0, 1, 2, 1, 1.0, 2.0
,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
filesPanel.add(fileLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
filesPanel.add(toggleSelectionButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
tablePanel.add(fileTable, new GridBagConstraints(0, 0, 1, 1, 1.0, 2.0
,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
filesSP.getViewport().add(tablePanel, null);
getContentPane().add(infoPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.SOUTHEAST, GridBagConstraints.HORIZONTAL, new Insets(3, 4, 3, 4), 0,0));//548, 218));
infoPanel.add(dateLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 50, 0));
infoPanel.add(clientLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 50, 0));
infoPanel.add(userLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 50, 0));
infoPanel.add(statusLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 50, 0));
infoPanel.add(changeLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 50, 0));
infoPanel.add(changeTF, new GridBagConstraints(2, 0, 4, 1, 1.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 170, 0));
infoPanel.add(dateTF, new GridBagConstraints(2, 1, 1, 1, 1.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 170, 0));
infoPanel.add(clientTF, new GridBagConstraints(2, 2, 1, 1, 1.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 170, 0));
infoPanel.add(userTF, new GridBagConstraints(2, 3, 1, 1, 1.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 170, 0));
infoPanel.add(statusTF, new GridBagConstraints(2, 4, 1, 1, 1.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 170, 0));
getContentPane().add(buttonPanel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 5, 0, 5), 3, 4));
buttonPanel.add(submit);
buttonPanel.add(cancel);
buttonPanel.add(reopenCB);
fileTable.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(java.awt.event.MouseEvent e)
{
if ( e.isPopupTrigger() || SwingUtilities.isRightMouseButton(e))
{
int row = fileTable.rowAtPoint(e.getPoint());
if ( row == -1 )
{
return;
}
fileTable.getSelectionModel().addSelectionInterval(row, row);
if ( _fileTablePopup == null )
{
buildFileTablePopup();
}
_fileTablePopup.show(fileTable, e.getX(), e.getY());
}
}
});
toggleSelectionButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Boolean bool;
boolean select = toggleSelectionButton.getText().equals(TOGGLE_BUTTON_TEXT[1]);
for (int i = 0; i < fileTable.getRowCount();i++ )
{
bool = new Boolean(select);
fileTable.setValueAt(bool, i, 0);
}
fileTable.repaint();
toggleSelectionButton.setText(select?TOGGLE_BUTTON_TEXT[0]:TOGGLE_BUTTON_TEXT[1]);
}
});
}
private void buildFileTablePopup()
{
if ( _fileTablePopup != null )
{
return;
}
_fileTablePopup = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Diff Against Depot", ActionImages.P4_DIFF);
menuItem.setActionCommand("diff");
menuItem.addActionListener(this);
_fileTablePopup.add(menuItem);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("cancel"))
{
_submitVal = false;
this.setVisible(false);
this.setModal(false);
} else if(e.getActionCommand().equals("submit"))
{
_submitVal = true;
this.setVisible(false);
this.setModal(false);
}
else if ("diff".equals(e.getActionCommand()))
{
diff();
}
}
private void diff()
{
int[] rows = fileTable.getSelectedRows();
if ( rows == null || rows.length == 0 )
{
return;
}
for (int i = 0; i < rows.length; i++ )
{
String file = (String)fileTable.getValueAt(rows[i], 1);
diffFile(file);
}
}
private void diffFile(String file)
{
// really should let the DiffAction handle this
// but I'm in a hurry right now
StringBuffer sb = new StringBuffer("diff -f ");
sb.append(file);
//Run the command
CommandTool.runCommand(sb.toString(), Main.m_props);
//Output the p4 messages
MessageWriter.outputErrorMessages(MessageFormatter.getInstance());
MessageWriter.outputMessages(MessageFormatter.getInstance());
}
}