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

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

global use of Utils.isEmpty/isBlank

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