| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.preferences.server;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.GridBagLayout;
|
|---|
| 7 |
|
|---|
| 8 | import javax.swing.BorderFactory;
|
|---|
| 9 | import javax.swing.JCheckBox;
|
|---|
| 10 | import javax.swing.JLabel;
|
|---|
| 11 | import javax.swing.JPanel;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
|
|---|
| 14 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
|---|
| 15 | import org.openstreetmap.josm.io.MessageNotifier;
|
|---|
| 16 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Preferences panel for OSM messages notifier.
|
|---|
| 20 | * @since 6349
|
|---|
| 21 | */
|
|---|
| 22 | public class FeaturesPanel extends JPanel {
|
|---|
| 23 |
|
|---|
| 24 | private JCheckBox notifier;
|
|---|
| 25 | private JLabel intervalLabel;
|
|---|
| 26 | private final JosmTextField notifierInterval = new JosmTextField(4);
|
|---|
| 27 | private final JosmTextField notesDaysClosed = new JosmTextField(4);
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Constructs a new {@code MessagesNotifierPanel}.
|
|---|
| 31 | */
|
|---|
| 32 | public FeaturesPanel() {
|
|---|
| 33 | build();
|
|---|
| 34 | initFromPreferences();
|
|---|
| 35 | updateEnabledState();
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | private void build() {
|
|---|
| 39 | setLayout(new GridBagLayout());
|
|---|
| 40 | setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
|---|
| 41 |
|
|---|
| 42 | notifier = new JCheckBox(tr("Periodically check for new messages"));
|
|---|
| 43 | add(notifier, GBC.std(0, 0).span(2).fill(GBC.HORIZONTAL));
|
|---|
| 44 | notifier.addChangeListener(e -> updateEnabledState());
|
|---|
| 45 |
|
|---|
| 46 | intervalLabel = new JLabel(tr("Check interval (minutes):"));
|
|---|
| 47 | intervalLabel.setLabelFor(notifierInterval);
|
|---|
| 48 | add(intervalLabel, GBC.std(0, 1));
|
|---|
| 49 |
|
|---|
| 50 | notifierInterval.setToolTipText(tr("Default value: {0}", MessageNotifier.PROP_INTERVAL.getDefaultValue()));
|
|---|
| 51 | notifierInterval.setMinimumSize(notifierInterval.getPreferredSize());
|
|---|
| 52 | add(notifierInterval, GBC.std(1, 1).insets(5, 0, 0, 0));
|
|---|
| 53 |
|
|---|
| 54 | final JLabel notesDaysClosedLabel = new JLabel(tr("Max age for closed notes (days):"));
|
|---|
| 55 | notesDaysClosedLabel.setLabelFor(notesDaysClosed);
|
|---|
| 56 | notesDaysClosedLabel.setToolTipText(tr("Specifies the number of days a note needs to be closed to no longer be downloaded"));
|
|---|
| 57 | add(notesDaysClosedLabel, GBC.std(0, 2).insets(0, 20, 0, 0));
|
|---|
| 58 | notesDaysClosed.setToolTipText(tr("Default value: {0}", DownloadNotesTask.DAYS_CLOSED.getDefaultValue()));
|
|---|
| 59 | notesDaysClosed.setMinimumSize(notesDaysClosed.getPreferredSize());
|
|---|
| 60 | add(notesDaysClosed, GBC.std(1, 2).insets(5, 20, 0, 0));
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | private void updateEnabledState() {
|
|---|
| 64 | boolean enabled = notifier.isSelected();
|
|---|
| 65 | intervalLabel.setEnabled(enabled);
|
|---|
| 66 | notifierInterval.setEnabled(enabled);
|
|---|
| 67 | notifierInterval.setEditable(enabled);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Initializes the panel from preferences
|
|---|
| 72 | */
|
|---|
| 73 | public final void initFromPreferences() {
|
|---|
| 74 | notifier.setSelected(MessageNotifier.PROP_NOTIFIER_ENABLED.get());
|
|---|
| 75 | notifierInterval.setText(Integer.toString(MessageNotifier.PROP_INTERVAL.get()));
|
|---|
| 76 | notesDaysClosed.setText(Integer.toString(DownloadNotesTask.DAYS_CLOSED.get()));
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Saves the current values to preferences
|
|---|
| 81 | */
|
|---|
| 82 | public void saveToPreferences() {
|
|---|
| 83 | final boolean enabled = notifier.isSelected();
|
|---|
| 84 | boolean changed = MessageNotifier.PROP_NOTIFIER_ENABLED.put(enabled);
|
|---|
| 85 | changed |= MessageNotifier.PROP_INTERVAL.parseAndPut(notifierInterval.getText());
|
|---|
| 86 | changed |= DownloadNotesTask.DAYS_CLOSED.parseAndPut(notesDaysClosed.getText());
|
|---|
| 87 | // If parameters have changed, restart notifier
|
|---|
| 88 | if (changed) {
|
|---|
| 89 | MessageNotifier.stop();
|
|---|
| 90 | if (enabled) {
|
|---|
| 91 | MessageNotifier.start();
|
|---|
| 92 | }
|
|---|
| 93 | } else {
|
|---|
| 94 | // Even if they have not changed, notifier should be stopped if user is no more identified enough
|
|---|
| 95 | if (!MessageNotifier.isUserEnoughIdentified()) {
|
|---|
| 96 | MessageNotifier.stop();
|
|---|
| 97 | } else if (enabled && !MessageNotifier.isRunning()) {
|
|---|
| 98 | // or restarted if user is again identified and notifier was enabled in preferences
|
|---|
| 99 | MessageNotifier.start();
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|