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

Last change on this file since 8264 was 8264, checked in by simon04, 9 years ago

hopefully fix #10864 - revert r8235, r8213 - Note layer: return to JToolTip based rendering - wrap long URLs using zero width space U+8203

  • Property svn:eol-style set to native
File size: 9.0 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;
5
6import java.awt.Dimension;
7import java.awt.Graphics2D;
8import java.awt.Point;
9import java.awt.event.MouseEvent;
10import java.awt.event.MouseListener;
11import java.io.File;
12import java.text.DateFormat;
13import java.util.ArrayList;
14import java.util.Collection;
15import java.util.Collections;
16import java.util.List;
17
18import javax.swing.Action;
19import javax.swing.Icon;
20import javax.swing.ImageIcon;
21import javax.swing.JToolTip;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.actions.SaveActionBase;
25import org.openstreetmap.josm.data.Bounds;
26import org.openstreetmap.josm.data.notes.Note;
27import org.openstreetmap.josm.data.notes.Note.State;
28import org.openstreetmap.josm.data.notes.NoteComment;
29import org.openstreetmap.josm.data.osm.NoteData;
30import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
31import org.openstreetmap.josm.gui.MapView;
32import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
33import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
34import org.openstreetmap.josm.gui.dialogs.NotesDialog;
35import org.openstreetmap.josm.io.NoteExporter;
36import org.openstreetmap.josm.io.XmlWriter;
37import org.openstreetmap.josm.tools.ColorHelper;
38import org.openstreetmap.josm.tools.date.DateUtils;
39
40/**
41 * A layer to hold Note objects
42 */
43public class NoteLayer extends AbstractModifiableLayer implements MouseListener {
44
45 private final NoteData noteData;
46
47 /**
48 * Create a new note layer with a set of notes
49 * @param notes A list of notes to show in this layer
50 * @param name The name of the layer. Typically "Notes"
51 */
52 public NoteLayer(Collection<Note> notes, String name) {
53 super(name);
54 noteData = new NoteData(notes);
55 }
56
57 /** Convenience constructor that creates a layer with an empty note list */
58 public NoteLayer() {
59 this(Collections.<Note>emptySet(), tr("Notes"));
60 }
61
62 @Override
63 public void hookUpMapView() {
64 Main.map.mapView.addMouseListener(this);
65 }
66
67 /**
68 * Returns the note data store being used by this layer
69 * @return noteData containing layer notes
70 */
71 public NoteData getNoteData() {
72 return noteData;
73 }
74
75 @Override
76 public boolean isModified() {
77 return noteData.isModified();
78 }
79
80 @Override
81 public boolean requiresUploadToServer() {
82 return isModified();
83 }
84
85 @Override
86 public boolean isSavable() {
87 return true;
88 }
89
90 @Override
91 public boolean requiresSaveToFile() {
92 return getAssociatedFile() != null && isModified();
93 }
94
95 @Override
96 public void paint(Graphics2D g, MapView mv, Bounds box) {
97 for (Note note : noteData.getNotes()) {
98 Point p = mv.getPoint(note.getLatLon());
99
100 ImageIcon icon = null;
101 if (note.getId() < 0) {
102 icon = NotesDialog.ICON_NEW_SMALL;
103 } else if (note.getState() == State.closed) {
104 icon = NotesDialog.ICON_CLOSED_SMALL;
105 } else {
106 icon = NotesDialog.ICON_OPEN_SMALL;
107 }
108 int width = icon.getIconWidth();
109 int height = icon.getIconHeight();
110 g.drawImage(icon.getImage(), p.x - (width / 2), p.y - height, Main.map.mapView);
111 }
112 if (noteData.getSelectedNote() != null) {
113 StringBuilder sb = new StringBuilder("<html>");
114 sb.append(tr("Note"));
115 sb.append(" ").append(noteData.getSelectedNote().getId());
116 for (NoteComment comment : noteData.getSelectedNote().getComments()) {
117 String commentText = comment.getText();
118 //closing a note creates an empty comment that we don't want to show
119 if (commentText != null && commentText.trim().length() > 0) {
120 sb.append("<hr/>");
121 String userName = XmlWriter.encode(comment.getUser().getName());
122 if (userName == null || userName.trim().length() == 0) {
123 userName = "&lt;Anonymous&gt;";
124 }
125 sb.append(userName);
126 sb.append(" on ");
127 sb.append(DateUtils.getDateFormat(DateFormat.MEDIUM).format(comment.getCommentTimestamp()));
128 sb.append(":<br/>");
129 String htmlText = XmlWriter.encode(comment.getText(), true);
130 htmlText = htmlText.replace("&#xA;", "<br/>"); //encode method leaves us with entity instead of \n
131 htmlText = htmlText.replace("/", "/\u200b"); //zero width space to wrap long URLs (see #10864)
132 sb.append(htmlText);
133 }
134 }
135 sb.append("</html>");
136 JToolTip toolTip = new JToolTip();
137 toolTip.setTipText(sb.toString());
138 Point p = mv.getPoint(noteData.getSelectedNote().getLatLon());
139
140 g.setColor(ColorHelper.html2color(Main.pref.get("color.selected")));
141 g.drawRect(p.x - (NotesDialog.ICON_SMALL_SIZE / 2), p.y - NotesDialog.ICON_SMALL_SIZE, NotesDialog.ICON_SMALL_SIZE - 1, NotesDialog.ICON_SMALL_SIZE - 1);
142
143 int tx = p.x + (NotesDialog.ICON_SMALL_SIZE / 2) + 5;
144 int ty = p.y - NotesDialog.ICON_SMALL_SIZE - 1;
145 g.translate(tx, ty);
146
147 //Carried over from the OSB plugin. Not entirely sure why it is needed
148 //but without it, the tooltip doesn't get sized correctly
149 for (int x = 0; x < 2; x++) {
150 Dimension d = toolTip.getUI().getPreferredSize(toolTip);
151 d.width = Math.min(d.width, (mv.getWidth() / 2));
152 if (d.width > 0 && d.height > 0) {
153 toolTip.setSize(d);
154 try {
155 toolTip.paint(g);
156 } catch (IllegalArgumentException e) {
157 // See #11123 - https://bugs.openjdk.java.net/browse/JDK-6719550
158 // Ignore the exception, as Netbeans does: http://hg.netbeans.org/main-silver/rev/c96f4d5fbd20
159 Main.error(e, false);
160 }
161 }
162 }
163 g.translate(-tx, -ty);
164 }
165 }
166
167 @Override
168 public Icon getIcon() {
169 return NotesDialog.ICON_OPEN_SMALL;
170 }
171
172 @Override
173 public String getToolTipText() {
174 return noteData.getNotes().size() + " " + tr("Notes");
175 }
176
177 @Override
178 public void mergeFrom(Layer from) {
179 throw new UnsupportedOperationException("Notes layer does not support merging yet");
180 }
181
182 @Override
183 public boolean isMergable(Layer other) {
184 return false;
185 }
186
187 @Override
188 public void visitBoundingBox(BoundingXYVisitor v) {
189 for (Note note : noteData.getNotes()) {
190 v.visit(note.getLatLon());
191 }
192 }
193
194 @Override
195 public Object getInfoComponent() {
196 StringBuilder sb = new StringBuilder();
197 sb.append(tr("Notes layer"));
198 sb.append("\n");
199 sb.append(tr("Total notes:"));
200 sb.append(" ");
201 sb.append(noteData.getNotes().size());
202 sb.append("\n");
203 sb.append(tr("Changes need uploading?"));
204 sb.append(" ");
205 sb.append(isModified());
206 return sb.toString();
207 }
208
209 @Override
210 public Action[] getMenuEntries() {
211 List<Action> actions = new ArrayList<>();
212 actions.add(LayerListDialog.getInstance().createShowHideLayerAction());
213 actions.add(LayerListDialog.getInstance().createDeleteLayerAction());
214 actions.add(new LayerListPopup.InfoAction(this));
215 actions.add(new LayerSaveAction(this));
216 actions.add(new LayerSaveAsAction(this));
217 return actions.toArray(new Action[actions.size()]);
218 }
219
220 @Override
221 public void mouseClicked(MouseEvent e) {
222 if (e.getButton() != MouseEvent.BUTTON1) {
223 return;
224 }
225 Point clickPoint = e.getPoint();
226 double snapDistance = 10;
227 double minDistance = Double.MAX_VALUE;
228 Note closestNote = null;
229 for (Note note : noteData.getNotes()) {
230 Point notePoint = Main.map.mapView.getPoint(note.getLatLon());
231 //move the note point to the center of the icon where users are most likely to click when selecting
232 notePoint.setLocation(notePoint.getX(), notePoint.getY() - NotesDialog.ICON_SMALL_SIZE / 2);
233 double dist = clickPoint.distanceSq(notePoint);
234 if (minDistance > dist && clickPoint.distance(notePoint) < snapDistance ) {
235 minDistance = dist;
236 closestNote = note;
237 }
238 }
239 noteData.setSelectedNote(closestNote);
240 }
241
242 @Override
243 public File createAndOpenSaveFileChooser() {
244 return SaveActionBase.createAndOpenSaveFileChooser(tr("Save GPX file"), NoteExporter.FILE_FILTER);
245 }
246
247 @Override
248 public void mousePressed(MouseEvent e) { }
249
250 @Override
251 public void mouseReleased(MouseEvent e) { }
252
253 @Override
254 public void mouseEntered(MouseEvent e) { }
255
256 @Override
257 public void mouseExited(MouseEvent e) { }
258}
Note: See TracBrowser for help on using the repository browser.