//////////////////////////
public void itemStateChanged(ItemEvent e) {
int newFormatIndex;
FilePathField pathField = getPathField();
// Updates the GUI if, and only if, the format selection has changed.
if(lastFormatIndex != (newFormatIndex = formatsComboBox.getSelectedIndex())) {
String fileName = pathField.getText(); // Name of the destination archive file.
String oldFormatExtension = Archiver.getFormatExtension(formats[lastFormatIndex]); // Old/current format's extension
if(fileName.endsWith("." + oldFormatExtension)) {
int selectionStart;
int selectionEnd;
// Saves the old selection.
selectionStart = pathField.getSelectionStart();
selectionEnd = pathField.getSelectionEnd();
// Computes the new file name.
fileName = fileName.substring(0, fileName.length() - oldFormatExtension.length()) +
Archiver.getFormatExtension(formats[newFormatIndex]);
// Makes sure that the selection stays somewhat coherent.
if(selectionEnd == pathField.getText().length())
selectionEnd = fileName.length();
// Resets the file path field.
pathField.setText(fileName);
pathField.setSelectionStart(selectionStart);
pathField.setSelectionEnd(selectionEnd);
}
commentArea.setEnabled(Archiver.formatSupportsComment(formats[formatsComboBox.getSelectedIndex()]));
lastFormatIndex = newFormatIndex;
}
// Transfer focus back to the text field
pathField.requestFocus();
}