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