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