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

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

see #8039, see #10456 - support read-only data layers

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