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