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

Last change on this file since 12855 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

  • 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.spi.preferences.Config;
21import org.openstreetmap.josm.tools.ImageProvider;
22
23/**
24 * Action to rename an specific layer. Provides the option to rename the
25 * file, this layer was loaded from as well (if it was loaded from a file).
26 *
27 * @author Imi
28 */
29public class RenameLayerAction extends AbstractAction {
30
31 private final File file;
32 private final transient Layer layer;
33
34 /**
35 * Constructs a new {@code RenameLayerAction}.
36 * @param file The file of the original location of this layer.
37 * If null, no possibility to "rename the file as well" is provided.
38 * @param layer layer to rename
39 */
40 public RenameLayerAction(File file, Layer layer) {
41 super(tr("Rename layer"), ImageProvider.get("dialogs", "edit"));
42 this.file = file;
43 this.layer = layer;
44 this.putValue("help", ht("/Action/RenameLayer"));
45 }
46
47 static class InitialValueOptionPane extends JOptionPane {
48 InitialValueOptionPane(Box panel, JosmTextField initial) {
49 super(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, initial);
50 }
51
52 @Override
53 public void selectInitialValue() {
54 JosmTextField initial = (JosmTextField) getInitialValue();
55 initial.requestFocusInWindow();
56 initial.selectAll();
57 }
58 }
59
60 @Override
61 public void actionPerformed(ActionEvent e) {
62 Box panel = Box.createVerticalBox();
63 final JosmTextField name = new JosmTextField(layer.getName());
64 panel.add(name);
65 JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
66 panel.add(filerename);
67 filerename.setEnabled(file != null);
68 if (filerename.isEnabled()) {
69 filerename.setSelected(Config.getPref().getBoolean("layer.rename-file", true));
70 }
71
72 final JOptionPane optionPane = new InitialValueOptionPane(panel, name);
73 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
74 dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
75 dlg.setVisible(true);
76
77 Object answer = optionPane.getValue();
78 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
79 (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))
80 return;
81
82 String nameText = name.getText();
83 if (filerename.isEnabled()) {
84 Config.getPref().putBoolean("layer.rename-file", filerename.isSelected());
85 if (filerename.isSelected()) {
86 String newname = nameText;
87 if (newname.indexOf('/') == -1 && newname.indexOf('\\') == -1) {
88 newname = file.getParent() + File.separator + newname;
89 }
90 String oldname = file.getName();
91 if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
92 newname += oldname.substring(oldname.lastIndexOf('.'));
93 }
94 File newFile = new File(newname);
95 if (!SaveActionBase.confirmOverwrite(newFile))
96 return;
97 if (Main.platform.rename(file, newFile)) {
98 layer.setAssociatedFile(newFile);
99 if (!layer.isRenamed()) {
100 nameText = newFile.getName();
101 }
102 } else {
103 JOptionPane.showMessageDialog(
104 Main.parent,
105 tr("Could not rename file ''{0}''", file.getPath()),
106 tr("Error"),
107 JOptionPane.ERROR_MESSAGE
108 );
109 return;
110 }
111 }
112 }
113 layer.rename(nameText);
114 Main.parent.repaint();
115 }
116}
Note: See TracBrowser for help on using the repository browser.