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