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        notesDaysClosed.setEditable(enabled);
069    }
070
071    /**
072     * Initializes the panel from preferences
073     */
074    public final void initFromPreferences() {
075        notifier.setSelected(MessageNotifier.PROP_NOTIFIER_ENABLED.get());
076        notifierInterval.setText(Integer.toString(MessageNotifier.PROP_INTERVAL.get()));
077        notesDaysClosed.setText(Integer.toString(DownloadNotesTask.DAYS_CLOSED.get()));
078    }
079
080    /**
081     * Saves the current values to preferences
082     */
083    public void saveToPreferences() {
084        final boolean enabled = notifier.isSelected();
085        boolean changed = MessageNotifier.PROP_NOTIFIER_ENABLED.put(enabled);
086        changed |= MessageNotifier.PROP_INTERVAL.parseAndPut(notifierInterval.getText());
087        changed |= DownloadNotesTask.DAYS_CLOSED.parseAndPut(notesDaysClosed.getText());
088        // If parameters have changed, restart notifier
089        if (changed) {
090            MessageNotifier.stop();
091            if (enabled) {
092                MessageNotifier.start();
093            }
094        } else {
095            // Even if they have not changed, notifier should be stopped if user is no more identified enough
096            if (!MessageNotifier.isUserEnoughIdentified()) {
097                MessageNotifier.stop();
098            } else if (enabled && !MessageNotifier.isRunning()) {
099                // or restarted if user is again identified and notifier was enabled in preferences
100                MessageNotifier.start();
101            }
102        }
103    }
104}