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 | import javax.swing.event.ChangeEvent;
|
---|
13 | import javax.swing.event.ChangeListener;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
16 | import org.openstreetmap.josm.io.MessageNotifier;
|
---|
17 | import org.openstreetmap.josm.tools.GBC;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Preferences panel for OSM messages notifier.
|
---|
21 | * @since 6349
|
---|
22 | */
|
---|
23 | public class MessagesNotifierPanel extends JPanel {
|
---|
24 |
|
---|
25 | private JCheckBox notifier;
|
---|
26 | private JLabel intervalLabel;
|
---|
27 | private final JosmTextField notifierInterval = new JosmTextField(4);
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code MessagesNotifierPanel}.
|
---|
31 | */
|
---|
32 | public MessagesNotifierPanel() {
|
---|
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.eol());
|
---|
44 | notifier.addChangeListener(new ChangeListener() {
|
---|
45 | @Override
|
---|
46 | public void stateChanged(ChangeEvent e) {
|
---|
47 | updateEnabledState();
|
---|
48 | }
|
---|
49 | });
|
---|
50 |
|
---|
51 | intervalLabel = new JLabel(tr("Check interval (minutes):"));
|
---|
52 | add(intervalLabel, GBC.std().insets(25,0,0,0));
|
---|
53 |
|
---|
54 | notifierInterval.setToolTipText(tr("Default value: {0}", MessageNotifier.PROP_INTERVAL.getDefaultValue()));
|
---|
55 | notifierInterval.setMinimumSize(notifierInterval.getPreferredSize());
|
---|
56 | add(notifierInterval, GBC.eol().insets(5,0,0,0));
|
---|
57 | }
|
---|
58 |
|
---|
59 | private void updateEnabledState() {
|
---|
60 | boolean enabled = notifier.isSelected();
|
---|
61 | intervalLabel.setEnabled(enabled);
|
---|
62 | notifierInterval.setEnabled(enabled);
|
---|
63 | notifierInterval.setEditable(enabled);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Initializes the panel from preferences
|
---|
68 | */
|
---|
69 | public void initFromPreferences() {
|
---|
70 | notifier.setSelected(MessageNotifier.PROP_NOTIFIER_ENABLED.get());
|
---|
71 | notifierInterval.setText(Integer.toString(MessageNotifier.PROP_INTERVAL.get()));
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Saves the current values to preferences
|
---|
76 | */
|
---|
77 | public void saveToPreferences() {
|
---|
78 | final boolean enabled = notifier.isSelected();
|
---|
79 | boolean changed = MessageNotifier.PROP_NOTIFIER_ENABLED.put(enabled);
|
---|
80 | changed |= MessageNotifier.PROP_INTERVAL.parseAndPut(notifierInterval.getText());
|
---|
81 | // If parameters have changed, restart notifier
|
---|
82 | if (changed) {
|
---|
83 | MessageNotifier.stop();
|
---|
84 | if (enabled) {
|
---|
85 | MessageNotifier.start();
|
---|
86 | }
|
---|
87 | // Even if they have not changed,
|
---|
88 | } else {
|
---|
89 | // notifier should be stopped if user is no more identified enough
|
---|
90 | if (!MessageNotifier.isUserEnoughIdentified()) {
|
---|
91 | MessageNotifier.stop();
|
---|
92 | // or restarted if user is again identified and notifier was enabled in preferences
|
---|
93 | } else if (enabled && !MessageNotifier.isRunning()) {
|
---|
94 | MessageNotifier.start();
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|