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

Last change on this file since 13608 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
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[626]2package org.openstreetmap.josm.actions;
3
[8308]4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
[626]5import static org.openstreetmap.josm.tools.I18n.tr;
6
[3501]7import java.awt.Dialog.ModalityType;
[626]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;
[8308]19import org.openstreetmap.josm.gui.widgets.JosmTextField;
[12846]20import org.openstreetmap.josm.spi.preferences.Config;
[626]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).
[1169]26 *
[626]27 * @author Imi
28 */
29public class RenameLayerAction extends AbstractAction {
30
[9067]31 private final File file;
32 private final transient Layer layer;
[626]33
[1169]34 /**
[9230]35 * Constructs a new {@code RenameLayerAction}.
[1169]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.
[9230]38 * @param layer layer to rename
[1169]39 */
40 public RenameLayerAction(File file, Layer layer) {
[13130]41 super(tr("Rename layer"));
42 new ImageProvider("dialogs", "edit").getResource().attachImageIcon(this, true);
[1169]43 this.file = file;
44 this.layer = layer;
[2512]45 this.putValue("help", ht("/Action/RenameLayer"));
[1169]46 }
[626]47
[11343]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
[6084]61 @Override
[1169]62 public void actionPerformed(ActionEvent e) {
63 Box panel = Box.createVerticalBox();
[5886]64 final JosmTextField name = new JosmTextField(layer.getName());
[1169]65 panel.add(name);
66 JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
[7026]67 panel.add(filerename);
68 filerename.setEnabled(file != null);
[1847]69 if (filerename.isEnabled()) {
[12846]70 filerename.setSelected(Config.getPref().getBoolean("layer.rename-file", true));
[1847]71 }
[626]72
[11343]73 final JOptionPane optionPane = new InitialValueOptionPane(panel, name);
[1169]74 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
[3501]75 dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
[1169]76 dlg.setVisible(true);
[626]77
[1169]78 Object answer = optionPane.getValue();
79 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
[8510]80 (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))
[1169]81 return;
[626]82
[1169]83 String nameText = name.getText();
84 if (filerename.isEnabled()) {
[12846]85 Config.getPref().putBoolean("layer.rename-file", filerename.isSelected());
[1169]86 if (filerename.isSelected()) {
87 String newname = nameText;
[6083]88 if (newname.indexOf('/') == -1 && newname.indexOf('\\') == -1) {
[1169]89 newname = file.getParent() + File.separator + newname;
[1847]90 }
[1169]91 String oldname = file.getName();
[1847]92 if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
[1169]93 newname += oldname.substring(oldname.lastIndexOf('.'));
[1847]94 }
[1169]95 File newFile = new File(newname);
[9591]96 if (!SaveActionBase.confirmOverwrite(newFile))
97 return;
[4570]98 if (Main.platform.rename(file, newFile)) {
[1646]99 layer.setAssociatedFile(newFile);
[9510]100 if (!layer.isRenamed()) {
101 nameText = newFile.getName();
102 }
[1169]103 } else {
[2017]104 JOptionPane.showMessageDialog(
[1847]105 Main.parent,
106 tr("Could not rename file ''{0}''", file.getPath()),
107 tr("Error"),
108 JOptionPane.ERROR_MESSAGE
109 );
[1169]110 return;
111 }
112 }
113 }
[9510]114 layer.rename(nameText);
[1169]115 Main.parent.repaint();
116 }
[626]117}
Note: See TracBrowser for help on using the repository browser.