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 File file;
031    private transient Layer layer;
032
033    /**
034     * @param file The file of the original location of this layer.
035     *      If null, no possibility to "rename the file as well" is provided.
036     */
037    public RenameLayerAction(File file, Layer layer) {
038        super(tr("Rename layer"), ImageProvider.get("dialogs", "edit"));
039        this.file = file;
040        this.layer = layer;
041        this.putValue("help", ht("/Action/RenameLayer"));
042    }
043
044    @Override
045    public void actionPerformed(ActionEvent e) {
046        Box panel = Box.createVerticalBox();
047        final JosmTextField name = new JosmTextField(layer.getName());
048        panel.add(name);
049        JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
050        panel.add(filerename);
051        filerename.setEnabled(file != null);
052        if (filerename.isEnabled()) {
053            filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
054        }
055
056        final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
057            @Override public void selectInitialValue() {
058                name.requestFocusInWindow();
059                name.selectAll();
060            }
061        };
062        final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
063        dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
064        dlg.setVisible(true);
065
066        Object answer = optionPane.getValue();
067        if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
068                (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))
069            return;
070
071        String nameText = name.getText();
072        if (filerename.isEnabled()) {
073            Main.pref.put("layer.rename-file", filerename.isSelected());
074            if (filerename.isSelected()) {
075                String newname = nameText;
076                if (newname.indexOf('/') == -1 && newname.indexOf('\\') == -1) {
077                    newname = file.getParent() + File.separator + newname;
078                }
079                String oldname = file.getName();
080                if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
081                    newname += oldname.substring(oldname.lastIndexOf('.'));
082                }
083                File newFile = new File(newname);
084                if (Main.platform.rename(file, newFile)) {
085                    layer.setAssociatedFile(newFile);
086                    nameText = newFile.getName();
087                } else {
088                    JOptionPane.showMessageDialog(
089                            Main.parent,
090                            tr("Could not rename file ''{0}''", file.getPath()),
091                            tr("Error"),
092                            JOptionPane.ERROR_MESSAGE
093                    );
094                    return;
095                }
096            }
097        }
098        layer.setName(nameText);
099        Main.parent.repaint();
100    }
101}