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.MouseEvent;
007
008import javax.swing.JOptionPane;
009
010import org.openstreetmap.josm.Main;
011import org.openstreetmap.josm.data.coor.LatLon;
012import org.openstreetmap.josm.data.osm.NoteData;
013import org.openstreetmap.josm.gui.MapFrame;
014import org.openstreetmap.josm.gui.NoteInputDialog;
015import org.openstreetmap.josm.gui.Notification;
016import org.openstreetmap.josm.gui.dialogs.NotesDialog;
017import org.openstreetmap.josm.tools.CheckParameterUtil;
018import org.openstreetmap.josm.tools.ImageProvider;
019
020/**
021 * Map mode to add a new note. Listens for a mouse click and then
022 * prompts the user for text and adds a note to the note layer
023 */
024public class AddNoteAction extends MapMode {
025
026    private NoteData noteData;
027
028    /**
029     * Construct a new map mode.
030     * @param mapFrame Map frame to pass to the superconstructor
031     * @param data Note data container. Must not be null
032     */
033    public AddNoteAction(MapFrame mapFrame, NoteData data) {
034        super(tr("Add a new Note"), "addnote.png",
035            tr("Add note mode"),
036            mapFrame, ImageProvider.getCursor("crosshair", "create_note"));
037        CheckParameterUtil.ensureParameterNotNull(data, "data");
038        noteData = data;
039    }
040
041    @Override
042    public String getModeHelpText() {
043        return tr("Click the location where you wish to create a new note");
044    }
045
046    @Override
047    public void enterMode() {
048        super.enterMode();
049        Main.map.mapView.addMouseListener(this);
050    }
051
052    @Override
053    public void exitMode() {
054        super.exitMode();
055        Main.map.mapView.removeMouseListener(this);
056    }
057
058    @Override
059    public void mouseClicked(MouseEvent e) {
060        Main.map.selectMapMode(Main.map.mapModeSelect);
061        LatLon latlon = Main.map.mapView.getLatLon(e.getPoint().x, e.getPoint().y);
062
063        NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Create new note"), tr("Create note"));
064        dialog.showNoteDialog(tr("Enter a detailed comment to create a note"), NotesDialog.ICON_NEW);
065
066        if (dialog.getValue() != 1) {
067            Main.debug("User aborted note creation");
068            return;
069        }
070        String input = dialog.getInputText();
071        if (input != null && !input.isEmpty()) {
072            noteData.createNote(latlon, input);
073        } else {
074            Notification notification = new Notification(tr("You must enter a comment to create a new note"));
075            notification.setIcon(JOptionPane.WARNING_MESSAGE);
076            notification.show();
077        }
078    }
079}