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

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

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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