source: josm/trunk/src/org/openstreetmap/josm/gui/layer/NoteLayer.java@ 13118

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

fix #11217 - do not steel focus when the selected note changes

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