001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006 007import java.awt.Dialog.ModalityType; 008import java.awt.event.ActionEvent; 009import java.io.File; 010 011import javax.swing.AbstractAction; 012import javax.swing.Box; 013import javax.swing.JCheckBox; 014import javax.swing.JDialog; 015import javax.swing.JOptionPane; 016 017import org.openstreetmap.josm.Main; 018import org.openstreetmap.josm.gui.layer.Layer; 019import org.openstreetmap.josm.gui.widgets.JosmTextField; 020import org.openstreetmap.josm.tools.ImageProvider; 021 022/** 023 * Action to rename an specific layer. Provides the option to rename the 024 * file, this layer was loaded from as well (if it was loaded from a file). 025 * 026 * @author Imi 027 */ 028public class RenameLayerAction extends AbstractAction { 029 030 private final File file; 031 private final transient Layer layer; 032 033 /** 034 * Constructs a new {@code RenameLayerAction}. 035 * @param file The file of the original location of this layer. 036 * If null, no possibility to "rename the file as well" is provided. 037 * @param layer layer to rename 038 */ 039 public RenameLayerAction(File file, Layer layer) { 040 super(tr("Rename layer"), ImageProvider.get("dialogs", "edit")); 041 this.file = file; 042 this.layer = layer; 043 this.putValue("help", ht("/Action/RenameLayer")); 044 } 045 046 static class InitialValueOptionPane extends JOptionPane { 047 InitialValueOptionPane(Box panel, JosmTextField initial) { 048 super(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, initial); 049 } 050 051 @Override 052 public void selectInitialValue() { 053 JosmTextField initial = (JosmTextField) getInitialValue(); 054 initial.requestFocusInWindow(); 055 initial.selectAll(); 056 } 057 } 058 059 @Override 060 public void actionPerformed(ActionEvent e) { 061 Box panel = Box.createVerticalBox(); 062 final JosmTextField name = new JosmTextField(layer.getName()); 063 panel.add(name); 064 JCheckBox filerename = new JCheckBox(tr("Also rename the file")); 065 panel.add(filerename); 066 filerename.setEnabled(file != null); 067 if (filerename.isEnabled()) { 068 filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true)); 069 } 070 071 final JOptionPane optionPane = new InitialValueOptionPane(panel, name); 072 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer")); 073 dlg.setModalityType(ModalityType.DOCUMENT_MODAL); 074 dlg.setVisible(true); 075 076 Object answer = optionPane.getValue(); 077 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE || 078 (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION)) 079 return; 080 081 String nameText = name.getText(); 082 if (filerename.isEnabled()) { 083 Main.pref.put("layer.rename-file", filerename.isSelected()); 084 if (filerename.isSelected()) { 085 String newname = nameText; 086 if (newname.indexOf('/') == -1 && newname.indexOf('\\') == -1) { 087 newname = file.getParent() + File.separator + newname; 088 } 089 String oldname = file.getName(); 090 if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) { 091 newname += oldname.substring(oldname.lastIndexOf('.')); 092 } 093 File newFile = new File(newname); 094 if (!SaveActionBase.confirmOverwrite(newFile)) 095 return; 096 if (Main.platform.rename(file, newFile)) { 097 layer.setAssociatedFile(newFile); 098 if (!layer.isRenamed()) { 099 nameText = newFile.getName(); 100 } 101 } else { 102 JOptionPane.showMessageDialog( 103 Main.parent, 104 tr("Could not rename file ''{0}''", file.getPath()), 105 tr("Error"), 106 JOptionPane.ERROR_MESSAGE 107 ); 108 return; 109 } 110 } 111 } 112 layer.rename(nameText); 113 Main.parent.repaint(); 114 } 115}