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

Last change on this file since 12279 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
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 = Note.DEFAULT_COMPARATOR;
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 */
34 public NoteData(Collection<Note> notes) {
35 noteList = new Storage<>();
36 if (notes != null) {
37 for (Note note : notes) {
38 noteList.add(note);
39 if (note.getId() <= newNoteId) {
40 newNoteId = note.getId() - 1;
41 }
42 }
43 }
44 }
45
46 /**
47 * Returns the notes stored in this layer
48 * @return collection of notes
49 */
50 public Collection<Note> getNotes() {
51 return Collections.unmodifiableCollection(noteList);
52 }
53
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() {
59 final List<Note> list = new ArrayList<>(noteList);
60 list.sort(comparator);
61 return list;
62 }
63
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;
77 if (Main.map != null) {
78 Main.map.noteDialog.selectionChanged();
79 Main.map.mapView.repaint();
80 }
81 }
82
83 /**
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()) {
94 if (comment.isNew()) {
95 return true;
96 }
97 }
98 }
99 return false;
100 }
101
102 /**
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 */
106 public synchronized void addNotes(Collection<Note> newNotes) {
107 for (Note newNote : newNotes) {
108 if (!noteList.contains(newNote)) {
109 noteList.add(newNote);
110 } else {
111 final Note existingNote = noteList.get(newNote);
112 final boolean isDirty = existingNote.getComments().stream().anyMatch(NoteComment::isNew);
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 }
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 */
132 public synchronized void createNote(LatLon location, String text) {
133 if (text == null || text.isEmpty()) {
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());
138 note.setState(State.OPEN);
139 note.setId(newNoteId--);
140 NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.OPENED, true);
141 note.addComment(comment);
142 if (Main.isDebugEnabled()) {
143 Main.debug("Created note {0} with comment: {1}", note.getId(), text);
144 }
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 */
154 public synchronized void addCommentToNote(Note note, String text) {
155 if (!noteList.contains(note)) {
156 throw new IllegalArgumentException("Note to modify must be in layer");
157 }
158 if (note.getState() == State.CLOSED) {
159 throw new IllegalStateException("Cannot add a comment to a closed note");
160 }
161 if (Main.isDebugEnabled()) {
162 Main.debug("Adding comment to note {0}: {1}", note.getId(), text);
163 }
164 NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.COMMENTED, true);
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 */
174 public synchronized void closeNote(Note note, String text) {
175 if (!noteList.contains(note)) {
176 throw new IllegalArgumentException("Note to close must be in layer");
177 }
178 if (note.getState() != State.OPEN) {
179 throw new IllegalStateException("Cannot close a note that isn't open");
180 }
181 if (Main.isDebugEnabled()) {
182 Main.debug("closing note {0} with comment: {1}", note.getId(), text);
183 }
184 NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.CLOSED, true);
185 note.addComment(comment);
186 note.setState(State.CLOSED);
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 */
196 public synchronized void reOpenNote(Note note, String text) {
197 if (!noteList.contains(note)) {
198 throw new IllegalArgumentException("Note to reopen must be in layer");
199 }
200 if (note.getState() != State.CLOSED) {
201 throw new IllegalStateException("Cannot reopen a note that isn't closed");
202 }
203 if (Main.isDebugEnabled()) {
204 Main.debug("reopening note {0} with comment: {1}", note.getId(), text);
205 }
206 NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.REOPENED, true);
207 note.addComment(comment);
208 note.setState(State.OPEN);
209 dataUpdated();
210 }
211
212 private void dataUpdated() {
213 if (Main.isDisplayingMapView()) {
214 Main.map.noteDialog.setNotes(getSortedNotes());
215 Main.map.mapView.repaint();
216 }
217 }
218
219 private static User getCurrentUser() {
220 JosmUserIdentityManager userMgr = JosmUserIdentityManager.getInstance();
221 return User.createOsmUser(userMgr.getUserId(), userMgr.getUserName());
222 }
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();
233 boolean reindex = oldNote.hashCode() != newNote.hashCode();
234 if (reindex) {
235 noteList.removeElem(oldNote);
236 }
237 oldNote.updateWith(newNote);
238 if (reindex) {
239 noteList.add(oldNote);
240 }
241 }
242 dataUpdated();
243 }
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 }
258}
Note: See TracBrowser for help on using the repository browser.