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.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; 021 022/** 023 * Map mode to add a new note. Listens for a mouse click and then 024 * prompts the user for text and adds a note to the note layer 025 */ 026public class AddNoteAction extends MapMode implements KeyPressReleaseListener { 027 028 private final transient NoteData noteData; 029 030 /** 031 * Construct a new map mode. 032 * @param mapFrame Map frame to pass to the superconstructor 033 * @param data Note data container. Must not be null 034 */ 035 public AddNoteAction(MapFrame mapFrame, NoteData data) { 036 super(tr("Add a new Note"), "addnote", 037 tr("Add note mode"), 038 mapFrame, 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 Main.map.mapView.addMouseListener(this); 052 Main.map.keyDetector.addKeyListener(this); 053 } 054 055 @Override 056 public void exitMode() { 057 super.exitMode(); 058 Main.map.mapView.removeMouseListener(this); 059 Main.map.keyDetector.removeKeyListener(this); 060 } 061 062 @Override 063 public void mouseClicked(MouseEvent e) { 064 if (!SwingUtilities.isLeftMouseButton(e)) { 065 // allow to pan without distraction 066 return; 067 } 068 Main.map.selectMapMode(Main.map.mapModeSelect); 069 070 NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Create new note"), tr("Create note")); 071 dialog.showNoteDialog(tr("Enter a detailed comment to create a note"), ImageProvider.get("dialogs/notes", "note_new")); 072 073 if (dialog.getValue() != 1) { 074 Main.debug("User aborted note creation"); 075 return; 076 } 077 String input = dialog.getInputText(); 078 if (input != null && !input.isEmpty()) { 079 LatLon latlon = Main.map.mapView.getLatLon(e.getPoint().x, e.getPoint().y); 080 noteData.createNote(latlon, input); 081 } else { 082 new Notification(tr("You must enter a comment to create a new note")).setIcon(JOptionPane.WARNING_MESSAGE).show(); 083 } 084 } 085 086 @Override 087 public void doKeyPressed(KeyEvent e) { 088 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { 089 Main.map.selectMapMode(Main.map.mapModeSelect); 090 } 091 } 092 093 @Override 094 public void doKeyReleased(KeyEvent e) { 095 // Do nothing 096 } 097}