1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.util.Comparator;
|
---|
8 |
|
---|
9 | import javax.swing.BoxLayout;
|
---|
10 | import javax.swing.ButtonGroup;
|
---|
11 | import javax.swing.JLabel;
|
---|
12 | import javax.swing.JPanel;
|
---|
13 | import javax.swing.JRadioButton;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.notes.Note;
|
---|
17 | import 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 | */
|
---|
22 | public class NoteSortDialog extends ExtendedDialog {
|
---|
23 |
|
---|
24 | private final JRadioButton defaultSort = new JRadioButton(tr("Default (open, closed, new)"));
|
---|
25 | private final JRadioButton userSort = new JRadioButton(tr("Username"));
|
---|
26 | private final JRadioButton dateSort = new JRadioButton(tr("Created date"));
|
---|
27 | private final 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 | }
|
---|