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.dialogs.NotesDialog; 019import org.openstreetmap.josm.gui.util.KeyPressReleaseListener; 020import org.openstreetmap.josm.tools.CheckParameterUtil; 021import org.openstreetmap.josm.tools.ImageProvider; 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 mapFrame Map frame to pass to the superconstructor 034 * @param data Note data container. Must not be null 035 */ 036 public AddNoteAction(MapFrame mapFrame, NoteData data) { 037 super(tr("Add a new Note"), "addnote", 038 tr("Add note mode"), 039 mapFrame, 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 Main.map.mapView.addMouseListener(this); 053 Main.map.keyDetector.addKeyListener(this); 054 } 055 056 @Override 057 public void exitMode() { 058 super.exitMode(); 059 Main.map.mapView.removeMouseListener(this); 060 Main.map.keyDetector.removeKeyListener(this); 061 } 062 063 @Override 064 public void mouseClicked(MouseEvent e) { 065 if (!SwingUtilities.isLeftMouseButton(e)) { 066 // allow to pan without distraction 067 return; 068 } 069 Main.map.selectMapMode(Main.map.mapModeSelect); 070 071 NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Create new note"), tr("Create note")); 072 dialog.showNoteDialog(tr("Enter a detailed comment to create a note"), NotesDialog.ICON_NEW); 073 074 if (dialog.getValue() != 1) { 075 Main.debug("User aborted note creation"); 076 return; 077 } 078 String input = dialog.getInputText(); 079 if (input != null && !input.isEmpty()) { 080 LatLon latlon = Main.map.mapView.getLatLon(e.getPoint().x, e.getPoint().y); 081 noteData.createNote(latlon, input); 082 } else { 083 Notification notification = new Notification(tr("You must enter a comment to create a new note")); 084 notification.setIcon(JOptionPane.WARNING_MESSAGE); 085 notification.show(); 086 } 087 } 088 089 @Override 090 public void doKeyPressed(KeyEvent e) { 091 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { 092 Main.map.selectMapMode(Main.map.mapModeSelect); 093 } 094 } 095 096 @Override 097 public void doKeyReleased(KeyEvent e) { 098 } 099}