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

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

see #15182 - deprecate Main.map and Main.isDisplayingMapView(). Replacements: gui.MainApplication.getMap() / gui.MainApplication.isDisplayingMapView()

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