[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[626] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[2323] | 5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[626] | 6 |
|
---|
[3501] | 7 | import java.awt.Dialog.ModalityType;
|
---|
[626] | 8 | import java.awt.event.ActionEvent;
|
---|
| 9 | import java.io.File;
|
---|
| 10 |
|
---|
| 11 | import javax.swing.AbstractAction;
|
---|
| 12 | import javax.swing.Box;
|
---|
| 13 | import javax.swing.JCheckBox;
|
---|
| 14 | import javax.swing.JDialog;
|
---|
| 15 | import javax.swing.JOptionPane;
|
---|
| 16 |
|
---|
| 17 | import org.openstreetmap.josm.Main;
|
---|
| 18 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
| 19 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[5886] | 20 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
[626] | 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 | */
|
---|
| 28 | public class RenameLayerAction extends AbstractAction {
|
---|
| 29 |
|
---|
[1169] | 30 | private File file;
|
---|
| 31 | private Layer layer;
|
---|
[626] | 32 |
|
---|
[1169] | 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;
|
---|
[2512] | 41 | this.putValue("help", ht("/Action/RenameLayer"));
|
---|
[1169] | 42 | }
|
---|
[626] | 43 |
|
---|
[6084] | 44 | @Override
|
---|
[1169] | 45 | public void actionPerformed(ActionEvent e) {
|
---|
| 46 | Box panel = Box.createVerticalBox();
|
---|
[5886] | 47 | final JosmTextField name = new JosmTextField(layer.getName());
|
---|
[1169] | 48 | panel.add(name);
|
---|
| 49 | JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
|
---|
| 50 | if (Main.applet) {
|
---|
| 51 | filerename.setEnabled(false);
|
---|
| 52 | filerename.setSelected(false);
|
---|
| 53 | } else {
|
---|
| 54 | panel.add(filerename);
|
---|
| 55 | filerename.setEnabled(file != null);
|
---|
| 56 | }
|
---|
[1847] | 57 | if (filerename.isEnabled()) {
|
---|
[1169] | 58 | filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
|
---|
[1847] | 59 | }
|
---|
[626] | 60 |
|
---|
[1169] | 61 | final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
|
---|
| 62 | @Override public void selectInitialValue() {
|
---|
| 63 | name.requestFocusInWindow();
|
---|
| 64 | name.selectAll();
|
---|
| 65 | }
|
---|
| 66 | };
|
---|
| 67 | final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
|
---|
[3501] | 68 | dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
|
---|
[1169] | 69 | dlg.setVisible(true);
|
---|
[626] | 70 |
|
---|
[1169] | 71 | Object answer = optionPane.getValue();
|
---|
| 72 | if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
|
---|
[1847] | 73 | (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION))
|
---|
[1169] | 74 | return;
|
---|
[626] | 75 |
|
---|
[1169] | 76 | String nameText = name.getText();
|
---|
| 77 | if (filerename.isEnabled()) {
|
---|
| 78 | Main.pref.put("layer.rename-file", filerename.isSelected());
|
---|
| 79 | if (filerename.isSelected()) {
|
---|
| 80 | String newname = nameText;
|
---|
[6083] | 81 | if (newname.indexOf('/') == -1 && newname.indexOf('\\') == -1) {
|
---|
[1169] | 82 | newname = file.getParent() + File.separator + newname;
|
---|
[1847] | 83 | }
|
---|
[1169] | 84 | String oldname = file.getName();
|
---|
[1847] | 85 | if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
|
---|
[1169] | 86 | newname += oldname.substring(oldname.lastIndexOf('.'));
|
---|
[1847] | 87 | }
|
---|
[1169] | 88 | File newFile = new File(newname);
|
---|
[4570] | 89 | if (Main.platform.rename(file, newFile)) {
|
---|
[1646] | 90 | layer.setAssociatedFile(newFile);
|
---|
[1169] | 91 | nameText = newFile.getName();
|
---|
| 92 | } else {
|
---|
[2017] | 93 | JOptionPane.showMessageDialog(
|
---|
[1847] | 94 | Main.parent,
|
---|
| 95 | tr("Could not rename file ''{0}''", file.getPath()),
|
---|
| 96 | tr("Error"),
|
---|
| 97 | JOptionPane.ERROR_MESSAGE
|
---|
| 98 | );
|
---|
[1169] | 99 | return;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[1890] | 103 | layer.setName(nameText);
|
---|
[1169] | 104 | Main.parent.repaint();
|
---|
| 105 | }
|
---|
[626] | 106 | }
|
---|