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