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

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

findbugs - EI_EXPOSE_REP2 - Note dates

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