001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions.mapmode;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.KeyEvent;
007import java.awt.event.MouseEvent;
008import java.util.Objects;
009
010import javax.swing.JOptionPane;
011import javax.swing.SwingUtilities;
012
013import org.openstreetmap.josm.data.coor.LatLon;
014import org.openstreetmap.josm.data.osm.NoteData;
015import org.openstreetmap.josm.gui.MainApplication;
016import org.openstreetmap.josm.gui.MapFrame;
017import org.openstreetmap.josm.gui.NoteInputDialog;
018import org.openstreetmap.josm.gui.Notification;
019import org.openstreetmap.josm.gui.util.KeyPressReleaseListener;
020import org.openstreetmap.josm.tools.ImageProvider;
021import org.openstreetmap.josm.tools.Logging;
022import org.openstreetmap.josm.tools.Utils;
023
024/**
025 * Map mode to add a new note. Listens for a mouse click and then
026 * prompts the user for text and adds a note to the note layer
027 */
028public class AddNoteAction extends MapMode implements KeyPressReleaseListener {
029
030    private final transient NoteData noteData;
031
032    /**
033     * Construct a new map mode.
034     * @param data Note data container. Must not be null
035     * @since 11713
036     */
037    public AddNoteAction(NoteData data) {
038        super(tr("Add a new Note"), "addnote", tr("Add note mode"),
039            ImageProvider.getCursor("crosshair", "create_note"));
040        noteData = Objects.requireNonNull(data, "data");
041    }
042
043    @Override
044    public String getModeHelpText() {
045        return tr("Click the location where you wish to create a new note");
046    }
047
048    @Override
049    public void enterMode() {
050        super.enterMode();
051        MapFrame map = MainApplication.getMap();
052        map.mapView.addMouseListener(this);
053        map.keyDetector.addKeyListener(this);
054    }
055
056    @Override
057    public void exitMode() {
058        super.exitMode();
059        MapFrame map = MainApplication.getMap();
060        map.mapView.removeMouseListener(this);
061        map.keyDetector.removeKeyListener(this);
062    }
063
064    @Override
065    public void mouseClicked(MouseEvent e) {
066        if (!SwingUtilities.isLeftMouseButton(e)) {
067            // allow to pan without distraction
068            return;
069        }
070        MapFrame map = MainApplication.getMap();
071        map.selectMapMode(map.mapModeSelect);
072
073        NoteInputDialog dialog = new NoteInputDialog(MainApplication.getMainFrame(), tr("Create a new note"), tr("Create note"));
074        dialog.showNoteDialog(tr("Enter a detailed comment to create a note"), ImageProvider.get("dialogs/notes", "note_new"));
075
076        if (dialog.getValue() != 1) {
077            Logging.debug("User aborted note creation");
078            return;
079        }
080        String input = dialog.getInputText();
081        if (!Utils.isEmpty(input)) {
082            LatLon latlon = map.mapView.getLatLon(e.getPoint().x, e.getPoint().y);
083            noteData.createNote(latlon, input);
084        } else {
085            new Notification(tr("You must enter a comment to create a new note")).setIcon(JOptionPane.WARNING_MESSAGE).show();
086        }
087    }
088
089    @Override
090    public void doKeyPressed(KeyEvent e) {
091        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
092            MapFrame map = MainApplication.getMap();
093            map.selectMapMode(map.mapModeSelect);
094        }
095    }
096
097    @Override
098    public void doKeyReleased(KeyEvent e) {
099        // 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}