001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences.server;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.GridBagLayout;
007
008import javax.swing.BorderFactory;
009import javax.swing.JCheckBox;
010import javax.swing.JLabel;
011import javax.swing.JPanel;
012
013import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
014import org.openstreetmap.josm.gui.widgets.JosmTextField;
015import org.openstreetmap.josm.io.MessageNotifier;
016import org.openstreetmap.josm.tools.GBC;
017
018/**
019 * Preferences panel for OSM messages notifier.
020 * @since 6349
021 */
022public class FeaturesPanel extends JPanel {
023
024    private JCheckBox notifier;
025    private JLabel intervalLabel;
026    private final JosmTextField notifierInterval = new JosmTextField(4);
027    private final JosmTextField notesDaysClosed = new JosmTextField(4);
028
029    /**
030     * Constructs a new {@code MessagesNotifierPanel}.
031     */
032    public FeaturesPanel() {
033        build();
034        initFromPreferences();
035        updateEnabledState();
036    }
037
038    private void build() {
039        setLayout(new GridBagLayout());
040        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
041
042        notifier = new JCheckBox(tr("Periodically check for new messages"));
043        add(notifier, GBC.eol());
044        notifier.addChangeListener(e -> updateEnabledState());
045
046        intervalLabel = new JLabel(tr("Check interval (minutes):"));
047        intervalLabel.setLabelFor(notifierInterval);
048        add(intervalLabel, GBC.std().insets(25, 0, 0, 0));
049
050        notifierInterval.setToolTipText(tr("Default value: {0}", MessageNotifier.PROP_INTERVAL.getDefaultValue()));
051        notifierInterval.setMinimumSize(notifierInterval.getPreferredSize());
052        add(notifierInterval, GBC.eol().insets(5, 0, 0, 0));
053
054        final JLabel notesDaysClosedLabel = new JLabel(tr("Max age for closed notes (days):"));
055        notesDaysClosedLabel.setLabelFor(notesDaysClosed);
056        notesDaysClosedLabel.setToolTipText(tr("Specifies the number of days a note needs to be closed to no longer be downloaded"));
057        add(notesDaysClosedLabel, GBC.std().insets(0, 20, 0, 0));
058        notesDaysClosed.setToolTipText(tr("Default value: {0}", DownloadNotesTask.DAYS_CLOSED.getDefaultValue()));
059        notesDaysClosed.setMinimumSize(notesDaysClosed.getPreferredSize());
060        add(notesDaysClosed, GBC.eol().insets(5, 20, 0, 0));
061    }
062
063    private void updateEnabledState() {
064        boolean enabled = notifier.isSelected();
065        intervalLabel.setEnabled(enabled);
066        notifierInterval.setEnabled(enabled);
067        notifierInterval.setEditable(enabled);
068    }
069
070    /**
071     * Initializes the panel from preferences
072     */
073    public final void initFromPreferences() {
074        notifier.setSelected(MessageNotifier.PROP_NOTIFIER_ENABLED.get());
075        notifierInterval.setText(Integer.toString(MessageNotifier.PROP_INTERVAL.get()));
076        notesDaysClosed.setText(Integer.toString(DownloadNotesTask.DAYS_CLOSED.get()));
077    }
078
079    /**
080     * Saves the current values to preferences
081     */
082    public void saveToPreferences() {
083        final boolean enabled = notifier.isSelected();
084        boolean changed = MessageNotifier.PROP_NOTIFIER_ENABLED.put(enabled);
085        changed |= MessageNotifier.PROP_INTERVAL.parseAndPut(notifierInterval.getText());
086        changed |= DownloadNotesTask.DAYS_CLOSED.parseAndPut(notesDaysClosed.getText());
087        // If parameters have changed, restart notifier
088        if (changed) {
089            MessageNotifier.stop();
090            if (enabled) {
091                MessageNotifier.start();
092            }
093        } else {
094            // Even if they have not changed, notifier should be stopped if user is no more identified enough
095            if (!MessageNotifier.isUserEnoughIdentified()) {
096                MessageNotifier.stop();
097            } else if (enabled && !MessageNotifier.isRunning()) {
098                // or restarted if user is again identified and notifier was enabled in preferences
099                MessageNotifier.start();
100            }
101        }
102    }
103}