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