source: josm/trunk/src/org/openstreetmap/josm/data/osm/NoteData.java@ 18868

Last change on this file since 18868 was 18868, checked in by taylor.smock, 8 months ago

See #16207: Keep track of download area for notes

  • Property svn:eol-style set to native
File size: 11.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.time.Instant;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.Comparator;
9import java.util.HashSet;
10import java.util.List;
11import java.util.Map;
12import java.util.Set;
13
14import org.openstreetmap.josm.data.Data;
15import org.openstreetmap.josm.data.DataSource;
16import org.openstreetmap.josm.data.UserIdentityManager;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.notes.Note;
19import org.openstreetmap.josm.data.notes.Note.State;
20import org.openstreetmap.josm.data.notes.NoteComment;
21import org.openstreetmap.josm.tools.ListenerList;
22import org.openstreetmap.josm.tools.Logging;
23import org.openstreetmap.josm.tools.Utils;
24
25/**
26 * Class to hold and perform operations on a set of notes
27 */
28public class NoteData implements Data {
29
30 /**
31 * A listener that can be informed on note data changes.
32 * @author Michael Zangl
33 * @since 12343
34 */
35 public interface NoteDataUpdateListener {
36 /**
37 * Called when the note data is updated
38 * @param data The data that was changed
39 */
40 void noteDataUpdated(NoteData data);
41
42 /**
43 * The selected node was changed
44 * @param noteData The data of which the selected node was changed
45 */
46 void selectedNoteChanged(NoteData noteData);
47 }
48
49 private long newNoteId = -1;
50
51 private final Storage<Note> noteList;
52 private Note selectedNote;
53 private Comparator<Note> comparator = Note.DEFAULT_COMPARATOR;
54
55 private final ListenerList<NoteDataUpdateListener> listeners = ListenerList.create();
56
57 private final Set<DataSource> dataSourceSet = new HashSet<>();
58
59 /**
60 * Construct a new note container without notes
61 * @since 14101
62 */
63 public NoteData() {
64 this(null);
65 }
66
67 /**
68 * Construct a new note container with a given list of notes
69 * @param notes The list of notes to populate the container with
70 */
71 public NoteData(Collection<Note> notes) {
72 noteList = new Storage<>();
73 if (notes != null) {
74 for (Note note : notes) {
75 noteList.add(note);
76 if (note.getId() <= newNoteId) {
77 newNoteId = note.getId() - 1;
78 }
79 }
80 }
81 }
82
83 /**
84 * Returns the notes stored in this layer
85 * @return collection of notes
86 */
87 public Collection<Note> getNotes() {
88 return Collections.unmodifiableCollection(noteList);
89 }
90
91 /**
92 * Returns the notes stored in this layer sorted according to {@link #comparator}
93 * @return sorted collection of notes
94 */
95 public Collection<Note> getSortedNotes() {
96 final List<Note> list = new ArrayList<>(noteList);
97 list.sort(comparator);
98 return list;
99 }
100
101 /**
102 * Returns the currently selected note
103 * @return currently selected note
104 */
105 public Note getSelectedNote() {
106 return selectedNote;
107 }
108
109 /**
110 * Set a selected note. Causes the dialog to select the note and
111 * the note layer to draw the selected note's comments.
112 * @param note Selected note. Null indicates no selection
113 */
114 public void setSelectedNote(Note note) {
115 selectedNote = note;
116 listeners.fireEvent(l -> l.selectedNoteChanged(this));
117 }
118
119 /**
120 * Return whether or not there are any changes in the note data set.
121 * These changes may need to be either uploaded or saved.
122 * @return true if local modifications have been made to the note data set. False otherwise.
123 */
124 public synchronized boolean isModified() {
125 for (Note note : noteList) {
126 if (note.getId() < 0) { //notes with negative IDs are new
127 return true;
128 }
129 for (NoteComment comment : note.getComments()) {
130 if (comment.isNew()) {
131 return true;
132 }
133 }
134 }
135 return false;
136 }
137
138 /**
139 * Merge notes from an existing note data.
140 * @param from existing note data
141 * @since 13437
142 */
143 public synchronized void mergeFrom(NoteData from) {
144 if (this != from) {
145 addNotes(from.noteList);
146 from.getDataSources().forEach(this::addDataSource);
147 }
148 }
149
150 /**
151 * Add notes to the data set. It only adds a note if the ID is not already present
152 * @param newNotes A list of notes to add
153 */
154 public synchronized void addNotes(Collection<Note> newNotes) {
155 for (Note newNote : newNotes) {
156 if (!noteList.contains(newNote)) {
157 noteList.add(newNote);
158 } else {
159 final Note existingNote = noteList.get(newNote);
160 final boolean isDirty = existingNote.getComments().stream().anyMatch(NoteComment::isNew);
161 if (!isDirty) {
162 noteList.put(newNote);
163 } else {
164 // TODO merge comments?
165 Logging.info("Keeping existing note id={0} with uncommitted changes", String.valueOf(newNote.getId()));
166 }
167 }
168 if (newNote.getId() <= newNoteId) {
169 newNoteId = newNote.getId() - 1;
170 }
171 }
172 dataUpdated();
173 }
174
175 /**
176 * Create a new note
177 * @param location Location of note
178 * @param text Required comment with which to open the note
179 */
180 public synchronized void createNote(LatLon location, String text) {
181 if (Utils.isEmpty(text)) {
182 throw new IllegalArgumentException("Comment can not be blank when creating a note");
183 }
184 Note note = new Note(location);
185 note.setCreatedAt(Instant.now());
186 note.setState(State.OPEN);
187 note.setId(newNoteId--);
188 NoteComment comment = new NoteComment(Instant.now(), getCurrentUser(), text, NoteComment.Action.OPENED, true);
189 note.addComment(comment);
190 if (Logging.isDebugEnabled()) {
191 Logging.debug("Created note {0} with comment: {1}", note.getId(), text);
192 }
193 noteList.add(note);
194 dataUpdated();
195 }
196
197 /**
198 * Add a new comment to an existing note
199 * @param note Note to add comment to. Must already exist in the layer
200 * @param text Comment to add
201 */
202 public synchronized void addCommentToNote(Note note, String text) {
203 if (!noteList.contains(note)) {
204 throw new IllegalArgumentException("Note to modify must be in layer");
205 }
206 if (note.getState() == State.CLOSED) {
207 throw new IllegalStateException("Cannot add a comment to a closed note");
208 }
209 if (Logging.isDebugEnabled()) {
210 Logging.debug("Adding comment to note {0}: {1}", note.getId(), text);
211 }
212 NoteComment comment = new NoteComment(Instant.now(), getCurrentUser(), text, NoteComment.Action.COMMENTED, true);
213 note.addComment(comment);
214 dataUpdated();
215 }
216
217 /**
218 * Close note with comment
219 * @param note Note to close. Must already exist in the layer
220 * @param text Comment to attach to close action, if desired
221 */
222 public synchronized void closeNote(Note note, String text) {
223 if (!noteList.contains(note)) {
224 throw new IllegalArgumentException("Note to close must be in layer");
225 }
226 if (note.getState() != State.OPEN) {
227 throw new IllegalStateException("Cannot close a note that isn't open");
228 }
229 if (Logging.isDebugEnabled()) {
230 Logging.debug("closing note {0} with comment: {1}", note.getId(), text);
231 }
232 Instant now = Instant.now();
233 note.addComment(new NoteComment(now, getCurrentUser(), text, NoteComment.Action.CLOSED, true));
234 note.setState(State.CLOSED);
235 note.setClosedAt(now);
236 dataUpdated();
237 }
238
239 /**
240 * Reopen a closed note.
241 * @param note Note to reopen. Must already exist in the layer
242 * @param text Comment to attach to the reopen action, if desired
243 */
244 public synchronized void reOpenNote(Note note, String text) {
245 if (!noteList.contains(note)) {
246 throw new IllegalArgumentException("Note to reopen must be in layer");
247 }
248 if (note.getState() != State.CLOSED) {
249 throw new IllegalStateException("Cannot reopen a note that isn't closed");
250 }
251 Logging.debug("reopening note {0} with comment: {1}", note.getId(), text);
252 NoteComment comment = new NoteComment(Instant.now(), getCurrentUser(), text, NoteComment.Action.REOPENED, true);
253 note.addComment(comment);
254 note.setState(State.OPEN);
255 dataUpdated();
256 }
257
258 private void dataUpdated() {
259 listeners.fireEvent(l -> l.noteDataUpdated(this));
260 }
261
262 private static User getCurrentUser() {
263 return UserIdentityManager.getInstance().asUser();
264 }
265
266 /**
267 * Updates notes with new state. Primarily to be used when updating the
268 * note layer after uploading note changes to the server.
269 * @param updatedNotes Map containing the original note as the key and the updated note as the value
270 */
271 public synchronized void updateNotes(Map<Note, Note> updatedNotes) {
272 for (Map.Entry<Note, Note> entry : updatedNotes.entrySet()) {
273 Note oldNote = entry.getKey();
274 Note newNote = entry.getValue();
275 boolean reindex = oldNote.hashCode() != newNote.hashCode();
276 if (reindex) {
277 noteList.removeElem(oldNote);
278 }
279 oldNote.updateWith(newNote);
280 if (reindex) {
281 noteList.add(oldNote);
282 }
283 }
284 dataUpdated();
285 }
286
287 /**
288 * Returns the current comparator being used to sort the note list.
289 * @return The current comparator being used to sort the note list
290 */
291 public Comparator<Note> getCurrentSortMethod() {
292 return comparator;
293 }
294
295 /** Set the comparator to be used to sort the note list. Several are available
296 * as public static members of this class.
297 * @param comparator - The Note comparator to sort by
298 */
299 public void setSortMethod(Comparator<Note> comparator) {
300 this.comparator = comparator;
301 dataUpdated();
302 }
303
304 /**
305 * Adds a listener that listens to node data changes
306 * @param listener The listener
307 */
308 public void addNoteDataUpdateListener(NoteDataUpdateListener listener) {
309 listeners.addListener(listener);
310 }
311
312 /**
313 * Removes a listener that listens to node data changes
314 * @param listener The listener
315 */
316 public void removeNoteDataUpdateListener(NoteDataUpdateListener listener) {
317 listeners.removeListener(listener);
318 }
319
320 @Override
321 public Collection<DataSource> getDataSources() {
322 return Collections.unmodifiableSet(this.dataSourceSet);
323 }
324
325 /**
326 * Adds a new data source.
327 * @param source data source to add
328 * @return {@code true} if the collection changed as a result of the call
329 * @since 18868
330 */
331 public synchronized boolean addDataSource(DataSource source) {
332 return this.dataSourceSet.add(source);
333 }
334}
Note: See TracBrowser for help on using the repository browser.