source: josm/trunk/src/org/openstreetmap/josm/actions/RenameLayerAction.java @ 5241

Revision 4570, 3.8 KB checked in by Don-vip, 7 months ago (diff)

Use of Main.platform.rename() instead of File.renameTo()

  • Property svn:eol-style set to native
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
6
7import java.awt.Dialog.ModalityType;
8import java.awt.event.ActionEvent;
9import java.io.File;
10
11import javax.swing.AbstractAction;
12import javax.swing.Box;
13import javax.swing.JCheckBox;
14import javax.swing.JDialog;
15import javax.swing.JOptionPane;
16import javax.swing.JTextField;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.gui.layer.Layer;
20import org.openstreetmap.josm.tools.ImageProvider;
21
22/**
23 * Action to rename an specific layer. Provides the option to rename the
24 * file, this layer was loaded from as well (if it was loaded from a file).
25 *
26 * @author Imi
27 */
28public class RenameLayerAction extends AbstractAction {
29
30    private File file;
31    private Layer layer;
32
33    /**
34     * @param file The file of the original location of this layer.
35     *      If null, no possibility to "rename the file as well" is provided.
36     */
37    public RenameLayerAction(File file, Layer layer) {
38        super(tr("Rename layer"), ImageProvider.get("dialogs", "edit"));
39        this.file = file;
40        this.layer = layer;
41        this.putValue("help", ht("/Action/RenameLayer"));
42    }
43
44    public void actionPerformed(ActionEvent e) {
45        Box panel = Box.createVerticalBox();
46        final JTextField name = new JTextField(layer.getName());
47        panel.add(name);
48        JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
49        if (Main.applet) {
50            filerename.setEnabled(false);
51            filerename.setSelected(false);
52        } else {
53            panel.add(filerename);
54            filerename.setEnabled(file != null);
55        }
56        if (filerename.isEnabled()) {
57            filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
58        }
59
60        final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
61            @Override public void selectInitialValue() {
62                name.requestFocusInWindow();
63                name.selectAll();
64            }
65        };
66        final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
67        dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
68        dlg.setVisible(true);
69
70        Object answer = optionPane.getValue();
71        if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
72                (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION))
73            return;
74
75        String nameText = name.getText();
76        if (filerename.isEnabled()) {
77            Main.pref.put("layer.rename-file", filerename.isSelected());
78            if (filerename.isSelected()) {
79                String newname = nameText;
80                if (newname.indexOf("/") == -1 && newname.indexOf("\\") == -1) {
81                    newname = file.getParent() + File.separator + newname;
82                }
83                String oldname = file.getName();
84                if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
85                    newname += oldname.substring(oldname.lastIndexOf('.'));
86                }
87                File newFile = new File(newname);
88                if (Main.platform.rename(file, newFile)) {
89                    layer.setAssociatedFile(newFile);
90                    nameText = newFile.getName();
91                } else {
92                    JOptionPane.showMessageDialog(
93                            Main.parent,
94                            tr("Could not rename file ''{0}''", file.getPath()),
95                            tr("Error"),
96                            JOptionPane.ERROR_MESSAGE
97                    );
98                    return;
99                }
100            }
101        }
102        layer.setName(nameText);
103        Main.parent.repaint();
104    }
105}
Note: See TracBrowser for help on using the repository browser.