001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Component;
007import java.util.Comparator;
008
009import javax.swing.BoxLayout;
010import javax.swing.ButtonGroup;
011import javax.swing.JLabel;
012import javax.swing.JPanel;
013import javax.swing.JRadioButton;
014
015import org.openstreetmap.josm.data.notes.Note;
016import org.openstreetmap.josm.tools.Logging;
017
018/**
019 * A dialog to allow the user to choose a sorting method for the list of notes
020 */
021public class NoteSortDialog extends ExtendedDialog {
022
023    private final JRadioButton defaultSort = new JRadioButton(tr("Default (open, closed, new)"));
024    private final JRadioButton userSort = new JRadioButton(tr("Username"));
025    private final JRadioButton dateSort = new JRadioButton(tr("Created date"));
026    private final JRadioButton lastActionSort = new JRadioButton(tr("Last change date"));
027
028    /**
029     * Construct a new dialog. The constructor automatically adds a "Cancel" button.
030     * @param parent - Parent component. Usually Main.parent
031     * @param title - Translated text to display in the title bar of the dialog
032     * @param buttonText - Translated text to be shown on the action button
033     */
034    public NoteSortDialog(Component parent, String title, String buttonText) {
035        super(parent, title, buttonText, tr("Cancel"));
036    }
037
038    /**
039     * Builds and displays the window to the user.
040     * @param currentSortMode - The current sort mode which will be pre-selected in the list
041     */
042    public void showSortDialog(Comparator<Note> currentSortMode) {
043        JLabel label = new JLabel(tr("Select note sorting method"));
044        if (currentSortMode == Note.DEFAULT_COMPARATOR) {
045            defaultSort.setSelected(true);
046        } else if (currentSortMode == Note.DATE_COMPARATOR) {
047            dateSort.setSelected(true);
048        } else if (currentSortMode == Note.USER_COMPARATOR) {
049            userSort.setSelected(true);
050        } else if (currentSortMode == Note.LAST_ACTION_COMPARATOR) {
051            lastActionSort.setSelected(true);
052        } else {
053            Logging.warn("sort mode not recognized");
054        }
055
056        ButtonGroup bg = new ButtonGroup();
057        bg.add(defaultSort);
058        bg.add(userSort);
059        bg.add(dateSort);
060        bg.add(lastActionSort);
061
062        JPanel panel = new JPanel();
063        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
064        panel.add(label);
065        panel.add(defaultSort);
066        panel.add(userSort);
067        panel.add(dateSort);
068        panel.add(lastActionSort);
069
070        setContent(panel);
071
072        showDialog();
073    }
074
075    /**
076     * Returns the Note comparator that the user has selected.
077     * @return Note comparator that the user has selected
078     */
079    public Comparator<Note> getSelectedComparator() {
080        if (dateSort.isSelected()) {
081            return Note.DATE_COMPARATOR;
082        } else if (userSort.isSelected()) {
083            return Note.USER_COMPARATOR;
084        } else if (lastActionSort.isSelected()) {
085            return Note.LAST_ACTION_COMPARATOR;
086        } else {
087            return Note.DEFAULT_COMPARATOR;
088        }
089    }
090}