Ignore:
Timestamp:
2015-04-18T22:37:12+02:00 (9 years ago)
Author:
simon04
Message:

see #10980 - Note download: update already downloaded non-modified notes; use Storage for internal storage

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/NoteData.java

    r7937 r8224  
    33
    44import java.util.ArrayList;
     5import java.util.Collection;
    56import java.util.Collections;
    67import java.util.Comparator;
    78import java.util.Date;
    8 import java.util.List;
    99import java.util.Map;
    1010
     
    1515import org.openstreetmap.josm.data.notes.NoteComment;
    1616import org.openstreetmap.josm.gui.JosmUserIdentityManager;
     17import org.openstreetmap.josm.tools.Predicate;
     18import org.openstreetmap.josm.tools.Utils;
    1719
    1820/**
     
    2325    private long newNoteId = -1;
    2426
    25     private final List<Note> noteList;
     27    private final Storage<Note> noteList;
    2628    private Note selectedNote = null;
    2729    private Comparator<Note> comparator = DEFAULT_COMPARATOR;
     
    8587
    8688    /**
    87      * Construct a new note container with an empty note list
    88      */
    89     public NoteData() {
    90         noteList = new ArrayList<>();
    91     }
    92 
    93     /**
    9489     * Construct a new note container with a given list of notes
    9590     * @param notes The list of notes to populate the container with
    9691     */
    97     public NoteData(List<Note> notes) {
    98         noteList = notes;
    99         Collections.sort(notes, comparator);
     92    public NoteData(Collection<Note> notes) {
     93        noteList = new Storage<>();
    10094        for (Note note : notes) {
     95            noteList.add(note);
    10196            if (note.getId() <= newNoteId) {
    10297                newNoteId = note.getId() - 1;
     
    107102    /**
    108103     * Returns the notes stored in this layer
    109      * @return List of Note objects
    110      */
    111     public List<Note> getNotes() {
    112         return noteList;
     104     * @return collection of notes
     105     */
     106    public Collection<Note> getNotes() {
     107        return Collections.unmodifiableCollection(noteList);
     108    }
     109
     110    /**
     111     * Returns the notes stored in this layer sorted according to {@link #comparator}
     112     * @return sorted collection of notes
     113     */
     114    public Collection<Note> getSortedNotes() {
     115        final ArrayList<Note> list = new ArrayList<>(noteList);
     116        Collections.sort(list, comparator);
     117        return list;
    113118    }
    114119
     
    155160     * @param newNotes A list of notes to add
    156161     */
    157     public synchronized void addNotes(List<Note> newNotes) {
     162    public synchronized void addNotes(Collection<Note> newNotes) {
    158163        for (Note newNote : newNotes) {
    159164            if (!noteList.contains(newNote)) {
    160165                noteList.add(newNote);
     166            } else {
     167                final Note existingNote = noteList.get(newNote);
     168                final boolean isDirty = Utils.exists(existingNote.getComments(), new Predicate<NoteComment>() {
     169                    @Override
     170                    public boolean evaluate(NoteComment object) {
     171                        return object.getIsNew();
     172                    }
     173                });
     174                if (!isDirty) {
     175                    noteList.put(newNote);
     176                } else {
     177                    // TODO merge comments?
     178                    Main.info("Keeping existing note id={0} with uncommitted changes", String.valueOf(newNote.getId()));
     179                }
    161180            }
    162181            if (newNote.getId() <= newNoteId) {
     
    165184        }
    166185        dataUpdated();
    167         if (Main.isDebugEnabled()) {
    168             Main.debug("notes in current set: " + noteList.size());
    169         }
    170186    }
    171187
     
    256272
    257273    private void dataUpdated() {
    258         Collections.sort(noteList, comparator);
    259         Main.map.noteDialog.setNoteList(noteList);
     274        Main.map.noteDialog.setNotes(getSortedNotes());
    260275        Main.map.mapView.repaint();
    261276    }
Note: See TracChangeset for help on using the changeset viewer.