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