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

Last change on this file since 10604 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
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
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;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.gui.layer.Layer;
19import org.openstreetmap.josm.gui.widgets.JosmTextField;
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 final File file;
31 private final transient Layer layer;
32
33 /**
34 * Constructs a new {@code RenameLayerAction}.
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.
37 * @param layer layer to rename
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;
43 this.putValue("help", ht("/Action/RenameLayer"));
44 }
45
46 @Override
47 public void actionPerformed(ActionEvent e) {
48 Box panel = Box.createVerticalBox();
49 final JosmTextField name = new JosmTextField(layer.getName());
50 panel.add(name);
51 JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
52 panel.add(filerename);
53 filerename.setEnabled(file != null);
54 if (filerename.isEnabled()) {
55 filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
56 }
57
58 final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
59 @Override public void selectInitialValue() {
60 name.requestFocusInWindow();
61 name.selectAll();
62 }
63 };
64 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
65 dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
66 dlg.setVisible(true);
67
68 Object answer = optionPane.getValue();
69 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
70 (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))
71 return;
72
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;
78 if (newname.indexOf('/') == -1 && newname.indexOf('\\') == -1) {
79 newname = file.getParent() + File.separator + newname;
80 }
81 String oldname = file.getName();
82 if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
83 newname += oldname.substring(oldname.lastIndexOf('.'));
84 }
85 File newFile = new File(newname);
86 if (!SaveActionBase.confirmOverwrite(newFile))
87 return;
88 if (Main.platform.rename(file, newFile)) {
89 layer.setAssociatedFile(newFile);
90 if (!layer.isRenamed()) {
91 nameText = newFile.getName();
92 }
93 } else {
94 JOptionPane.showMessageDialog(
95 Main.parent,
96 tr("Could not rename file ''{0}''", file.getPath()),
97 tr("Error"),
98 JOptionPane.ERROR_MESSAGE
99 );
100 return;
101 }
102 }
103 }
104 layer.rename(nameText);
105 Main.parent.repaint();
106 }
107}
Note: See TracBrowser for help on using the repository browser.