source: josm/trunk/src/org/openstreetmap/josm/actions/mapmode/AddNoteAction.java@ 9067

Last change on this file since 9067 was 9067, checked in by Don-vip, 8 years ago

sonar - Immutable Field

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.mapmode;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.KeyEvent;
7import java.awt.event.MouseEvent;
8
9import javax.swing.JOptionPane;
10import javax.swing.SwingUtilities;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.coor.LatLon;
14import org.openstreetmap.josm.data.osm.NoteData;
15import org.openstreetmap.josm.gui.MapFrame;
16import org.openstreetmap.josm.gui.NoteInputDialog;
17import org.openstreetmap.josm.gui.Notification;
18import org.openstreetmap.josm.gui.dialogs.NotesDialog;
19import org.openstreetmap.josm.gui.util.KeyPressReleaseListener;
20import org.openstreetmap.josm.tools.CheckParameterUtil;
21import org.openstreetmap.josm.tools.ImageProvider;
22
23/**
24 * Map mode to add a new note. Listens for a mouse click and then
25 * prompts the user for text and adds a note to the note layer
26 */
27public class AddNoteAction extends MapMode implements KeyPressReleaseListener {
28
29 private final transient NoteData noteData;
30
31 /**
32 * Construct a new map mode.
33 * @param mapFrame Map frame to pass to the superconstructor
34 * @param data Note data container. Must not be null
35 */
36 public AddNoteAction(MapFrame mapFrame, NoteData data) {
37 super(tr("Add a new Note"), "addnote",
38 tr("Add note mode"),
39 mapFrame, ImageProvider.getCursor("crosshair", "create_note"));
40 CheckParameterUtil.ensureParameterNotNull(data, "data");
41 noteData = data;
42 }
43
44 @Override
45 public String getModeHelpText() {
46 return tr("Click the location where you wish to create a new note");
47 }
48
49 @Override
50 public void enterMode() {
51 super.enterMode();
52 Main.map.mapView.addMouseListener(this);
53 Main.map.keyDetector.addKeyListener(this);
54 }
55
56 @Override
57 public void exitMode() {
58 super.exitMode();
59 Main.map.mapView.removeMouseListener(this);
60 Main.map.keyDetector.removeKeyListener(this);
61 }
62
63 @Override
64 public void mouseClicked(MouseEvent e) {
65 if (!SwingUtilities.isLeftMouseButton(e)) {
66 // allow to pan without distraction
67 return;
68 }
69 Main.map.selectMapMode(Main.map.mapModeSelect);
70
71 NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Create new note"), tr("Create note"));
72 dialog.showNoteDialog(tr("Enter a detailed comment to create a note"), NotesDialog.ICON_NEW);
73
74 if (dialog.getValue() != 1) {
75 Main.debug("User aborted note creation");
76 return;
77 }
78 String input = dialog.getInputText();
79 if (input != null && !input.isEmpty()) {
80 LatLon latlon = Main.map.mapView.getLatLon(e.getPoint().x, e.getPoint().y);
81 noteData.createNote(latlon, input);
82 } else {
83 Notification notification = new Notification(tr("You must enter a comment to create a new note"));
84 notification.setIcon(JOptionPane.WARNING_MESSAGE);
85 notification.show();
86 }
87 }
88
89 @Override
90 public void doKeyPressed(KeyEvent e) {
91 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
92 Main.map.selectMapMode(Main.map.mapModeSelect);
93 }
94 }
95
96 @Override
97 public void doKeyReleased(KeyEvent e) {
98 }
99}
Note: See TracBrowser for help on using the repository browser.