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

Last change on this file since 11117 was 9591, checked in by Don-vip, 8 years ago

fix #12390 - rename layer does not check existing file (patch by kolesar)

  • Property svn:eol-style set to native
File size: 4.0 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[626]2package org.openstreetmap.josm.actions;
3
[8308]4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
[626]5import static org.openstreetmap.josm.tools.I18n.tr;
6
[3501]7import java.awt.Dialog.ModalityType;
[626]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;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.gui.layer.Layer;
[8308]19import org.openstreetmap.josm.gui.widgets.JosmTextField;
[626]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).
[1169]25 *
[626]26 * @author Imi
27 */
28public class RenameLayerAction extends AbstractAction {
29
[9067]30 private final File file;
31 private final transient Layer layer;
[626]32
[1169]33 /**
[9230]34 * Constructs a new {@code RenameLayerAction}.
[1169]35 * @param file The file of the original location of this layer.
36 * If null, no possibility to "rename the file as well" is provided.
[9230]37 * @param layer layer to rename
[1169]38 */
39 public RenameLayerAction(File file, Layer layer) {
40 super(tr("Rename layer"), ImageProvider.get("dialogs", "edit"));
41 this.file = file;
42 this.layer = layer;
[2512]43 this.putValue("help", ht("/Action/RenameLayer"));
[1169]44 }
[626]45
[6084]46 @Override
[1169]47 public void actionPerformed(ActionEvent e) {
48 Box panel = Box.createVerticalBox();
[5886]49 final JosmTextField name = new JosmTextField(layer.getName());
[1169]50 panel.add(name);
51 JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
[7026]52 panel.add(filerename);
53 filerename.setEnabled(file != null);
[1847]54 if (filerename.isEnabled()) {
[1169]55 filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
[1847]56 }
[626]57
[8510]58 final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
[1169]59 @Override public void selectInitialValue() {
60 name.requestFocusInWindow();
61 name.selectAll();
62 }
63 };
64 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
[3501]65 dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
[1169]66 dlg.setVisible(true);
[626]67
[1169]68 Object answer = optionPane.getValue();
69 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
[8510]70 (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))
[1169]71 return;
[626]72
[1169]73 String nameText = name.getText();
74 if (filerename.isEnabled()) {
75 Main.pref.put("layer.rename-file", filerename.isSelected());
76 if (filerename.isSelected()) {
77 String newname = nameText;
[6083]78 if (newname.indexOf('/') == -1 && newname.indexOf('\\') == -1) {
[1169]79 newname = file.getParent() + File.separator + newname;
[1847]80 }
[1169]81 String oldname = file.getName();
[1847]82 if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
[1169]83 newname += oldname.substring(oldname.lastIndexOf('.'));
[1847]84 }
[1169]85 File newFile = new File(newname);
[9591]86 if (!SaveActionBase.confirmOverwrite(newFile))
87 return;
[4570]88 if (Main.platform.rename(file, newFile)) {
[1646]89 layer.setAssociatedFile(newFile);
[9510]90 if (!layer.isRenamed()) {
91 nameText = newFile.getName();
92 }
[1169]93 } else {
[2017]94 JOptionPane.showMessageDialog(
[1847]95 Main.parent,
96 tr("Could not rename file ''{0}''", file.getPath()),
97 tr("Error"),
98 JOptionPane.ERROR_MESSAGE
99 );
[1169]100 return;
101 }
102 }
103 }
[9510]104 layer.rename(nameText);
[1169]105 Main.parent.repaint();
106 }
[626]107}
Note: See TracBrowser for help on using the repository browser.