001    // License: GPL. Copyright 2007 by Immanuel Scholz and others
002    package org.openstreetmap.josm.actions;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
006    
007    import java.awt.Dialog.ModalityType;
008    import java.awt.event.ActionEvent;
009    import java.io.File;
010    
011    import javax.swing.AbstractAction;
012    import javax.swing.Box;
013    import javax.swing.JCheckBox;
014    import javax.swing.JDialog;
015    import javax.swing.JOptionPane;
016    import javax.swing.JTextField;
017    
018    import org.openstreetmap.josm.Main;
019    import org.openstreetmap.josm.gui.layer.Layer;
020    import 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     */
028    public 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        public void actionPerformed(ActionEvent e) {
045            Box panel = Box.createVerticalBox();
046            final JTextField name = new JTextField(layer.getName());
047            panel.add(name);
048            JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
049            if (Main.applet) {
050                filerename.setEnabled(false);
051                filerename.setSelected(false);
052            } else {
053                panel.add(filerename);
054                filerename.setEnabled(file != null);
055            }
056            if (filerename.isEnabled()) {
057                filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
058            }
059    
060            final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
061                @Override public void selectInitialValue() {
062                    name.requestFocusInWindow();
063                    name.selectAll();
064                }
065            };
066            final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
067            dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
068            dlg.setVisible(true);
069    
070            Object answer = optionPane.getValue();
071            if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
072                    (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION))
073                return;
074    
075            String nameText = name.getText();
076            if (filerename.isEnabled()) {
077                Main.pref.put("layer.rename-file", filerename.isSelected());
078                if (filerename.isSelected()) {
079                    String newname = nameText;
080                    if (newname.indexOf("/") == -1 && newname.indexOf("\\") == -1) {
081                        newname = file.getParent() + File.separator + newname;
082                    }
083                    String oldname = file.getName();
084                    if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
085                        newname += oldname.substring(oldname.lastIndexOf('.'));
086                    }
087                    File newFile = new File(newname);
088                    if (Main.platform.rename(file, newFile)) {
089                        layer.setAssociatedFile(newFile);
090                        nameText = newFile.getName();
091                    } else {
092                        JOptionPane.showMessageDialog(
093                                Main.parent,
094                                tr("Could not rename file ''{0}''", file.getPath()),
095                                tr("Error"),
096                                JOptionPane.ERROR_MESSAGE
097                        );
098                        return;
099                    }
100                }
101            }
102            layer.setName(nameText);
103            Main.parent.repaint();
104        }
105    }