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.data.coor.LatLon; 013import org.openstreetmap.josm.data.osm.NoteData; 014import org.openstreetmap.josm.gui.MainApplication; 015import org.openstreetmap.josm.gui.MapFrame; 016import org.openstreetmap.josm.gui.NoteInputDialog; 017import org.openstreetmap.josm.gui.Notification; 018import org.openstreetmap.josm.gui.util.KeyPressReleaseListener; 019import org.openstreetmap.josm.tools.CheckParameterUtil; 020import org.openstreetmap.josm.tools.ImageProvider; 021import org.openstreetmap.josm.tools.Logging; 022 023/** 024 * Map mode to add a new note. Listens for a mouse click and then 025 * prompts the user for text and adds a note to the note layer 026 */ 027public class AddNoteAction extends MapMode implements KeyPressReleaseListener { 028 029 private final transient NoteData noteData; 030 031 /** 032 * Construct a new map mode. 033 * @param data Note data container. Must not be null 034 * @since 11713 035 */ 036 public AddNoteAction(NoteData data) { 037 super(tr("Add a new Note"), "addnote", tr("Add note mode"), 038 ImageProvider.getCursor("crosshair", "create_note")); 039 CheckParameterUtil.ensureParameterNotNull(data, "data"); 040 noteData = 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 (input != null && !input.isEmpty()) { 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}