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.JLabel;
009import javax.swing.JOptionPane;
010import javax.swing.JScrollPane;
011import javax.swing.JTextArea;
012
013import org.openstreetmap.josm.Main;
014import org.openstreetmap.josm.data.coor.LatLon;
015import org.openstreetmap.josm.data.osm.NoteData;
016import org.openstreetmap.josm.gui.MapFrame;
017import org.openstreetmap.josm.gui.Notification;
018import org.openstreetmap.josm.gui.dialogs.NoteDialog;
019import org.openstreetmap.josm.tools.ImageProvider;
020
021/**
022 * Map mode to add a new note. Listens for a mouse click and then
023 * prompts the user for text and adds a note to the note layer
024 */
025public class AddNoteAction extends MapMode {
026
027    private NoteData noteData;
028
029    /**
030     * Construct a new map mode.
031     * @param mapFrame Map frame to pass to the superconstructor
032     * @param data Note data container. Must not be null
033     */
034    public AddNoteAction(MapFrame mapFrame, NoteData data) {
035        super(tr("Add a new Note"), "addnote.png",
036            tr("Add note mode"),
037            mapFrame, ImageProvider.getCursor("crosshair", "create_note"));
038        if (data == null) {
039            throw new IllegalArgumentException("Note data must not be null");
040        }
041        noteData = data;
042    }
043
044    @Override
045    public String getModeHelpText() {
046        return tr("Click the location where you wish to create a new note");
047    }
048
049    @Override
050    public void enterMode() {
051        super.enterMode();
052        Main.map.mapView.addMouseListener(this);
053    }
054
055    @Override
056    public void exitMode() {
057        super.exitMode();
058        Main.map.mapView.removeMouseListener(this);
059    }
060
061    @Override
062    public void mouseClicked(MouseEvent e) {
063        Main.map.selectMapMode(Main.map.mapModeSelect);
064        LatLon latlon = Main.map.mapView.getLatLon(e.getPoint().x, e.getPoint().y);
065        JLabel label = new JLabel(tr("Enter a comment for a new note"));
066        JTextArea textArea = new JTextArea();
067        textArea.setRows(6);
068        textArea.setColumns(30);
069        textArea.setLineWrap(true);
070        JScrollPane scrollPane = new JScrollPane(textArea);
071
072        Object[] components = new Object[]{label, scrollPane};
073        int option = JOptionPane.showConfirmDialog(Main.map,
074                components,
075                tr("Create new note"),
076                JOptionPane.OK_CANCEL_OPTION,
077                JOptionPane.PLAIN_MESSAGE,
078                NoteDialog.ICON_NEW);
079        if (option == JOptionPane.OK_OPTION) {
080            String input = textArea.getText();
081            if (input != null && !input.isEmpty()) {
082                noteData.createNote(latlon, input);
083            } else {
084                Notification notification = new Notification("You must enter a comment to create a new note");
085                notification.setIcon(JOptionPane.WARNING_MESSAGE);
086                notification.show();
087            }
088        }
089    }
090}