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

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

see #15229 - fix deprecations caused by [12840]

  • Property svn:eol-style set to native
File size: 4.3 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;
[626]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).
[1169]25 *
[626]26 * @author Imi
27 */
28public class RenameLayerAction extends AbstractAction {
29
[9067]30 private final File file;
31 private final transient Layer layer;
[626]32
[1169]33 /**
[9230]34 * Constructs a new {@code RenameLayerAction}.
[1169]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.
[9230]37 * @param layer layer to rename
[1169]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;
[2512]43 this.putValue("help", ht("/Action/RenameLayer"));
[1169]44 }
[626]45
[11343]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
[6084]59 @Override
[1169]60 public void actionPerformed(ActionEvent e) {
61 Box panel = Box.createVerticalBox();
[5886]62 final JosmTextField name = new JosmTextField(layer.getName());
[1169]63 panel.add(name);
64 JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
[7026]65 panel.add(filerename);
66 filerename.setEnabled(file != null);
[1847]67 if (filerename.isEnabled()) {
[1169]68 filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
[1847]69 }
[626]70
[11343]71 final JOptionPane optionPane = new InitialValueOptionPane(panel, name);
[1169]72 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
[3501]73 dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
[1169]74 dlg.setVisible(true);
[626]75
[1169]76 Object answer = optionPane.getValue();
77 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
[8510]78 (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))
[1169]79 return;
[626]80
[1169]81 String nameText = name.getText();
82 if (filerename.isEnabled()) {
[12841]83 Main.pref.putBoolean("layer.rename-file", filerename.isSelected());
[1169]84 if (filerename.isSelected()) {
85 String newname = nameText;
[6083]86 if (newname.indexOf('/') == -1 && newname.indexOf('\\') == -1) {
[1169]87 newname = file.getParent() + File.separator + newname;
[1847]88 }
[1169]89 String oldname = file.getName();
[1847]90 if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
[1169]91 newname += oldname.substring(oldname.lastIndexOf('.'));
[1847]92 }
[1169]93 File newFile = new File(newname);
[9591]94 if (!SaveActionBase.confirmOverwrite(newFile))
95 return;
[4570]96 if (Main.platform.rename(file, newFile)) {
[1646]97 layer.setAssociatedFile(newFile);
[9510]98 if (!layer.isRenamed()) {
99 nameText = newFile.getName();
100 }
[1169]101 } else {
[2017]102 JOptionPane.showMessageDialog(
[1847]103 Main.parent,
104 tr("Could not rename file ''{0}''", file.getPath()),
105 tr("Error"),
106 JOptionPane.ERROR_MESSAGE
107 );
[1169]108 return;
109 }
110 }
111 }
[9510]112 layer.rename(nameText);
[1169]113 Main.parent.repaint();
114 }
[626]115}
Note: See TracBrowser for help on using the repository browser.