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 file 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 | this.putValue("help", "Action/LayerRename");
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void actionPerformed(ActionEvent e) {
|
---|
43 | Box panel = Box.createVerticalBox();
|
---|
44 | final JTextField name = new JTextField(layer.name);
|
---|
45 | panel.add(name);
|
---|
46 | JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
|
---|
47 | if (Main.applet) {
|
---|
48 | filerename.setEnabled(false);
|
---|
49 | filerename.setSelected(false);
|
---|
50 | } else {
|
---|
51 | panel.add(filerename);
|
---|
52 | filerename.setEnabled(file != null);
|
---|
53 | }
|
---|
54 | if (filerename.isEnabled())
|
---|
55 | filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
|
---|
56 |
|
---|
57 | final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
|
---|
58 | @Override public void selectInitialValue() {
|
---|
59 | name.requestFocusInWindow();
|
---|
60 | name.selectAll();
|
---|
61 | }
|
---|
62 | };
|
---|
63 | final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
|
---|
64 | dlg.setVisible(true);
|
---|
65 |
|
---|
66 | Object answer = optionPane.getValue();
|
---|
67 | if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
|
---|
68 | (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION)) {
|
---|
69 | return;
|
---|
70 | }
|
---|
71 |
|
---|
72 | String nameText = name.getText();
|
---|
73 | if (filerename.isEnabled()) {
|
---|
74 | Main.pref.put("layer.rename-file", filerename.isSelected());
|
---|
75 | if (filerename.isSelected()) {
|
---|
76 | String newname = nameText;
|
---|
77 | if (newname.indexOf("/") == -1 && newname.indexOf("\\") == -1)
|
---|
78 | newname = file.getParent() + File.separator + newname;
|
---|
79 | String oldname = file.getName();
|
---|
80 | if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0)
|
---|
81 | newname += oldname.substring(oldname.lastIndexOf('.'));
|
---|
82 | File newFile = new File(newname);
|
---|
83 | if (file.renameTo(newFile)) {
|
---|
84 | layer.associatedFile = newFile;
|
---|
85 | nameText = newFile.getName();
|
---|
86 | } else {
|
---|
87 | JOptionPane.showMessageDialog(Main.parent, tr("Could not rename the file \"{0}\".", file.getPath()));
|
---|
88 | return;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | layer.name = nameText;
|
---|
93 | Main.parent.repaint();
|
---|
94 | }
|
---|
95 | }
|
---|