1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Dimension;
|
---|
9 | import java.awt.Graphics2D;
|
---|
10 | import java.awt.Point;
|
---|
11 | import java.awt.event.MouseEvent;
|
---|
12 | import java.awt.event.MouseListener;
|
---|
13 | import java.awt.event.MouseWheelEvent;
|
---|
14 | import java.awt.event.MouseWheelListener;
|
---|
15 | import java.io.File;
|
---|
16 | import java.text.DateFormat;
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.Collections;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.regex.Pattern;
|
---|
22 |
|
---|
23 | import javax.swing.Action;
|
---|
24 | import javax.swing.BorderFactory;
|
---|
25 | import javax.swing.Icon;
|
---|
26 | import javax.swing.ImageIcon;
|
---|
27 | import javax.swing.JWindow;
|
---|
28 | import javax.swing.SwingUtilities;
|
---|
29 | import javax.swing.UIManager;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.actions.SaveActionBase;
|
---|
33 | import org.openstreetmap.josm.data.Bounds;
|
---|
34 | import org.openstreetmap.josm.data.notes.Note;
|
---|
35 | import org.openstreetmap.josm.data.notes.Note.State;
|
---|
36 | import org.openstreetmap.josm.data.notes.NoteComment;
|
---|
37 | import org.openstreetmap.josm.data.osm.NoteData;
|
---|
38 | import org.openstreetmap.josm.data.osm.NoteData.NoteDataUpdateListener;
|
---|
39 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
40 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
41 | import org.openstreetmap.josm.gui.MainFrame;
|
---|
42 | import org.openstreetmap.josm.gui.MapView;
|
---|
43 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
44 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
45 | import org.openstreetmap.josm.gui.io.AbstractIOTask;
|
---|
46 | import org.openstreetmap.josm.gui.io.UploadNoteLayerTask;
|
---|
47 | import org.openstreetmap.josm.gui.io.importexport.NoteExporter;
|
---|
48 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
49 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
50 | import org.openstreetmap.josm.io.XmlWriter;
|
---|
51 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
52 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
53 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
54 | import org.openstreetmap.josm.tools.Logging;
|
---|
55 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * A layer to hold Note objects.
|
---|
59 | * @since 7522
|
---|
60 | */
|
---|
61 | public class NoteLayer extends AbstractModifiableLayer implements MouseListener, NoteDataUpdateListener {
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Pattern to detect end of sentences followed by another one, or a link, in western script.
|
---|
65 | * Group 1 (capturing): period, interrogation mark, exclamation mark
|
---|
66 | * Group non capturing: at least one horizontal or vertical whitespace
|
---|
67 | * Group 2 (capturing): a letter (any script), or any punctuation
|
---|
68 | */
|
---|
69 | private static final Pattern SENTENCE_MARKS_WESTERN = Pattern.compile("([\\.\\?\\!])(?:[\\h\\v]+)([\\p{L}\\p{Punct}])");
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Pattern to detect end of sentences followed by another one, or a link, in eastern script.
|
---|
73 | * Group 1 (capturing): ideographic full stop
|
---|
74 | * Group 2 (capturing): a letter (any script), or any punctuation
|
---|
75 | */
|
---|
76 | private static final Pattern SENTENCE_MARKS_EASTERN = Pattern.compile("(\\u3002)([\\p{L}\\p{Punct}])");
|
---|
77 |
|
---|
78 | private final NoteData noteData;
|
---|
79 |
|
---|
80 | private Note displayedNote;
|
---|
81 | private HtmlPanel displayedPanel;
|
---|
82 | private JWindow displayedWindow;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Create a new note layer with a set of notes
|
---|
86 | * @param notes A list of notes to show in this layer
|
---|
87 | * @param name The name of the layer. Typically "Notes"
|
---|
88 | */
|
---|
89 | public NoteLayer(Collection<Note> notes, String name) {
|
---|
90 | super(name);
|
---|
91 | noteData = new NoteData(notes);
|
---|
92 | noteData.addNoteDataUpdateListener(this);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** Convenience constructor that creates a layer with an empty note list */
|
---|
96 | public NoteLayer() {
|
---|
97 | this(Collections.<Note>emptySet(), tr("Notes"));
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public void hookUpMapView() {
|
---|
102 | MainApplication.getMap().mapView.addMouseListener(this);
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public synchronized void destroy() {
|
---|
107 | MainApplication.getMap().mapView.removeMouseListener(this);
|
---|
108 | noteData.removeNoteDataUpdateListener(this);
|
---|
109 | hideNoteWindow();
|
---|
110 | super.destroy();
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Returns the note data store being used by this layer
|
---|
115 | * @return noteData containing layer notes
|
---|
116 | */
|
---|
117 | public NoteData getNoteData() {
|
---|
118 | return noteData;
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override
|
---|
122 | public boolean isModified() {
|
---|
123 | return noteData.isModified();
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | public boolean isUploadable() {
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Override
|
---|
132 | public boolean requiresUploadToServer() {
|
---|
133 | return isModified();
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override
|
---|
137 | public boolean isSavable() {
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | public boolean requiresSaveToFile() {
|
---|
143 | return getAssociatedFile() != null && isModified();
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Override
|
---|
147 | public void paint(Graphics2D g, MapView mv, Bounds box) {
|
---|
148 | final int iconHeight = ImageProvider.ImageSizes.SMALLICON.getAdjustedHeight();
|
---|
149 | final int iconWidth = ImageProvider.ImageSizes.SMALLICON.getAdjustedWidth();
|
---|
150 |
|
---|
151 | for (Note note : noteData.getNotes()) {
|
---|
152 | Point p = mv.getPoint(note.getLatLon());
|
---|
153 |
|
---|
154 | ImageIcon icon;
|
---|
155 | if (note.getId() < 0) {
|
---|
156 | icon = ImageProvider.get("dialogs/notes", "note_new", ImageProvider.ImageSizes.SMALLICON);
|
---|
157 | } else if (note.getState() == State.CLOSED) {
|
---|
158 | icon = ImageProvider.get("dialogs/notes", "note_closed", ImageProvider.ImageSizes.SMALLICON);
|
---|
159 | } else {
|
---|
160 | icon = ImageProvider.get("dialogs/notes", "note_open", ImageProvider.ImageSizes.SMALLICON);
|
---|
161 | }
|
---|
162 | int width = icon.getIconWidth();
|
---|
163 | int height = icon.getIconHeight();
|
---|
164 | g.drawImage(icon.getImage(), p.x - (width / 2), p.y - height, MainApplication.getMap().mapView);
|
---|
165 | }
|
---|
166 | Note selectedNote = noteData.getSelectedNote();
|
---|
167 | if (selectedNote != null) {
|
---|
168 | paintSelectedNote(g, mv, iconHeight, iconWidth, selectedNote);
|
---|
169 | } else {
|
---|
170 | hideNoteWindow();
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | private void hideNoteWindow() {
|
---|
175 | if (displayedWindow != null) {
|
---|
176 | displayedWindow.setVisible(false);
|
---|
177 | for (MouseWheelListener listener : displayedWindow.getMouseWheelListeners()) {
|
---|
178 | displayedWindow.removeMouseWheelListener(listener);
|
---|
179 | }
|
---|
180 | displayedWindow.dispose();
|
---|
181 | displayedWindow = null;
|
---|
182 | displayedPanel = null;
|
---|
183 | displayedNote = null;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | private void paintSelectedNote(Graphics2D g, MapView mv, final int iconHeight, final int iconWidth, Note selectedNote) {
|
---|
188 | Point p = mv.getPoint(selectedNote.getLatLon());
|
---|
189 |
|
---|
190 | g.setColor(ColorHelper.html2color(Config.getPref().get("color.selected")));
|
---|
191 | g.drawRect(p.x - (iconWidth / 2), p.y - iconHeight, iconWidth - 1, iconHeight - 1);
|
---|
192 |
|
---|
193 | if (displayedNote != null && !displayedNote.equals(selectedNote)) {
|
---|
194 | hideNoteWindow();
|
---|
195 | }
|
---|
196 |
|
---|
197 | Point screenloc = mv.getLocationOnScreen();
|
---|
198 | int tx = screenloc.x + p.x + (iconWidth / 2) + 5;
|
---|
199 | int ty = screenloc.y + p.y - iconHeight - 1;
|
---|
200 |
|
---|
201 | String text = getNoteToolTip(selectedNote);
|
---|
202 |
|
---|
203 | if (displayedWindow == null) {
|
---|
204 | displayedPanel = new HtmlPanel(text);
|
---|
205 | displayedPanel.setBackground(UIManager.getColor("ToolTip.background"));
|
---|
206 | displayedPanel.setForeground(UIManager.getColor("ToolTip.foreground"));
|
---|
207 | displayedPanel.setFont(UIManager.getFont("ToolTip.font"));
|
---|
208 | displayedPanel.setBorder(BorderFactory.createLineBorder(Color.black));
|
---|
209 | displayedPanel.enableClickableHyperlinks();
|
---|
210 | fixPanelSize(mv, text);
|
---|
211 | displayedWindow = new JWindow((MainFrame) Main.parent);
|
---|
212 | displayedWindow.setAutoRequestFocus(false);
|
---|
213 | displayedWindow.add(displayedPanel);
|
---|
214 | // Forward mouse wheel scroll event to MapMover
|
---|
215 | displayedWindow.addMouseWheelListener(e -> mv.getMapMover().mouseWheelMoved(
|
---|
216 | (MouseWheelEvent) SwingUtilities.convertMouseEvent(displayedWindow, e, mv)));
|
---|
217 | } else {
|
---|
218 | displayedPanel.setText(text);
|
---|
219 | fixPanelSize(mv, text);
|
---|
220 | }
|
---|
221 |
|
---|
222 | displayedWindow.pack();
|
---|
223 | displayedWindow.setLocation(tx, ty);
|
---|
224 | displayedWindow.setVisible(mv.contains(p));
|
---|
225 | displayedNote = selectedNote;
|
---|
226 | }
|
---|
227 |
|
---|
228 | private void fixPanelSize(MapView mv, String text) {
|
---|
229 | Dimension d = displayedPanel.getPreferredSize();
|
---|
230 | if (d.width > mv.getWidth() / 2) {
|
---|
231 | // To make sure long notes are displayed correctly
|
---|
232 | displayedPanel.setText(insertLineBreaks(text));
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Inserts HTML line breaks ({@code <br>} at the end of each sentence mark
|
---|
238 | * (period, interrogation mark, exclamation mark, ideographic full stop).
|
---|
239 | * @param longText a long text that does not fit on a single line without exceeding half of the map view
|
---|
240 | * @return text with line breaks
|
---|
241 | */
|
---|
242 | static String insertLineBreaks(String longText) {
|
---|
243 | return SENTENCE_MARKS_WESTERN.matcher(SENTENCE_MARKS_EASTERN.matcher(longText).replaceAll("$1<br>$2")).replaceAll("$1<br>$2");
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Returns the HTML-formatted tooltip text for the given note.
|
---|
248 | * @param note note to display
|
---|
249 | * @return the HTML-formatted tooltip text for the given note
|
---|
250 | * @since 13111
|
---|
251 | */
|
---|
252 | public static String getNoteToolTip(Note note) {
|
---|
253 | StringBuilder sb = new StringBuilder("<html>");
|
---|
254 | sb.append(tr("Note"))
|
---|
255 | .append(' ').append(note.getId());
|
---|
256 | for (NoteComment comment : note.getComments()) {
|
---|
257 | String commentText = comment.getText();
|
---|
258 | //closing a note creates an empty comment that we don't want to show
|
---|
259 | if (commentText != null && !commentText.trim().isEmpty()) {
|
---|
260 | sb.append("<hr/>");
|
---|
261 | String userName = XmlWriter.encode(comment.getUser().getName());
|
---|
262 | if (userName == null || userName.trim().isEmpty()) {
|
---|
263 | userName = "<Anonymous>";
|
---|
264 | }
|
---|
265 | sb.append(userName)
|
---|
266 | .append(" on ")
|
---|
267 | .append(DateUtils.getDateFormat(DateFormat.MEDIUM).format(comment.getCommentTimestamp()))
|
---|
268 | .append(":<br>");
|
---|
269 | String htmlText = XmlWriter.encode(comment.getText(), true);
|
---|
270 | // encode method leaves us with entity instead of \n
|
---|
271 | htmlText = htmlText.replace("
", "<br>");
|
---|
272 | // convert URLs to proper HTML links
|
---|
273 | htmlText = htmlText.replaceAll("(https?://\\S+)", "<a href=\"$1\">$1</a>");
|
---|
274 | sb.append(htmlText);
|
---|
275 | }
|
---|
276 | }
|
---|
277 | sb.append("</html>");
|
---|
278 | String result = sb.toString();
|
---|
279 | Logging.debug(result);
|
---|
280 | return result;
|
---|
281 | }
|
---|
282 |
|
---|
283 | @Override
|
---|
284 | public Icon getIcon() {
|
---|
285 | return ImageProvider.get("dialogs/notes", "note_open", ImageProvider.ImageSizes.SMALLICON);
|
---|
286 | }
|
---|
287 |
|
---|
288 | @Override
|
---|
289 | public String getToolTipText() {
|
---|
290 | return trn("{0} note", "{0} notes", noteData.getNotes().size(), noteData.getNotes().size());
|
---|
291 | }
|
---|
292 |
|
---|
293 | @Override
|
---|
294 | public void mergeFrom(Layer from) {
|
---|
295 | throw new UnsupportedOperationException("Notes layer does not support merging yet");
|
---|
296 | }
|
---|
297 |
|
---|
298 | @Override
|
---|
299 | public boolean isMergable(Layer other) {
|
---|
300 | return false;
|
---|
301 | }
|
---|
302 |
|
---|
303 | @Override
|
---|
304 | public void visitBoundingBox(BoundingXYVisitor v) {
|
---|
305 | for (Note note : noteData.getNotes()) {
|
---|
306 | v.visit(note.getLatLon());
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | @Override
|
---|
311 | public Object getInfoComponent() {
|
---|
312 | StringBuilder sb = new StringBuilder();
|
---|
313 | sb.append(tr("Notes layer"))
|
---|
314 | .append('\n')
|
---|
315 | .append(tr("Total notes:"))
|
---|
316 | .append(' ')
|
---|
317 | .append(noteData.getNotes().size())
|
---|
318 | .append('\n')
|
---|
319 | .append(tr("Changes need uploading?"))
|
---|
320 | .append(' ')
|
---|
321 | .append(isModified());
|
---|
322 | return sb.toString();
|
---|
323 | }
|
---|
324 |
|
---|
325 | @Override
|
---|
326 | public Action[] getMenuEntries() {
|
---|
327 | List<Action> actions = new ArrayList<>();
|
---|
328 | actions.add(LayerListDialog.getInstance().createShowHideLayerAction());
|
---|
329 | actions.add(LayerListDialog.getInstance().createDeleteLayerAction());
|
---|
330 | actions.add(new LayerListPopup.InfoAction(this));
|
---|
331 | actions.add(new LayerSaveAction(this));
|
---|
332 | actions.add(new LayerSaveAsAction(this));
|
---|
333 | return actions.toArray(new Action[actions.size()]);
|
---|
334 | }
|
---|
335 |
|
---|
336 | @Override
|
---|
337 | public void mouseClicked(MouseEvent e) {
|
---|
338 | if (!SwingUtilities.isLeftMouseButton(e)) {
|
---|
339 | return;
|
---|
340 | }
|
---|
341 | Point clickPoint = e.getPoint();
|
---|
342 | double snapDistance = 10;
|
---|
343 | double minDistance = Double.MAX_VALUE;
|
---|
344 | final int iconHeight = ImageProvider.ImageSizes.SMALLICON.getAdjustedHeight();
|
---|
345 | Note closestNote = null;
|
---|
346 | for (Note note : noteData.getNotes()) {
|
---|
347 | Point notePoint = MainApplication.getMap().mapView.getPoint(note.getLatLon());
|
---|
348 | //move the note point to the center of the icon where users are most likely to click when selecting
|
---|
349 | notePoint.setLocation(notePoint.getX(), notePoint.getY() - iconHeight / 2);
|
---|
350 | double dist = clickPoint.distanceSq(notePoint);
|
---|
351 | if (minDistance > dist && clickPoint.distance(notePoint) < snapDistance) {
|
---|
352 | minDistance = dist;
|
---|
353 | closestNote = note;
|
---|
354 | }
|
---|
355 | }
|
---|
356 | noteData.setSelectedNote(closestNote);
|
---|
357 | }
|
---|
358 |
|
---|
359 | @Override
|
---|
360 | public File createAndOpenSaveFileChooser() {
|
---|
361 | return SaveActionBase.createAndOpenSaveFileChooser(tr("Save Note file"), NoteExporter.FILE_FILTER);
|
---|
362 | }
|
---|
363 |
|
---|
364 | @Override
|
---|
365 | public AbstractIOTask createUploadTask(ProgressMonitor monitor) {
|
---|
366 | return new UploadNoteLayerTask(this, monitor);
|
---|
367 | }
|
---|
368 |
|
---|
369 | @Override
|
---|
370 | public void mousePressed(MouseEvent e) {
|
---|
371 | // Do nothing
|
---|
372 | }
|
---|
373 |
|
---|
374 | @Override
|
---|
375 | public void mouseReleased(MouseEvent e) {
|
---|
376 | // Do nothing
|
---|
377 | }
|
---|
378 |
|
---|
379 | @Override
|
---|
380 | public void mouseEntered(MouseEvent e) {
|
---|
381 | // Do nothing
|
---|
382 | }
|
---|
383 |
|
---|
384 | @Override
|
---|
385 | public void mouseExited(MouseEvent e) {
|
---|
386 | // Do nothing
|
---|
387 | }
|
---|
388 |
|
---|
389 | @Override
|
---|
390 | public void noteDataUpdated(NoteData data) {
|
---|
391 | invalidate();
|
---|
392 | }
|
---|
393 |
|
---|
394 | @Override
|
---|
395 | public void selectedNoteChanged(NoteData noteData) {
|
---|
396 | invalidate();
|
---|
397 | }
|
---|
398 | }
|
---|