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

Last change on this file since 12505 was 12343, checked in by michael2402, 7 years ago

Add the ability to listen to NoteData changes and use it to repaint notes layer.

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