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

Last change on this file since 12167 was 11343, checked in by Don-vip, 7 years ago

findbugs - disable SE_TRANSIENT_FIELD_NOT_RESTORED, fix some SIC_INNER_SHOULD_BE_STATIC_ANON

  • Property svn:eol-style set to native
File size: 4.3 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 static class InitialValueOptionPane extends JOptionPane {
47 InitialValueOptionPane(Box panel, JosmTextField initial) {
48 super(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, initial);
49 }
50
51 @Override
52 public void selectInitialValue() {
53 JosmTextField initial = (JosmTextField) getInitialValue();
54 initial.requestFocusInWindow();
55 initial.selectAll();
56 }
57 }
58
59 @Override
60 public void actionPerformed(ActionEvent e) {
61 Box panel = Box.createVerticalBox();
62 final JosmTextField name = new JosmTextField(layer.getName());
63 panel.add(name);
64 JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
65 panel.add(filerename);
66 filerename.setEnabled(file != null);
67 if (filerename.isEnabled()) {
68 filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
69 }
70
71 final JOptionPane optionPane = new InitialValueOptionPane(panel, name);
72 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
73 dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
74 dlg.setVisible(true);
75
76 Object answer = optionPane.getValue();
77 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
78 (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))
79 return;
80
81 String nameText = name.getText();
82 if (filerename.isEnabled()) {
83 Main.pref.put("layer.rename-file", filerename.isSelected());
84 if (filerename.isSelected()) {
85 String newname = nameText;
86 if (newname.indexOf('/') == -1 && newname.indexOf('\\') == -1) {
87 newname = file.getParent() + File.separator + newname;
88 }
89 String oldname = file.getName();
90 if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
91 newname += oldname.substring(oldname.lastIndexOf('.'));
92 }
93 File newFile = new File(newname);
94 if (!SaveActionBase.confirmOverwrite(newFile))
95 return;
96 if (Main.platform.rename(file, newFile)) {
97 layer.setAssociatedFile(newFile);
98 if (!layer.isRenamed()) {
99 nameText = newFile.getName();
100 }
101 } else {
102 JOptionPane.showMessageDialog(
103 Main.parent,
104 tr("Could not rename file ''{0}''", file.getPath()),
105 tr("Error"),
106 JOptionPane.ERROR_MESSAGE
107 );
108 return;
109 }
110 }
111 }
112 layer.rename(nameText);
113 Main.parent.repaint();
114 }
115}
Note: See TracBrowser for help on using the repository browser.