| 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 |
|
|---|
| 8 | import org.openstreetmap.josm.Main;
|
|---|
| 9 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 10 | import org.openstreetmap.josm.data.notes.Note;
|
|---|
| 11 | import org.openstreetmap.josm.data.notes.Note.State;
|
|---|
| 12 | import org.openstreetmap.josm.data.notes.NoteComment;
|
|---|
| 13 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Class to hold and perform operations on a set of notes
|
|---|
| 17 | */
|
|---|
| 18 | public class NoteData {
|
|---|
| 19 |
|
|---|
| 20 | private long newNoteId = -1;
|
|---|
| 21 |
|
|---|
| 22 | private final List<Note> noteList;
|
|---|
| 23 | private Note selectedNote = null;
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * Construct a new note container with an empty note list
|
|---|
| 27 | */
|
|---|
| 28 | public NoteData() {
|
|---|
| 29 | noteList = new ArrayList<>();
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Construct a new note container with a given list of notes
|
|---|
| 34 | * @param notes The list of notes to populate the container with
|
|---|
| 35 | */
|
|---|
| 36 | public NoteData(List<Note> notes) {
|
|---|
| 37 | noteList = notes;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Returns the notes stored in this layer
|
|---|
| 42 | * @return List of Note objects
|
|---|
| 43 | */
|
|---|
| 44 | public List<Note> getNotes() {
|
|---|
| 45 | return noteList;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /** Returns the currently selected note
|
|---|
| 49 | * @return currently selected note
|
|---|
| 50 | */
|
|---|
| 51 | public Note getSelectedNote() {
|
|---|
| 52 | return selectedNote;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /** Set a selected note. Causes the dialog to select the note and
|
|---|
| 56 | * the note layer to draw the selected note's comments.
|
|---|
| 57 | * @param note Selected note. Null indicates no selection
|
|---|
| 58 | */
|
|---|
| 59 | public void setSelectedNote(Note note) {
|
|---|
| 60 | selectedNote = note;
|
|---|
| 61 | Main.map.noteDialog.selectionChanged();
|
|---|
| 62 | Main.map.mapView.repaint();
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Add notes to the data set. It only adds a note if the ID is not already present
|
|---|
| 67 | * @param newNotes A list of notes to add
|
|---|
| 68 | */
|
|---|
| 69 | public void addNotes(List<Note> newNotes) {
|
|---|
| 70 | for (Note newNote : newNotes) {
|
|---|
| 71 | if (!noteList.contains(newNote)) {
|
|---|
| 72 | noteList.add(newNote);
|
|---|
| 73 | }
|
|---|
| 74 | if (newNote.getId() <= newNoteId) {
|
|---|
| 75 | newNoteId = newNote.getId() - 1;
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | dataUpdated();
|
|---|
| 79 | Main.debug("notes in current set: " + noteList.size());
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Create a new note
|
|---|
| 84 | * @param location Location of note
|
|---|
| 85 | * @param text Required comment with which to open the note
|
|---|
| 86 | */
|
|---|
| 87 | public void createNote(LatLon location, String text) {
|
|---|
| 88 | if(text == null || text.isEmpty()) {
|
|---|
| 89 | throw new IllegalArgumentException("Comment can not be blank when creating a note");
|
|---|
| 90 | }
|
|---|
| 91 | Note note = new Note(location);
|
|---|
| 92 | note.setCreatedAt(new Date());
|
|---|
| 93 | note.setState(State.open);
|
|---|
| 94 | note.setId(newNoteId--);
|
|---|
| 95 | NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.opened, true);
|
|---|
| 96 | note.addComment(comment);
|
|---|
| 97 | Main.debug("Created note {0} with comment: {1}", note.getId(), text);
|
|---|
| 98 | noteList.add(note);
|
|---|
| 99 | dataUpdated();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /**
|
|---|
| 103 | * Add a new comment to an existing note
|
|---|
| 104 | * @param note Note to add comment to. Must already exist in the layer
|
|---|
| 105 | * @param text Comment to add
|
|---|
| 106 | */
|
|---|
| 107 | public void addCommentToNote(Note note, String text) {
|
|---|
| 108 | if (!noteList.contains(note)) {
|
|---|
| 109 | throw new IllegalArgumentException("Note to modify must be in layer");
|
|---|
| 110 | }
|
|---|
| 111 | if (note.getState() == State.closed) {
|
|---|
| 112 | throw new IllegalStateException("Cannot add a comment to a closed note");
|
|---|
| 113 | }
|
|---|
| 114 | Main.debug("Adding comment to note {0}: {1}", note.getId(), text);
|
|---|
| 115 | NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.commented, true);
|
|---|
| 116 | note.addComment(comment);
|
|---|
| 117 | dataUpdated();
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /**
|
|---|
| 121 | * Close note with comment
|
|---|
| 122 | * @param note Note to close. Must already exist in the layer
|
|---|
| 123 | * @param text Comment to attach to close action, if desired
|
|---|
| 124 | */
|
|---|
| 125 | public void closeNote(Note note, String text) {
|
|---|
| 126 | if (!noteList.contains(note)) {
|
|---|
| 127 | throw new IllegalArgumentException("Note to close must be in layer");
|
|---|
| 128 | }
|
|---|
| 129 | if (note.getState() != State.open) {
|
|---|
| 130 | throw new IllegalStateException("Cannot close a note that isn't open");
|
|---|
| 131 | }
|
|---|
| 132 | Main.debug("closing note {0} with comment: {1}", note.getId(), text);
|
|---|
| 133 | NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.closed, true);
|
|---|
| 134 | note.addComment(comment);
|
|---|
| 135 | note.setState(State.closed);
|
|---|
| 136 | note.setClosedAt(new Date());
|
|---|
| 137 | dataUpdated();
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /**
|
|---|
| 141 | * Reopen a closed note.
|
|---|
| 142 | * @param note Note to reopen. Must already exist in the layer
|
|---|
| 143 | * @param text Comment to attach to the reopen action, if desired
|
|---|
| 144 | */
|
|---|
| 145 | public void reOpenNote(Note note, String text) {
|
|---|
| 146 | if (!noteList.contains(note)) {
|
|---|
| 147 | throw new IllegalArgumentException("Note to reopen must be in layer");
|
|---|
| 148 | }
|
|---|
| 149 | if (note.getState() != State.closed) {
|
|---|
| 150 | throw new IllegalStateException("Cannot reopen a note that isn't closed");
|
|---|
| 151 | }
|
|---|
| 152 | Main.debug("reopening note {0} with comment: {1}", note.getId(), text);
|
|---|
| 153 | NoteComment comment = new NoteComment(new Date(), getCurrentUser(), text, NoteComment.Action.reopened, true);
|
|---|
| 154 | note.addComment(comment);
|
|---|
| 155 | note.setState(State.open);
|
|---|
| 156 | dataUpdated();
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | private void dataUpdated() {
|
|---|
| 160 | Main.map.noteDialog.setNoteList(noteList);
|
|---|
| 161 | Main.map.mapView.repaint();
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | private User getCurrentUser() {
|
|---|
| 165 | JosmUserIdentityManager userMgr = JosmUserIdentityManager.getInstance();
|
|---|
| 166 | return User.createOsmUser(userMgr.getUserId(), userMgr.getUserName());
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|