}
@Override
public void actionPerformed(ActionEvent e) {
Box panel = Box.createVerticalBox();
final JosmTextField name = new JosmTextField(layer.getName());
panel.add(name);
JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
panel.add(filerename);
filerename.setEnabled(file != null);
if (filerename.isEnabled()) {
filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
}
final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
@Override public void selectInitialValue() {
name.requestFocusInWindow();
name.selectAll();
}
};
final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
dlg.setVisible(true);
Object answer = optionPane.getValue();
if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
(answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION))
return;
String nameText = name.getText();
if (filerename.isEnabled()) {
Main.pref.put("layer.rename-file", filerename.isSelected());
if (filerename.isSelected()) {
String newname = nameText;
if (newname.indexOf('/') == -1 && newname.indexOf('\\') == -1) {
newname = file.getParent() + File.separator + newname;
}
String oldname = file.getName();
if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
newname += oldname.substring(oldname.lastIndexOf('.'));
}
File newFile = new File(newname);
if (Main.platform.rename(file, newFile)) {
layer.setAssociatedFile(newFile);