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

Last change on this file since 13790 was 13130, checked in by Don-vip, 6 years ago

fix #15572 - use ImageProvider attach API for all JOSM actions to ensure proper icon size everywhere

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