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

Last change on this file since 11329 was 10433, checked in by Don-vip, 8 years ago

checkstyle

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