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

Last change on this file since 11296 was 10717, checked in by simon04, 8 years ago

see #11390, see #12890 - Lambda can be replaced with method reference

  • Property svn:eol-style set to native
File size: 10.7 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;
18
19/**
20 * Class to hold and perform operations on a set of notes
21 */
22public class NoteData {
23
24 private long newNoteId = -1;
25
26 private final Storage<Note> noteList;
27 private Note selectedNote;
28 private Comparator<Note> comparator = DEFAULT_COMPARATOR;
29
30 /**
31 * Sorts notes in the following order:
32 * 1) Open notes
33 * 2) Closed notes
34 * 3) New notes
35 * Within each subgroup it sorts by ID
36 */
37 public static final Comparator<Note> DEFAULT_COMPARATOR = (n1, n2) -> {
38 if (n1.getId() < 0 && n2.getId() > 0) {
39 return 1;
40 }
41 if (n1.getId() > 0 && n2.getId() < 0) {
42 return -1;
43 }
44 if (n1.getState() == State.CLOSED && n2.getState() == State.OPEN) {
45 return 1;
46 }
47 if (n1.getState() == State.OPEN && n2.getState() == State.CLOSED) {
48 return -1;
49 }
50 return Long.compare(Math.abs(n1.getId()), Math.abs(n2.getId()));
51 };
52
53 /** Sorts notes strictly by creation date */
54 public static final Comparator<Note> DATE_COMPARATOR = (n1, n2) -> n1.getCreatedAt().compareTo(n2.getCreatedAt());
55
56 /** Sorts notes by user, then creation date */
57 public static final Comparator<Note> USER_COMPARATOR = (n1, n2) -> {
58 String n1User = n1.getFirstComment().getUser().getName();
59 String n2User = n2.getFirstComment().getUser().getName();
60 if (n1User.equals(n2User)) {
61 return n1.getCreatedAt().compareTo(n2.getCreatedAt());
62 }
63 return n1.getFirstComment().getUser().getName().compareTo(n2.getFirstComment().getUser().getName());
64 };
65
66 /** Sorts notes by the last modified date */
67 public static final Comparator<Note> LAST_ACTION_COMPARATOR = (n1, n2) -> {
68 Date n1Date = n1.getComments().get(n1.getComments().size()-1).getCommentTimestamp();
69 Date n2Date = n2.getComments().get(n2.getComments().size()-1).getCommentTimestamp();
70 return n1Date.compareTo(n2Date);
71 };
72
73 /**
74 * Construct a new note container with a given list of notes
75 * @param notes The list of notes to populate the container with
76 */
77 public NoteData(Collection<Note> notes) {
78 noteList = new Storage<>();
79 if (notes != null) {
80 for (Note note : notes) {
81 noteList.add(note);
82 if (note.getId() <= newNoteId) {
83 newNoteId = note.getId() - 1;
84 }
85 }
86 }
87 }
88
89 /**
90 * Returns the notes stored in this layer
91 * @return collection of notes
92 */
93 public Collection<Note> getNotes() {
94 return Collections.unmodifiableCollection(noteList);
95 }
96
97 /**
98 * Returns the notes stored in this layer sorted according to {@link #comparator}
99 * @return sorted collection of notes
100 */
101 public Collection<Note> getSortedNotes() {
102 final List<Note> list = new ArrayList<>(noteList);
103 list.sort(comparator);
104 return list;
105 }
106
107 /** Returns the currently selected note
108 * @return currently selected note
109 */
110 public Note getSelectedNote() {
111 return selectedNote;
112 }
113
114 /** Set a selected note. Causes the dialog to select the note and
115 * the note layer to draw the selected note's comments.
116 * @param note Selected note. Null indicates no selection
117 */
118 public void setSelectedNote(Note note) {
119 selectedNote = note;
120 if (Main.map != null) {
121 Main.map.noteDialog.selectionChanged();
122 Main.map.mapView.repaint();
123 }
124 }
125
126 /**
127 * Return whether or not there are any changes in the note data set.
128 * These changes may need to be either uploaded or saved.
129 * @return true if local modifications have been made to the note data set. False otherwise.
130 */
131 public synchronized boolean isModified() {
132 for (Note note : noteList) {
133 if (note.getId() < 0) { //notes with negative IDs are new
134 return true;
135 }
136 for (NoteComment comment : note.getComments()) {
137 if (comment.isNew()) {
138 return true;
139 }
140 }
141 }
142 return false;
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 Main.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 (text == null || text.isEmpty()) {
177 throw new IllegalArgumentException("Comment can not be blank when creating a note");
178 }
179 Note note = new Note(location);
180 note.setCreatedAt(new Date());
181 note.setState(State.OPEN);
182 note.setId(newNoteId--);
183 NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.OPENED, true);
184 note.addComment(comment);
185 if (Main.isDebugEnabled()) {
186 Main.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 (Main.isDebugEnabled()) {
205 Main.debug("Adding comment to note {0}: {1}", note.getId(), text);
206 }
207 NoteComment comment = new NoteComment(new Date(), 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 (Main.isDebugEnabled()) {
225 Main.debug("closing note {0} with comment: {1}", note.getId(), text);
226 }
227 NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.CLOSED, true);
228 note.addComment(comment);
229 note.setState(State.CLOSED);
230 note.setClosedAt(new Date());
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 if (Main.isDebugEnabled()) {
247 Main.debug("reopening note {0} with comment: {1}", note.getId(), text);
248 }
249 NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.REOPENED, true);
250 note.addComment(comment);
251 note.setState(State.OPEN);
252 dataUpdated();
253 }
254
255 private void dataUpdated() {
256 if (Main.isDisplayingMapView()) {
257 Main.map.noteDialog.setNotes(getSortedNotes());
258 Main.map.mapView.repaint();
259 }
260 }
261
262 private static User getCurrentUser() {
263 JosmUserIdentityManager userMgr = JosmUserIdentityManager.getInstance();
264 return User.createOsmUser(userMgr.getUserId(), userMgr.getUserName());
265 }
266
267 /**
268 * Updates notes with new state. Primarily to be used when updating the
269 * note layer after uploading note changes to the server.
270 * @param updatedNotes Map containing the original note as the key and the updated note as the value
271 */
272 public synchronized void updateNotes(Map<Note, Note> updatedNotes) {
273 for (Map.Entry<Note, Note> entry : updatedNotes.entrySet()) {
274 Note oldNote = entry.getKey();
275 Note newNote = entry.getValue();
276 boolean reindex = oldNote.hashCode() != newNote.hashCode();
277 if (reindex) {
278 noteList.removeElem(oldNote);
279 }
280 oldNote.updateWith(newNote);
281 if (reindex) {
282 noteList.add(oldNote);
283 }
284 }
285 dataUpdated();
286 }
287
288 /** @return The current comparator being used to sort the note list */
289 public Comparator<Note> getCurrentSortMethod() {
290 return comparator;
291 }
292
293 /** Set the comparator to be used to sort the note list. Several are available
294 * as public static members of this class.
295 * @param comparator - The Note comparator to sort by
296 */
297 public void setSortMethod(Comparator<Note> comparator) {
298 this.comparator = comparator;
299 dataUpdated();
300 }
301}
Note: See TracBrowser for help on using the repository browser.