001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
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.tools.ImageProvider;
020import org.openstreetmap.josm.gui.widgets.JosmTextField;
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 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        if (Main.applet) {
051            filerename.setEnabled(false);
052            filerename.setSelected(false);
053        } else {
054            panel.add(filerename);
055            filerename.setEnabled(file != null);
056        }
057        if (filerename.isEnabled()) {
058            filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
059        }
060
061        final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
062            @Override public void selectInitialValue() {
063                name.requestFocusInWindow();
064                name.selectAll();
065            }
066        };
067        final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
068        dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
069        dlg.setVisible(true);
070
071        Object answer = optionPane.getValue();
072        if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
073                (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION))
074            return;
075
076        String nameText = name.getText();
077        if (filerename.isEnabled()) {
078            Main.pref.put("layer.rename-file", filerename.isSelected());
079            if (filerename.isSelected()) {
080                String newname = nameText;
081                if (newname.indexOf('/') == -1 && newname.indexOf('\\') == -1) {
082                    newname = file.getParent() + File.separator + newname;
083                }
084                String oldname = file.getName();
085                if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
086                    newname += oldname.substring(oldname.lastIndexOf('.'));
087                }
088                File newFile = new File(newname);
089                if (Main.platform.rename(file, newFile)) {
090                    layer.setAssociatedFile(newFile);
091                    nameText = newFile.getName();
092                } else {
093                    JOptionPane.showMessageDialog(
094                            Main.parent,
095                            tr("Could not rename file ''{0}''", file.getPath()),
096                            tr("Error"),
097                            JOptionPane.ERROR_MESSAGE
098                    );
099                    return;
100                }
101            }
102        }
103        layer.setName(nameText);
104        Main.parent.repaint();
105    }
106}