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

Last change on this file since 2474 was 2323, checked in by Gubaer, 14 years ago

Added explicit help topics
See also current list of help topics with links to source files and to help pages

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