1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.KeyEvent;
|
---|
7 | import java.awt.event.MouseEvent;
|
---|
8 |
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 | import javax.swing.SwingUtilities;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
14 | import org.openstreetmap.josm.data.osm.NoteData;
|
---|
15 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
16 | import org.openstreetmap.josm.gui.NoteInputDialog;
|
---|
17 | import org.openstreetmap.josm.gui.Notification;
|
---|
18 | import org.openstreetmap.josm.gui.dialogs.NotesDialog;
|
---|
19 | import org.openstreetmap.josm.gui.util.KeyPressReleaseListener;
|
---|
20 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Map mode to add a new note. Listens for a mouse click and then
|
---|
25 | * prompts the user for text and adds a note to the note layer
|
---|
26 | */
|
---|
27 | public class AddNoteAction extends MapMode implements KeyPressReleaseListener {
|
---|
28 |
|
---|
29 | private transient NoteData noteData;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Construct a new map mode.
|
---|
33 | * @param mapFrame Map frame to pass to the superconstructor
|
---|
34 | * @param data Note data container. Must not be null
|
---|
35 | */
|
---|
36 | public AddNoteAction(MapFrame mapFrame, NoteData data) {
|
---|
37 | super(tr("Add a new Note"), "addnote",
|
---|
38 | tr("Add note mode"),
|
---|
39 | mapFrame, 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 | Main.map.mapView.addMouseListener(this);
|
---|
53 | Main.map.keyDetector.addKeyListener(this);
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public void exitMode() {
|
---|
58 | super.exitMode();
|
---|
59 | Main.map.mapView.removeMouseListener(this);
|
---|
60 | Main.map.keyDetector.removeKeyListener(this);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public void mouseClicked(MouseEvent e) {
|
---|
65 | if (!SwingUtilities.isLeftMouseButton(e)) {
|
---|
66 | // allow to pan without distraction
|
---|
67 | return;
|
---|
68 | }
|
---|
69 | Main.map.selectMapMode(Main.map.mapModeSelect);
|
---|
70 | LatLon latlon = Main.map.mapView.getLatLon(e.getPoint().x, e.getPoint().y);
|
---|
71 |
|
---|
72 | NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Create new note"), tr("Create note"));
|
---|
73 | dialog.showNoteDialog(tr("Enter a detailed comment to create a note"), NotesDialog.ICON_NEW);
|
---|
74 |
|
---|
75 | if (dialog.getValue() != 1) {
|
---|
76 | Main.debug("User aborted note creation");
|
---|
77 | return;
|
---|
78 | }
|
---|
79 | String input = dialog.getInputText();
|
---|
80 | if (input != null && !input.isEmpty()) {
|
---|
81 | noteData.createNote(latlon, input);
|
---|
82 | } else {
|
---|
83 | Notification notification = new Notification(tr("You must enter a comment to create a new note"));
|
---|
84 | notification.setIcon(JOptionPane.WARNING_MESSAGE);
|
---|
85 | notification.show();
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public void doKeyPressed(KeyEvent e) {
|
---|
91 | if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
---|
92 | Main.map.selectMapMode(Main.map.mapModeSelect);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public void doKeyReleased(KeyEvent e) {
|
---|
98 | }
|
---|
99 | }
|
---|