| 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 | import java.util.Objects;
|
|---|
| 9 |
|
|---|
| 10 | import javax.swing.JOptionPane;
|
|---|
| 11 | import javax.swing.SwingUtilities;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.NoteData;
|
|---|
| 15 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 16 | import org.openstreetmap.josm.gui.MapFrame;
|
|---|
| 17 | import org.openstreetmap.josm.gui.NoteInputDialog;
|
|---|
| 18 | import org.openstreetmap.josm.gui.Notification;
|
|---|
| 19 | import org.openstreetmap.josm.gui.util.KeyPressReleaseListener;
|
|---|
| 20 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 21 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 22 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 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 | */
|
|---|
| 28 | public 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 | noteData = Objects.requireNonNull(data, "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 | MapFrame map = MainApplication.getMap();
|
|---|
| 52 | map.mapView.addMouseListener(this);
|
|---|
| 53 | map.keyDetector.addKeyListener(this);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | @Override
|
|---|
| 57 | public void exitMode() {
|
|---|
| 58 | super.exitMode();
|
|---|
| 59 | MapFrame map = MainApplication.getMap();
|
|---|
| 60 | map.mapView.removeMouseListener(this);
|
|---|
| 61 | map.keyDetector.removeKeyListener(this);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | @Override
|
|---|
| 65 | public void mouseClicked(MouseEvent e) {
|
|---|
| 66 | if (!SwingUtilities.isLeftMouseButton(e)) {
|
|---|
| 67 | // allow to pan without distraction
|
|---|
| 68 | return;
|
|---|
| 69 | }
|
|---|
| 70 | MapFrame map = MainApplication.getMap();
|
|---|
| 71 | map.selectMapMode(map.mapModeSelect);
|
|---|
| 72 |
|
|---|
| 73 | NoteInputDialog dialog = new NoteInputDialog(MainApplication.getMainFrame(), tr("Create a new note"), tr("Create note"));
|
|---|
| 74 | dialog.showNoteDialog(tr("Enter a detailed comment to create a note"), ImageProvider.get("dialogs/notes", "note_new"));
|
|---|
| 75 |
|
|---|
| 76 | if (dialog.getValue() != 1) {
|
|---|
| 77 | Logging.debug("User aborted note creation");
|
|---|
| 78 | return;
|
|---|
| 79 | }
|
|---|
| 80 | String input = dialog.getInputText();
|
|---|
| 81 | if (!Utils.isEmpty(input)) {
|
|---|
| 82 | LatLon latlon = map.mapView.getLatLon(e.getPoint().x, e.getPoint().y);
|
|---|
| 83 | noteData.createNote(latlon, input);
|
|---|
| 84 | } else {
|
|---|
| 85 | new Notification(tr("You must enter a comment to create a new note")).setIcon(JOptionPane.WARNING_MESSAGE).show();
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | @Override
|
|---|
| 90 | public void doKeyPressed(KeyEvent e) {
|
|---|
| 91 | if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
|---|
| 92 | MapFrame map = MainApplication.getMap();
|
|---|
| 93 | map.selectMapMode(map.mapModeSelect);
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | @Override
|
|---|
| 98 | public void doKeyReleased(KeyEvent e) {
|
|---|
| 99 | // Do nothing
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | @Override
|
|---|
| 103 | protected boolean listenToLayerChange() {
|
|---|
| 104 | return false;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | @Override
|
|---|
| 108 | protected boolean listenToSelectionChange() {
|
|---|
| 109 | return false;
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|