source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/server/FeaturesPanel.java@ 10611

Last change on this file since 10611 was 10611, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.server;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7
8import javax.swing.BorderFactory;
9import javax.swing.JCheckBox;
10import javax.swing.JLabel;
11import javax.swing.JPanel;
12
13import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
14import org.openstreetmap.josm.gui.widgets.JosmTextField;
15import org.openstreetmap.josm.io.MessageNotifier;
16import org.openstreetmap.josm.tools.GBC;
17
18/**
19 * Preferences panel for OSM messages notifier.
20 * @since 6349
21 */
22public class FeaturesPanel extends JPanel {
23
24 private JCheckBox notifier;
25 private JLabel intervalLabel;
26 private final JosmTextField notifierInterval = new JosmTextField(4);
27 private final JosmTextField notesDaysClosed = new JosmTextField(4);
28
29 /**
30 * Constructs a new {@code MessagesNotifierPanel}.
31 */
32 public FeaturesPanel() {
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(e -> updateEnabledState());
45
46 intervalLabel = new JLabel(tr("Check interval (minutes):"));
47 intervalLabel.setLabelFor(notifierInterval);
48 add(intervalLabel, GBC.std().insets(25, 0, 0, 0));
49
50 notifierInterval.setToolTipText(tr("Default value: {0}", MessageNotifier.PROP_INTERVAL.getDefaultValue()));
51 notifierInterval.setMinimumSize(notifierInterval.getPreferredSize());
52 add(notifierInterval, GBC.eol().insets(5, 0, 0, 0));
53
54 final JLabel notesDaysClosedLabel = new JLabel(tr("Max age for closed notes (days):"));
55 notesDaysClosedLabel.setLabelFor(notesDaysClosed);
56 notesDaysClosedLabel.setToolTipText(tr("Specifies the number of days a note needs to be closed to no longer be downloaded"));
57 add(notesDaysClosedLabel, GBC.std().insets(0, 20, 0, 0));
58 notesDaysClosed.setToolTipText(tr("Default value: {0}", DownloadNotesTask.DAYS_CLOSED.getDefaultValue()));
59 notesDaysClosed.setMinimumSize(notesDaysClosed.getPreferredSize());
60 add(notesDaysClosed, GBC.eol().insets(5, 20, 0, 0));
61 }
62
63 private void updateEnabledState() {
64 boolean enabled = notifier.isSelected();
65 intervalLabel.setEnabled(enabled);
66 notifierInterval.setEnabled(enabled);
67 notifierInterval.setEditable(enabled);
68 notesDaysClosed.setEditable(enabled);
69 }
70
71 /**
72 * Initializes the panel from preferences
73 */
74 public final void initFromPreferences() {
75 notifier.setSelected(MessageNotifier.PROP_NOTIFIER_ENABLED.get());
76 notifierInterval.setText(Integer.toString(MessageNotifier.PROP_INTERVAL.get()));
77 notesDaysClosed.setText(Integer.toString(DownloadNotesTask.DAYS_CLOSED.get()));
78 }
79
80 /**
81 * Saves the current values to preferences
82 */
83 public void saveToPreferences() {
84 final boolean enabled = notifier.isSelected();
85 boolean changed = MessageNotifier.PROP_NOTIFIER_ENABLED.put(enabled);
86 changed |= MessageNotifier.PROP_INTERVAL.parseAndPut(notifierInterval.getText());
87 changed |= DownloadNotesTask.DAYS_CLOSED.parseAndPut(notesDaysClosed.getText());
88 // If parameters have changed, restart notifier
89 if (changed) {
90 MessageNotifier.stop();
91 if (enabled) {
92 MessageNotifier.start();
93 }
94 } else {
95 // Even if they have not changed, notifier should be stopped if user is no more identified enough
96 if (!MessageNotifier.isUserEnoughIdentified()) {
97 MessageNotifier.stop();
98 } else if (enabled && !MessageNotifier.isRunning()) {
99 // or restarted if user is again identified and notifier was enabled in preferences
100 MessageNotifier.start();
101 }
102 }
103 }
104}
Note: See TracBrowser for help on using the repository browser.