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

Last change on this file since 6380 was 6380, checked in by Don-vip, 11 years ago

update license/copyright information

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
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.Dialog.ModalityType;
8import java.awt.event.ActionEvent;
9import java.io.File;
10
11import javax.swing.AbstractAction;
12import javax.swing.Box;
13import javax.swing.JCheckBox;
14import javax.swing.JDialog;
15import javax.swing.JOptionPane;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.gui.layer.Layer;
19import org.openstreetmap.josm.tools.ImageProvider;
20import org.openstreetmap.josm.gui.widgets.JosmTextField;
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 */
28public 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 @Override
45 public void actionPerformed(ActionEvent e) {
46 Box panel = Box.createVerticalBox();
47 final JosmTextField name = new JosmTextField(layer.getName());
48 panel.add(name);
49 JCheckBox filerename = new JCheckBox(tr("Also rename the file"));
50 if (Main.applet) {
51 filerename.setEnabled(false);
52 filerename.setSelected(false);
53 } else {
54 panel.add(filerename);
55 filerename.setEnabled(file != null);
56 }
57 if (filerename.isEnabled()) {
58 filerename.setSelected(Main.pref.getBoolean("layer.rename-file", true));
59 }
60
61 final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
62 @Override public void selectInitialValue() {
63 name.requestFocusInWindow();
64 name.selectAll();
65 }
66 };
67 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Rename layer"));
68 dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
69 dlg.setVisible(true);
70
71 Object answer = optionPane.getValue();
72 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
73 (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION))
74 return;
75
76 String nameText = name.getText();
77 if (filerename.isEnabled()) {
78 Main.pref.put("layer.rename-file", filerename.isSelected());
79 if (filerename.isSelected()) {
80 String newname = nameText;
81 if (newname.indexOf('/') == -1 && newname.indexOf('\\') == -1) {
82 newname = file.getParent() + File.separator + newname;
83 }
84 String oldname = file.getName();
85 if (name.getText().indexOf('.') == -1 && oldname.indexOf('.') >= 0) {
86 newname += oldname.substring(oldname.lastIndexOf('.'));
87 }
88 File newFile = new File(newname);
89 if (Main.platform.rename(file, newFile)) {
90 layer.setAssociatedFile(newFile);
91 nameText = newFile.getName();
92 } else {
93 JOptionPane.showMessageDialog(
94 Main.parent,
95 tr("Could not rename file ''{0}''", file.getPath()),
96 tr("Error"),
97 JOptionPane.ERROR_MESSAGE
98 );
99 return;
100 }
101 }
102 }
103 layer.setName(nameText);
104 Main.parent.repaint();
105 }
106}
Note: See TracBrowser for help on using the repository browser.