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