| 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.Date;
|
|---|
| 6 | import java.util.List;
|
|---|
| 7 | import java.util.Map;
|
|---|
| 8 |
|
|---|
| 9 | import org.openstreetmap.josm.Main;
|
|---|
| 10 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 11 | import org.openstreetmap.josm.data.notes.Note;
|
|---|
| 12 | import org.openstreetmap.josm.data.notes.Note.State;
|
|---|
| 13 | import org.openstreetmap.josm.data.notes.NoteComment;
|
|---|
| 14 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * Class to hold and perform operations on a set of notes
|
|---|
| 18 | */
|
|---|
| 19 | public class NoteData {
|
|---|
| 20 |
|
|---|
| 21 | private long newNoteId = -1;
|
|---|
| 22 |
|
|---|
| 23 | private final List<Note> noteList;
|
|---|
| 24 | private Note selectedNote = null;
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Construct a new note container with an empty note list
|
|---|
| 28 | */
|
|---|
| 29 | public NoteData() {
|
|---|
| 30 | noteList = new ArrayList<>();
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Construct a new note container with a given list of notes
|
|---|
| 35 | * @param notes The list of notes to populate the container with
|
|---|
| 36 | */
|
|---|
| 37 | public NoteData(List<Note> notes) {
|
|---|
| 38 | noteList = notes;
|
|---|
| 39 | for (Note note : notes) {
|
|---|
| 40 | if (note.getId() <= newNoteId) {
|
|---|
| 41 | newNoteId = note.getId() - 1;
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Returns the notes stored in this layer
|
|---|
| 48 | * @return List of Note objects
|
|---|
| 49 | */
|
|---|
| 50 | public List<Note> getNotes() {
|
|---|
| 51 | return noteList;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /** Returns the currently selected note
|
|---|
| 55 | * @return currently selected note
|
|---|
| 56 | */
|
|---|
| 57 | public Note getSelectedNote() {
|
|---|
| 58 | return selectedNote;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /** Set a selected note. Causes the dialog to select the note and
|
|---|
| 62 | * the note layer to draw the selected note's comments.
|
|---|
| 63 | * @param note Selected note. Null indicates no selection
|
|---|
| 64 | */
|
|---|
| 65 | public void setSelectedNote(Note note) {
|
|---|
| 66 | selectedNote = note;
|
|---|
| 67 | if (Main.map != null) {
|
|---|
| 68 | Main.map.noteDialog.selectionChanged();
|
|---|
| 69 | Main.map.mapView.repaint();
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Return whether or not there are any changes in the note data set.
|
|---|
| 75 | * These changes may need to be either uploaded or saved.
|
|---|
| 76 | * @return true if local modifications have been made to the note data set. False otherwise.
|
|---|
| 77 | */
|
|---|
| 78 | public synchronized boolean isModified() {
|
|---|
| 79 | for (Note note : noteList) {
|
|---|
| 80 | if (note.getId() < 0) { //notes with negative IDs are new
|
|---|
| 81 | return true;
|
|---|
| 82 | }
|
|---|
| 83 | for (NoteComment comment : note.getComments()) {
|
|---|
| 84 | if (comment.getIsNew()) {
|
|---|
| 85 | return true;
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | return false;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /**
|
|---|
| 93 | * Add notes to the data set. It only adds a note if the ID is not already present
|
|---|
| 94 | * @param newNotes A list of notes to add
|
|---|
| 95 | */
|
|---|
| 96 | public synchronized void addNotes(List<Note> newNotes) {
|
|---|
| 97 | for (Note newNote : newNotes) {
|
|---|
| 98 | if (!noteList.contains(newNote)) {
|
|---|
| 99 | noteList.add(newNote);
|
|---|
| 100 | }
|
|---|
| 101 | if (newNote.getId() <= newNoteId) {
|
|---|
| 102 | newNoteId = newNote.getId() - 1;
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | dataUpdated();
|
|---|
| 106 | if (Main.isDebugEnabled()) {
|
|---|
| 107 | Main.debug("notes in current set: " + noteList.size());
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /**
|
|---|
| 112 | * Create a new note
|
|---|
| 113 | * @param location Location of note
|
|---|
| 114 | * @param text Required comment with which to open the note
|
|---|
| 115 | */
|
|---|
| 116 | public synchronized void createNote(LatLon location, String text) {
|
|---|
| 117 | if(text == null || text.isEmpty()) {
|
|---|
| 118 | throw new IllegalArgumentException("Comment can not be blank when creating a note");
|
|---|
| 119 | }
|
|---|
| 120 | Note note = new Note(location);
|
|---|
| 121 | note.setCreatedAt(new Date());
|
|---|
| 122 | note.setState(State.open);
|
|---|
| 123 | note.setId(newNoteId--);
|
|---|
| 124 | NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.opened, true);
|
|---|
| 125 | note.addComment(comment);
|
|---|
| 126 | if (Main.isDebugEnabled()) {
|
|---|
| 127 | Main.debug("Created note {0} with comment: {1}", note.getId(), text);
|
|---|
| 128 | }
|
|---|
| 129 | noteList.add(note);
|
|---|
| 130 | dataUpdated();
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * Add a new comment to an existing note
|
|---|
| 135 | * @param note Note to add comment to. Must already exist in the layer
|
|---|
| 136 | * @param text Comment to add
|
|---|
| 137 | */
|
|---|
| 138 | public synchronized void addCommentToNote(Note note, String text) {
|
|---|
| 139 | if (!noteList.contains(note)) {
|
|---|
| 140 | throw new IllegalArgumentException("Note to modify must be in layer");
|
|---|
| 141 | }
|
|---|
| 142 | if (note.getState() == State.closed) {
|
|---|
| 143 | throw new IllegalStateException("Cannot add a comment to a closed note");
|
|---|
| 144 | }
|
|---|
| 145 | if (Main.isDebugEnabled()) {
|
|---|
| 146 | Main.debug("Adding comment to note {0}: {1}", note.getId(), text);
|
|---|
| 147 | }
|
|---|
| 148 | NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.commented, true);
|
|---|
| 149 | note.addComment(comment);
|
|---|
| 150 | dataUpdated();
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /**
|
|---|
| 154 | * Close note with comment
|
|---|
| 155 | * @param note Note to close. Must already exist in the layer
|
|---|
| 156 | * @param text Comment to attach to close action, if desired
|
|---|
| 157 | */
|
|---|
| 158 | public synchronized void closeNote(Note note, String text) {
|
|---|
| 159 | if (!noteList.contains(note)) {
|
|---|
| 160 | throw new IllegalArgumentException("Note to close must be in layer");
|
|---|
| 161 | }
|
|---|
| 162 | if (note.getState() != State.open) {
|
|---|
| 163 | throw new IllegalStateException("Cannot close a note that isn't open");
|
|---|
| 164 | }
|
|---|
| 165 | if (Main.isDebugEnabled()) {
|
|---|
| 166 | Main.debug("closing note {0} with comment: {1}", note.getId(), text);
|
|---|
| 167 | }
|
|---|
| 168 | NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.closed, true);
|
|---|
| 169 | note.addComment(comment);
|
|---|
| 170 | note.setState(State.closed);
|
|---|
| 171 | note.setClosedAt(new Date());
|
|---|
| 172 | dataUpdated();
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /**
|
|---|
| 176 | * Reopen a closed note.
|
|---|
| 177 | * @param note Note to reopen. Must already exist in the layer
|
|---|
| 178 | * @param text Comment to attach to the reopen action, if desired
|
|---|
| 179 | */
|
|---|
| 180 | public synchronized void reOpenNote(Note note, String text) {
|
|---|
| 181 | if (!noteList.contains(note)) {
|
|---|
| 182 | throw new IllegalArgumentException("Note to reopen must be in layer");
|
|---|
| 183 | }
|
|---|
| 184 | if (note.getState() != State.closed) {
|
|---|
| 185 | throw new IllegalStateException("Cannot reopen a note that isn't closed");
|
|---|
| 186 | }
|
|---|
| 187 | if (Main.isDebugEnabled()) {
|
|---|
| 188 | Main.debug("reopening note {0} with comment: {1}", note.getId(), text);
|
|---|
| 189 | }
|
|---|
| 190 | NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.reopened, true);
|
|---|
| 191 | note.addComment(comment);
|
|---|
| 192 | note.setState(State.open);
|
|---|
| 193 | dataUpdated();
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | private void dataUpdated() {
|
|---|
| 197 | Main.map.noteDialog.setNoteList(noteList);
|
|---|
| 198 | Main.map.mapView.repaint();
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | private User getCurrentUser() {
|
|---|
| 202 | JosmUserIdentityManager userMgr = JosmUserIdentityManager.getInstance();
|
|---|
| 203 | return User.createOsmUser(userMgr.getUserId(), userMgr.getUserName());
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /**
|
|---|
| 207 | * Updates notes with new state. Primarily to be used when updating the
|
|---|
| 208 | * note layer after uploading note changes to the server.
|
|---|
| 209 | * @param updatedNotes Map containing the original note as the key and the updated note as the value
|
|---|
| 210 | */
|
|---|
| 211 | public synchronized void updateNotes(Map<Note, Note> updatedNotes) {
|
|---|
| 212 | for (Map.Entry<Note, Note> entry : updatedNotes.entrySet()) {
|
|---|
| 213 | Note oldNote = entry.getKey();
|
|---|
| 214 | Note newNote = entry.getValue();
|
|---|
| 215 | oldNote.updateWith(newNote);
|
|---|
| 216 | }
|
|---|
| 217 | dataUpdated();
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|