| 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 |
|
|---|
| 6 | import java.awt.event.ActionEvent;
|
|---|
| 7 | import java.io.File;
|
|---|
| 8 |
|
|---|
| 9 | import javax.swing.AbstractAction;
|
|---|
| 10 | import javax.swing.Box;
|
|---|
| 11 | import javax.swing.JCheckBox;
|
|---|
| 12 | import javax.swing.JDialog;
|
|---|
| 13 | import javax.swing.JOptionPane;
|
|---|
| 14 | import javax.swing.JTextField;
|
|---|
| 15 |
|
|---|
| 16 | import org.openstreetmap.josm.Main;
|
|---|
| 17 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 18 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Action to rename an specific layer. Provides the option to rename the
|
|---|
| 22 | * file, this layer was loaded from as well (if it was loaded from a file).
|
|---|
| 23 | *
|
|---|
| 24 | * @author Imi
|
|---|
| 25 | */
|
|---|
| 26 | public class RenameLayerAction extends AbstractAction {
|
|---|
| 27 |
|
|---|
| 28 | private File file;
|
|---|
| 29 | private Layer layer;
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * @param file The filen of the original location of this layer.
|
|---|
| 33 | * If null, no possibility to "rename the file as well" is provided.
|
|---|
| 34 | */
|
|---|
| 35 | public RenameLayerAction(File file, Layer layer) {
|
|---|
| 36 | super(tr("Rename layer"), ImageProvider.get("dialogs", "edit"));
|
|---|
| 37 | this.file = file;
|
|---|
| 38 | this.layer = layer;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | public void actionPerformed(ActionEvent e) {
|
|---|
| 42 | Box panel = Box.createVerticalBox();
|
|---|
| 43 | final JTextField name = new JTextField(layer.name);
|
|---|
| 44 | panel.add(name);
|
|---|
| 45 | JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
|
|---|
| 46 | if (Main.applet) {
|
|---|
| 47 | filerename.setEnabled(false);
|
|---|
| 48 | filerename.setSelected(false);
|
|---|
| 49 | } else {
|
|---|
| 50 | panel.add(filerename);
|
|---|
| 51 | filerename.setEnabled(file != null);
|
|---|
| 52 | }
|
|---|
| 53 | if (filerename.isEnabled())
|
|---|
| 54 | filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
|
|---|
| 55 |
|
|---|
| 56 | final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
|
|---|
| 57 | @Override public void selectInitialValue() {
|
|---|
| 58 | name.requestFocusInWindow();
|
|---|
| 59 | name.selectAll();
|
|---|
| 60 | }
|
|---|
| 61 | };
|
|---|
| 62 | final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
|
|---|
| 63 | dlg.setVisible(true);
|
|---|
| 64 |
|
|---|
| 65 | Object answer = optionPane.getValue();
|
|---|
| 66 | if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
|
|---|
| 67 | (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION)) {
|
|---|
| 68 | return;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | String nameText = name.getText();
|
|---|
| 72 | if (filerename.isEnabled()) {
|
|---|
| 73 | Main.pref.put("layer.rename-file", filerename.isSelected());
|
|---|
| 74 | if (filerename.isSelected()) {
|
|---|
| 75 | String newname = nameText;
|
|---|
| 76 | if (newname.indexOf("/") == -1 && newname.indexOf("\\") == -1)
|
|---|
| 77 | newname = file.getParent() + File.separator + newname;
|
|---|
| 78 | String oldname = file.getName();
|
|---|
| 79 | if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0)
|
|---|
| 80 | newname += oldname.substring(oldname.lastIndexOf('.'));
|
|---|
| 81 | File newFile = new File(newname);
|
|---|
| 82 | if (file.renameTo(newFile)) {
|
|---|
| 83 | layer.associatedFile = newFile;
|
|---|
| 84 | nameText = newFile.getName();
|
|---|
| 85 | } else {
|
|---|
| 86 | JOptionPane.showMessageDialog(Main.parent, tr("Could not rename the file \"{0}\".", file.getPath()));
|
|---|
| 87 | return;
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | layer.name = nameText;
|
|---|
| 92 | Main.parent.repaint();
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|