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; 008 009import javax.swing.JOptionPane; 010import javax.swing.SwingUtilities; 011 012import org.openstreetmap.josm.Main; 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.CheckParameterUtil; 021import org.openstreetmap.josm.tools.ImageProvider; 022import org.openstreetmap.josm.tools.Logging; 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 CheckParameterUtil.ensureParameterNotNull(data, "data"); 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 MapFrame map = MainApplication.getMap(); 053 map.mapView.addMouseListener(this); 054 map.keyDetector.addKeyListener(this); 055 } 056 057 @Override 058 public void exitMode() { 059 super.exitMode(); 060 MapFrame map = MainApplication.getMap(); 061 map.mapView.removeMouseListener(this); 062 map.keyDetector.removeKeyListener(this); 063 } 064 065 @Override 066 public void mouseClicked(MouseEvent e) { 067 if (!SwingUtilities.isLeftMouseButton(e)) { 068 // allow to pan without distraction 069 return; 070 } 071 MapFrame map = MainApplication.getMap(); 072 map.selectMapMode(map.mapModeSelect); 073 074 NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Create a new note"), tr("Create note")); 075 dialog.showNoteDialog(tr("Enter a detailed comment to create a note"), ImageProvider.get("dialogs/notes", "note_new")); 076 077 if (dialog.getValue() != 1) { 078 Logging.debug("User aborted note creation"); 079 return; 080 } 081 String input = dialog.getInputText(); 082 if (input != null && !input.isEmpty()) { 083 LatLon latlon = map.mapView.getLatLon(e.getPoint().x, e.getPoint().y); 084 noteData.createNote(latlon, input); 085 } else { 086 new Notification(tr("You must enter a comment to create a new note")).setIcon(JOptionPane.WARNING_MESSAGE).show(); 087 } 088 } 089 090 @Override 091 public void doKeyPressed(KeyEvent e) { 092 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { 093 MapFrame map = MainApplication.getMap(); 094 map.selectMapMode(map.mapModeSelect); 095 } 096 } 097 098 @Override 099 public void doKeyReleased(KeyEvent e) { 100 // Do nothing 101 } 102}