source: josm/trunk/src/org/openstreetmap/josm/actions/mapmode/AddNoteAction.java@ 11713

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

add Ant target to run PMD (only few rules for now), fix violations

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.mapmode;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.KeyEvent;
7import java.awt.event.MouseEvent;
8
9import javax.swing.JOptionPane;
10import javax.swing.SwingUtilities;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.coor.LatLon;
14import org.openstreetmap.josm.data.osm.NoteData;
15import org.openstreetmap.josm.gui.NoteInputDialog;
16import org.openstreetmap.josm.gui.Notification;
17import org.openstreetmap.josm.gui.util.KeyPressReleaseListener;
18import org.openstreetmap.josm.tools.CheckParameterUtil;
19import org.openstreetmap.josm.tools.ImageProvider;
20
21/**
22 * Map mode to add a new note. Listens for a mouse click and then
23 * prompts the user for text and adds a note to the note layer
24 */
25public class AddNoteAction extends MapMode implements KeyPressReleaseListener {
26
27 private final transient NoteData noteData;
28
29 /**
30 * Construct a new map mode.
31 * @param data Note data container. Must not be null
32 * @since 11713
33 */
34 public AddNoteAction(NoteData data) {
35 super(tr("Add a new Note"), "addnote", tr("Add note mode"),
36 ImageProvider.getCursor("crosshair", "create_note"));
37 CheckParameterUtil.ensureParameterNotNull(data, "data");
38 noteData = data;
39 }
40
41 @Override
42 public String getModeHelpText() {
43 return tr("Click the location where you wish to create a new note");
44 }
45
46 @Override
47 public void enterMode() {
48 super.enterMode();
49 Main.map.mapView.addMouseListener(this);
50 Main.map.keyDetector.addKeyListener(this);
51 }
52
53 @Override
54 public void exitMode() {
55 super.exitMode();
56 Main.map.mapView.removeMouseListener(this);
57 Main.map.keyDetector.removeKeyListener(this);
58 }
59
60 @Override
61 public void mouseClicked(MouseEvent e) {
62 if (!SwingUtilities.isLeftMouseButton(e)) {
63 // allow to pan without distraction
64 return;
65 }
66 Main.map.selectMapMode(Main.map.mapModeSelect);
67
68 NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Create new note"), tr("Create note"));
69 dialog.showNoteDialog(tr("Enter a detailed comment to create a note"), ImageProvider.get("dialogs/notes", "note_new"));
70
71 if (dialog.getValue() != 1) {
72 Main.debug("User aborted note creation");
73 return;
74 }
75 String input = dialog.getInputText();
76 if (input != null && !input.isEmpty()) {
77 LatLon latlon = Main.map.mapView.getLatLon(e.getPoint().x, e.getPoint().y);
78 noteData.createNote(latlon, input);
79 } else {
80 new Notification(tr("You must enter a comment to create a new note")).setIcon(JOptionPane.WARNING_MESSAGE).show();
81 }
82 }
83
84 @Override
85 public void doKeyPressed(KeyEvent e) {
86 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
87 Main.map.selectMapMode(Main.map.mapModeSelect);
88 }
89 }
90
91 @Override
92 public void doKeyReleased(KeyEvent e) {
93 // Do nothing
94 }
95}
Note: See TracBrowser for help on using the repository browser.