/*
*
* Perforce/JBuilder Opentool
* Copyright (C) 2002 Mark Ackerman
*
* 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 com.dafreels.opentools.*;
import com.dafreels.opentools.actions.*;
import com.dafreels.vcs.command.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.ArrayList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
/**
* <p>Title: Perforce Open Tool </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: RMA</p>
* @author Mark Ackerman
*
*/
public class AddFilesDialog extends JDialog
{
private JComboBox _changeListCombo;
private JList _fileList;
private boolean _canceled;
private JButton _addButton;
private JButton _removeFileButton;
public AddFilesDialog(Dialog dialog, boolean modal)
{
super(dialog, modal);
buildControls();
}
public AddFilesDialog(Frame frame, boolean modal)
{
super(frame, modal);
buildControls();
}
/**
* create the controls for this dialog
*/
protected void buildControls()
{
getContentPane().setLayout(new GridBagLayout());
_removeFileButton = new JButton("Remove Selected Files");
_removeFileButton.setMnemonic('R');
_removeFileButton.setEnabled(false);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5,5,0,5);
getContentPane().add(_removeFileButton, gbc);
JLabel label = new JLabel("Add to Change:");
label.setDisplayedMnemonic('C');
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.NORTHEAST;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(7,5,0,5);
getContentPane().add(label, gbc);
_changeListCombo = new JComboBox();
_changeListCombo.setRenderer(new ToolTipComboBoxRenderer());
_changeListCombo.setToolTipText("");
label.setLabelFor(_changeListCombo);
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.NORTHEAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5,5,0,5);
getContentPane().add(_changeListCombo, gbc);
_fileList = new JList(new DefaultListModel())
{
public Dimension getPreferredScrollableViewportSize()
{
Dimension d = super.getPreferredScrollableViewportSize();
d.height = 200;
d.width = 400;
return d;
}
};
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 3;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5,5,0,5);
getContentPane().add(new JScrollPane(_fileList), gbc);
JPanel buttonPanel = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 3;
gbc.anchor = GridBagConstraints.SOUTHWEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5,5,0,5);
getContentPane().add(buttonPanel, gbc);
_addButton = new JButton("Add File(s)");
_addButton.setMnemonic('A');
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.SOUTHWEST;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5,5,0,5);
buttonPanel.add(_addButton, gbc);
JButton cancelButton = new JButton("Cancel");
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.SOUTHWEST;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5,5,0,5);
buttonPanel.add(cancelButton, gbc);
_addButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
_canceled = false;
if ("New".equals(getChangelist()))
{
newChangeList();
}
else
{
setVisible(false);
}
}
});
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
_canceled = true;
setVisible(false);
}
});
_removeFileButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
removeSelectedFiles();
}
});
_changeListCombo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ChangeItem changeItem = (ChangeItem)_changeListCombo.getSelectedItem();
if ( changeItem == null )
{
_changeListCombo.setToolTipText("");
return;
}
_changeListCombo.setToolTipText(changeItem.getDescription());
}
});
_fileList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
_removeFileButton.setEnabled(_fileList.getSelectedIndex() > -1);
}
});
pack();
setLocationRelativeTo(getParent());
getRootPane().setDefaultButton(_addButton);
}
/**
* remove the selected files from the list
*/
private void removeSelectedFiles()
{
int[] rows = _fileList.getSelectedIndices();
if ( rows == null || rows.length == 0 )
{
return;
}
DefaultListModel model = (DefaultListModel)_fileList.getModel();
for (int i = rows.length-1;i >= 0; i-- )
{
model.remove(rows[i]);
}
if ( model.getSize() == 0 )
{
_addButton.setEnabled(false);
}
}
/**
* fill the Files to add list with <code>filesToAdd</code>
* @param filesToAdd the List of files to add to the Files JList
*/
public void fillForm(java.util.List filesToAdd)
{
DefaultListModel model = new DefaultListModel();
for(int i = 0; i< filesToAdd.size(); i++ )
{
model.addElement(filesToAdd.get(i));
}
_fileList.setModel(model);
getChangeLists();
}
protected ChangeItem getNewChangeList()
{
CommandTool.runCommand("changes -s pending", Main.m_props);
MessageWriter.outputErrorMessages(MessageFormatter.getInstance());
String client = Main.m_props.getUserName()+"@"+Main.m_props.getClientSpec();
int idx, idx2;
String change, message, desc;
ChangeItem changeItem;
DefaultComboBoxModel model = (DefaultComboBoxModel)_changeListCombo.getModel();
boolean found;
while( (message = MessageFormatter.getNextMessage()) != null)
{
// format of line is
// Change 208 on 2002/05/02 by mark@Q *pending* 'ResPrm '
if ( message.indexOf(client) > -1 )
{ // a line for my client
idx = message.indexOf(" ");
idx2 = message.indexOf(" ", idx+1);
change = message.substring(idx, idx2).toLowerCase();
idx = message.indexOf("'");
desc = message.substring(idx+1, message.length()-2);
found = false;
for (int i = 0; i < model.getSize(); i++ )
{
if ( model.getElementAt(i).toString().equals(change))
{
found = true;
break;
}
}
if ( !found )
{
return new ChangeItem(change, desc);
}
}
}
return null;
}
protected void getChangeLists()
{
CommandTool.runCommand("changes -s pending", Main.m_props);
MessageWriter.outputErrorMessages(MessageFormatter.getInstance());
String client = Main.m_props.getUserName()+"@"+Main.m_props.getClientSpec();
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement(new ChangeItem("Default","Default Changelist"));
int idx, idx2;
String change, message, desc;
ChangeItem changeItem;
while( (message = MessageFormatter.getNextMessage()) != null)
{
// format of line is
// Change 208 on 2002/05/02 by mark@Q *pending* 'ResPrm '
if ( message.indexOf(client) > -1 )
{ // a line for my client
idx = message.indexOf(" ");
idx2 = message.indexOf(" ", idx+1);
change = message.substring(idx, idx2).toLowerCase();
idx = message.indexOf("'");
desc = message.substring(idx+1, message.length()-2);
changeItem = new ChangeItem(change, desc);
model.addElement(changeItem);
}
}
model.addElement(new ChangeItem("New", "Create a new ChangeList"));
_changeListCombo.setModel(model);
}
/**
* get the files to add
* @return the files to add
*/
public java.util.List getFiles()
{
DefaultListModel model = (DefaultListModel)_fileList.getModel();
ArrayList files = new ArrayList(model.getSize());
for (int i = 0; i < model.getSize(); i++ )
{
files.add(model.getElementAt(i));
}
return files;
}
/**
* get the selected change list
* if the default changelist is selected then null is returned
* @return the selected change list
*/
public String getChangelist()
{
ChangeItem change = (ChangeItem)_changeListCombo.getSelectedItem();
if ( change == null )
{
return null;
}
if ( "default".equalsIgnoreCase(change.getChangeNumber()))
{
return null;
}
return change.getChangeNumber();
}
/**
* return whether the user canceled the dialog
* @return
*/
public boolean wasCanceled()
{
return _canceled;
}
private void newChangeList()
{
SubmitAction action = new SubmitAction();
if ( action.submit(null))
{
ChangeItem changeItem = getNewChangeList();
if ( changeItem != null )
{
_changeListCombo.addItem(changeItem);
_changeListCombo.setSelectedItem(changeItem);
}
}
}
private class ChangeItem
{
private String _changeNumber;
private String _description;
public ChangeItem(String changeNumber, String description)
{
_changeNumber = changeNumber;
_description = description;
}
public String getChangeNumber()
{
return _changeNumber;
}
public String getDescription()
{
return _description;
}
public String toString()
{
return _changeNumber+ " ("+_description+")";
}
}
private class ToolTipComboBoxRenderer extends BasicComboBoxRenderer
{
public Component getListCellRendererComponent (JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus)
{
if (isSelected)
{
DefaultComboBoxModel model = (DefaultComboBoxModel)list.getModel();
if ((index >= 0) && (index < model.getSize()))
{
list.setToolTipText(((ChangeItem)model.getElementAt(index)).getDescription());
}
else
{
list.setToolTipText(null);
}
}
return super.getListCellRendererComponent (list, value, index, isSelected, cellHasFocus);
}
}
}