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.gui.widgets.JosmTextField; 016import org.openstreetmap.josm.io.MessageNotifier; 017import org.openstreetmap.josm.tools.GBC; 018 019/** 020 * Preferences panel for OSM messages notifier. 021 * @since 6349 022 */ 023public class MessagesNotifierPanel extends JPanel { 024 025 private JCheckBox notifier; 026 private JLabel intervalLabel; 027 private final JosmTextField notifierInterval = new JosmTextField(4); 028 029 /** 030 * Constructs a new {@code MessagesNotifierPanel}. 031 */ 032 public MessagesNotifierPanel() { 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(new ChangeListener() { 045 @Override 046 public void stateChanged(ChangeEvent e) { 047 updateEnabledState(); 048 } 049 }); 050 051 intervalLabel = new JLabel(tr("Check interval (minutes):")); 052 add(intervalLabel, GBC.std().insets(25,0,0,0)); 053 054 notifierInterval.setToolTipText(tr("Default value: {0}", MessageNotifier.PROP_INTERVAL.getDefaultValue())); 055 notifierInterval.setMinimumSize(notifierInterval.getPreferredSize()); 056 add(notifierInterval, GBC.eol().insets(5,0,0,0)); 057 } 058 059 private void updateEnabledState() { 060 boolean enabled = notifier.isSelected(); 061 intervalLabel.setEnabled(enabled); 062 notifierInterval.setEnabled(enabled); 063 notifierInterval.setEditable(enabled); 064 } 065 066 /** 067 * Initializes the panel from preferences 068 */ 069 public final void initFromPreferences() { 070 notifier.setSelected(MessageNotifier.PROP_NOTIFIER_ENABLED.get()); 071 notifierInterval.setText(Integer.toString(MessageNotifier.PROP_INTERVAL.get())); 072 } 073 074 /** 075 * Saves the current values to preferences 076 */ 077 public void saveToPreferences() { 078 final boolean enabled = notifier.isSelected(); 079 boolean changed = MessageNotifier.PROP_NOTIFIER_ENABLED.put(enabled); 080 changed |= MessageNotifier.PROP_INTERVAL.parseAndPut(notifierInterval.getText()); 081 // If parameters have changed, restart notifier 082 if (changed) { 083 MessageNotifier.stop(); 084 if (enabled) { 085 MessageNotifier.start(); 086 } 087 // Even if they have not changed, 088 } else { 089 // notifier should be stopped if user is no more identified enough 090 if (!MessageNotifier.isUserEnoughIdentified()) { 091 MessageNotifier.stop(); 092 // or restarted if user is again identified and notifier was enabled in preferences 093 } else if (enabled && !MessageNotifier.isRunning()) { 094 MessageNotifier.start(); 095 } 096 } 097 } 098}