source: josm/src/org/openstreetmap/josm/actions/RenameLayerAction.java@ 172

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