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

Last change on this file since 8464 was 8308, checked in by Don-vip, 9 years ago

fix potential NPEs and Sonar issues related to serialization

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