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

Last change on this file since 12816 was 12743, checked in by Don-vip, 7 years ago

see #15229 - see #15182 - deprecate gui.JosmUserIdentityManager - replaced by data.UserIdentityManager

  • Property svn:eol-style set to native
File size: 10.0 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.data.UserIdentityManager;
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.tools.ListenerList;
18import org.openstreetmap.josm.tools.Logging;
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 /**
87 * Returns the currently selected note
88 * @return currently selected note
89 */
90 public Note getSelectedNote() {
91 return selectedNote;
92 }
93
94 /**
95 * Set a selected note. Causes the dialog to select the note and
96 * the note layer to draw the selected note's comments.
97 * @param note Selected note. Null indicates no selection
98 */
99 public void setSelectedNote(Note note) {
100 selectedNote = note;
101 listeners.fireEvent(l -> l.selectedNoteChanged(this));
102 }
103
104 /**
105 * Return whether or not there are any changes in the note data set.
106 * These changes may need to be either uploaded or saved.
107 * @return true if local modifications have been made to the note data set. False otherwise.
108 */
109 public synchronized boolean isModified() {
110 for (Note note : noteList) {
111 if (note.getId() < 0) { //notes with negative IDs are new
112 return true;
113 }
114 for (NoteComment comment : note.getComments()) {
115 if (comment.isNew()) {
116 return true;
117 }
118 }
119 }
120 return false;
121 }
122
123 /**
124 * Add notes to the data set. It only adds a note if the ID is not already present
125 * @param newNotes A list of notes to add
126 */
127 public synchronized void addNotes(Collection<Note> newNotes) {
128 for (Note newNote : newNotes) {
129 if (!noteList.contains(newNote)) {
130 noteList.add(newNote);
131 } else {
132 final Note existingNote = noteList.get(newNote);
133 final boolean isDirty = existingNote.getComments().stream().anyMatch(NoteComment::isNew);
134 if (!isDirty) {
135 noteList.put(newNote);
136 } else {
137 // TODO merge comments?
138 Logging.info("Keeping existing note id={0} with uncommitted changes", String.valueOf(newNote.getId()));
139 }
140 }
141 if (newNote.getId() <= newNoteId) {
142 newNoteId = newNote.getId() - 1;
143 }
144 }
145 dataUpdated();
146 }
147
148 /**
149 * Create a new note
150 * @param location Location of note
151 * @param text Required comment with which to open the note
152 */
153 public synchronized void createNote(LatLon location, String text) {
154 if (text == null || text.isEmpty()) {
155 throw new IllegalArgumentException("Comment can not be blank when creating a note");
156 }
157 Note note = new Note(location);
158 note.setCreatedAt(new Date());
159 note.setState(State.OPEN);
160 note.setId(newNoteId--);
161 NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.OPENED, true);
162 note.addComment(comment);
163 if (Logging.isDebugEnabled()) {
164 Logging.debug("Created note {0} with comment: {1}", note.getId(), text);
165 }
166 noteList.add(note);
167 dataUpdated();
168 }
169
170 /**
171 * Add a new comment to an existing note
172 * @param note Note to add comment to. Must already exist in the layer
173 * @param text Comment to add
174 */
175 public synchronized void addCommentToNote(Note note, String text) {
176 if (!noteList.contains(note)) {
177 throw new IllegalArgumentException("Note to modify must be in layer");
178 }
179 if (note.getState() == State.CLOSED) {
180 throw new IllegalStateException("Cannot add a comment to a closed note");
181 }
182 if (Logging.isDebugEnabled()) {
183 Logging.debug("Adding comment to note {0}: {1}", note.getId(), text);
184 }
185 NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.COMMENTED, true);
186 note.addComment(comment);
187 dataUpdated();
188 }
189
190 /**
191 * Close note with comment
192 * @param note Note to close. Must already exist in the layer
193 * @param text Comment to attach to close action, if desired
194 */
195 public synchronized void closeNote(Note note, String text) {
196 if (!noteList.contains(note)) {
197 throw new IllegalArgumentException("Note to close must be in layer");
198 }
199 if (note.getState() != State.OPEN) {
200 throw new IllegalStateException("Cannot close a note that isn't open");
201 }
202 if (Logging.isDebugEnabled()) {
203 Logging.debug("closing note {0} with comment: {1}", note.getId(), text);
204 }
205 NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.CLOSED, true);
206 note.addComment(comment);
207 note.setState(State.CLOSED);
208 note.setClosedAt(new Date());
209 dataUpdated();
210 }
211
212 /**
213 * Reopen a closed note.
214 * @param note Note to reopen. Must already exist in the layer
215 * @param text Comment to attach to the reopen action, if desired
216 */
217 public synchronized void reOpenNote(Note note, String text) {
218 if (!noteList.contains(note)) {
219 throw new IllegalArgumentException("Note to reopen must be in layer");
220 }
221 if (note.getState() != State.CLOSED) {
222 throw new IllegalStateException("Cannot reopen a note that isn't closed");
223 }
224 Logging.debug("reopening note {0} with comment: {1}", note.getId(), text);
225 NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.REOPENED, true);
226 note.addComment(comment);
227 note.setState(State.OPEN);
228 dataUpdated();
229 }
230
231 private void dataUpdated() {
232 listeners.fireEvent(l -> l.noteDataUpdated(this));
233 }
234
235 private static User getCurrentUser() {
236 UserIdentityManager userMgr = UserIdentityManager.getInstance();
237 return User.createOsmUser(userMgr.getUserId(), userMgr.getUserName());
238 }
239
240 /**
241 * Updates notes with new state. Primarily to be used when updating the
242 * note layer after uploading note changes to the server.
243 * @param updatedNotes Map containing the original note as the key and the updated note as the value
244 */
245 public synchronized void updateNotes(Map<Note, Note> updatedNotes) {
246 for (Map.Entry<Note, Note> entry : updatedNotes.entrySet()) {
247 Note oldNote = entry.getKey();
248 Note newNote = entry.getValue();
249 boolean reindex = oldNote.hashCode() != newNote.hashCode();
250 if (reindex) {
251 noteList.removeElem(oldNote);
252 }
253 oldNote.updateWith(newNote);
254 if (reindex) {
255 noteList.add(oldNote);
256 }
257 }
258 dataUpdated();
259 }
260
261 /**
262 * Returns the current comparator being used to sort the note list.
263 * @return The current comparator being used to sort the note list
264 */
265 public Comparator<Note> getCurrentSortMethod() {
266 return comparator;
267 }
268
269 /** Set the comparator to be used to sort the note list. Several are available
270 * as public static members of this class.
271 * @param comparator - The Note comparator to sort by
272 */
273 public void setSortMethod(Comparator<Note> comparator) {
274 this.comparator = comparator;
275 dataUpdated();
276 }
277
278 /**
279 * Adds a listener that listens to node data changes
280 * @param listener The listener
281 */
282 public void addNoteDataUpdateListener(NoteDataUpdateListener listener) {
283 listeners.addListener(listener);
284 }
285
286 /**
287 * Removes a listener that listens to node data changes
288 * @param listener The listener
289 */
290 public void removeNoteDataUpdateListener(NoteDataUpdateListener listener) {
291 listeners.removeListener(listener);
292 }
293}
Note: See TracBrowser for help on using the repository browser.