Ticket #10826: notes_sort.patch

File notes_sort.patch, 10.6 KB (added by ToeBee, 11 years ago)
  • src/org/openstreetmap/josm/data/osm/NoteData.java

     
    22package org.openstreetmap.josm.data.osm;
    33
    44import java.util.ArrayList;
     5import java.util.Collections;
     6import java.util.Comparator;
    57import java.util.Date;
    68import java.util.List;
    79import java.util.Map;
     
    2224
    2325    private final List<Note> noteList;
    2426    private Note selectedNote = null;
     27    private Comparator<Note> comparator = DEFAULT_COMPARATOR;
    2528
    2629    /**
     30     * Sorts notes in the following order:
     31     * 1) Open notes
     32     * 2) Closed notes
     33     * 3) New notes
     34     * Within each subgroup it sorts by ID
     35     */
     36    public static final Comparator<Note> DEFAULT_COMPARATOR = new Comparator<Note>() {
     37        @Override
     38        public int compare(Note n1, Note 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.valueOf(Math.abs(n1.getId())).compareTo(Long.valueOf(Math.abs(n2.getId())));
     52        }
     53    };
     54
     55    /** Sorts notes strictly by creation date */
     56    public static final Comparator<Note> DATE_COMPARATOR = new Comparator<Note>() {
     57        @Override
     58        public int compare(Note n1, Note n2) {
     59            return n1.getCreatedAt().compareTo(n2.getCreatedAt());
     60        }
     61    };
     62
     63    /** Sorts notes by user, then creation date */
     64    public static final Comparator<Note> USER_COMPARATOR = new Comparator<Note>() {
     65        @Override
     66        public int compare(Note n1, Note n2) {
     67            String n1User = n1.getFirstComment().getUser().getName();
     68            String n2User = n2.getFirstComment().getUser().getName();
     69            if (n1User.equals(n2User)) {
     70                return n1.getCreatedAt().compareTo(n2.getCreatedAt());
     71            }
     72            return n1.getFirstComment().getUser().getName().compareTo(n2.getFirstComment().getUser().getName());
     73        }
     74    };
     75
     76    /** Sorts notes by the last modified date */
     77    public static final Comparator<Note> LAST_ACTION_COMPARATOR = new Comparator<Note>() {
     78        @Override
     79        public int compare(Note n1, Note n2) {
     80            Date n1Date = n1.getComments().get(n1.getComments().size()-1).getCommentTimestamp();
     81            Date n2Date = n2.getComments().get(n2.getComments().size()-1).getCommentTimestamp();
     82            return n1Date.compareTo(n2Date);
     83        }
     84    };
     85
     86    /**
    2787     * Construct a new note container with an empty note list
    2888     */
    2989    public NoteData() {
     
    3696     */
    3797    public NoteData(List<Note> notes) {
    3898        noteList = notes;
     99        Collections.sort(notes, comparator);
    39100        for (Note note : notes) {
    40101            if (note.getId() <= newNoteId) {
    41102                newNoteId = note.getId() - 1;
     
    194255    }
    195256
    196257    private void dataUpdated() {
     258        Collections.sort(noteList, comparator);
    197259        Main.map.noteDialog.setNoteList(noteList);
    198260        Main.map.mapView.repaint();
    199261    }
     
    216278        }
    217279        dataUpdated();
    218280    }
     281
     282    /** @return The current comparator being used to sort the note list */
     283    public Comparator<Note> getCurrentSortMethod() {
     284        return comparator;
     285    }
     286
     287    /** Set the comparator to be used to sort the note list. Several are available
     288     * as public static members of this class.
     289     * @param comparator - The Note comparator to sort by
     290     */
     291    public void setSortMethod(Comparator<Note> comparator) {
     292        this.comparator = comparator;
     293        dataUpdated();
     294    }
    219295}
  • src/org/openstreetmap/josm/gui/NoteSortDialog.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.gui;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import java.awt.Component;
     7import java.util.Comparator;
     8
     9import javax.swing.BoxLayout;
     10import javax.swing.ButtonGroup;
     11import javax.swing.JLabel;
     12import javax.swing.JPanel;
     13import javax.swing.JRadioButton;
     14
     15import org.openstreetmap.josm.Main;
     16import org.openstreetmap.josm.data.notes.Note;
     17import org.openstreetmap.josm.data.osm.NoteData;
     18
     19/**
     20 * A dialog to allow the user to choose a sorting method for the list of notes
     21 */
     22public class NoteSortDialog extends ExtendedDialog {
     23
     24    private JRadioButton defaultSort = new JRadioButton(tr("Default (open, closed, new)"));
     25    private JRadioButton userSort = new JRadioButton(tr("Username"));
     26    private JRadioButton dateSort = new JRadioButton(tr("Created date"));
     27    private JRadioButton lastActionSort = new JRadioButton(tr("Last change date"));
     28
     29    /**
     30     * Construct a new dialog. The constructor automatically adds a "Cancel" button.
     31     * @param parent - Parent component. Usually Main.parent
     32     * @param title - Translated text to display in the title bar of the dialog
     33     * @param buttonText - Translated text to be shown on the action button
     34     */
     35    public NoteSortDialog(Component parent, String title, String buttonText) {
     36        super(parent, title, new String[] {buttonText, tr("Cancel")});
     37    }
     38
     39    /**
     40     * Builds and displays the window to the user.
     41     * @param currentSortMode - The current sort mode which will be pre-selected in the list
     42     */
     43    public void showSortDialog(Comparator<Note> currentSortMode) {
     44        JLabel label = new JLabel(tr("Select note sorting method"));
     45        if (currentSortMode == NoteData.DEFAULT_COMPARATOR) {
     46            defaultSort.setSelected(true);
     47        } else if (currentSortMode == NoteData.DATE_COMPARATOR) {
     48            dateSort.setSelected(true);
     49        } else if (currentSortMode == NoteData.USER_COMPARATOR) {
     50            userSort.setSelected(true);
     51        } else if (currentSortMode == NoteData.LAST_ACTION_COMPARATOR) {
     52            lastActionSort.setSelected(true);
     53        } else {
     54            Main.warn("sort mode not recognized");
     55        }
     56
     57        ButtonGroup bg = new ButtonGroup();
     58        bg.add(defaultSort);
     59        bg.add(userSort);
     60        bg.add(dateSort);
     61        bg.add(lastActionSort);
     62
     63        JPanel panel = new JPanel();
     64        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
     65        panel.add(label);
     66        panel.add(defaultSort);
     67        panel.add(userSort);
     68        panel.add(dateSort);
     69        panel.add(lastActionSort);
     70
     71        setContent(panel);
     72
     73        showDialog();
     74    }
     75
     76    /** @return Note comparator that the user has selected */
     77    public Comparator<Note> getSelectedComparator() {
     78        if (dateSort.isSelected()) {
     79            return NoteData.DATE_COMPARATOR;
     80        } else if (userSort.isSelected()) {
     81            return NoteData.USER_COMPARATOR;
     82        } else if (lastActionSort.isSelected()) {
     83            return NoteData.LAST_ACTION_COMPARATOR;
     84        } else {
     85            return NoteData.DEFAULT_COMPARATOR;
     86        }
     87    }
     88}
  • src/org/openstreetmap/josm/gui/dialogs/NoteDialog.java

     
    3535import org.openstreetmap.josm.gui.MapView;
    3636import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
    3737import org.openstreetmap.josm.gui.NoteInputDialog;
     38import org.openstreetmap.josm.gui.NoteSortDialog;
    3839import org.openstreetmap.josm.gui.SideButton;
    3940import org.openstreetmap.josm.gui.layer.Layer;
    4041import org.openstreetmap.josm.gui.layer.NoteLayer;
     
    7475    private final CloseAction closeAction;
    7576    private final NewAction newAction;
    7677    private final ReopenAction reopenAction;
     78    private final SortAction sortAction;
    7779    private final UploadNotesAction uploadAction;
    7880
    7981    private NoteData noteData;
     
    8991        closeAction = new CloseAction();
    9092        newAction = new NewAction();
    9193        reopenAction = new ReopenAction();
     94        sortAction = new SortAction();
    9295        uploadAction = new UploadNotesAction();
    9396        buildDialog();
    9497    }
     
    120123                new SideButton(addCommentAction, false),
    121124                new SideButton(closeAction, false),
    122125                new SideButton(reopenAction, false),
     126                new SideButton(sortAction, false),
    123127                new SideButton(uploadAction, false)}));
    124128        updateButtonStates();
    125129    }
     
    143147        } else {
    144148            uploadAction.setEnabled(true);
    145149        }
     150        //enable sort button if any notes are loaded
     151        if (noteData == null || noteData.getNotes().size() == 0) {
     152            sortAction.setEnabled(false);
     153        } else {
     154            sortAction.setEnabled(true);
     155        }
    146156    }
    147157
    148158    @Override
     
    160170
    161171    @Override
    162172    public void layerAdded(Layer newLayer) {
    163         if (Main.isDebugEnabled()) {
    164             Main.debug("layer added: " + newLayer);
    165         }
    166173        if (newLayer instanceof NoteLayer) {
    167             if (Main.isDebugEnabled()) {
    168                 Main.debug("note layer added");
    169             }
     174            Main.debug("note layer added");
    170175            noteData = ((NoteLayer)newLayer).getNoteData();
    171176            model.setData(noteData.getNotes());
     177            setNoteList(noteData.getNotes());
    172178        }
    173179    }
    174180
     
    363369            noteData.reOpenNote(note, dialog.getInputText());
    364370        }
    365371    }
     372
     373    class SortAction extends AbstractAction {
     374
     375        public SortAction() {
     376            putValue(SHORT_DESCRIPTION, tr("Sort notes"));
     377            putValue(NAME, tr("Sort"));
     378            putValue(SMALL_ICON, ImageProvider.get("dialogs", "sort"));
     379        }
     380
     381        @Override
     382        public void actionPerformed(ActionEvent e) {
     383            NoteSortDialog sortDialog = new NoteSortDialog(Main.parent, tr("Sort notes"), tr("Apply"));
     384            sortDialog.showSortDialog(noteData.getCurrentSortMethod());
     385            if (sortDialog.getValue() == 1) {
     386                noteData.setSortMethod(sortDialog.getSelectedComparator());
     387            }
     388        }
     389    }
    366390}