source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/plugin/PluginUpdatePolicyPanel.java@ 16438

Last change on this file since 16438 was 16438, checked in by simon04, 4 years ago

see #19251 - Java 8: use Stream

  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.plugin;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.FlowLayout;
7import java.awt.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.Insets;
10import java.util.Arrays;
11import java.util.EnumMap;
12import java.util.Locale;
13import java.util.Map;
14import java.util.Optional;
15
16import javax.swing.ButtonGroup;
17import javax.swing.JLabel;
18import javax.swing.JPanel;
19import javax.swing.JRadioButton;
20import javax.swing.event.ChangeEvent;
21import javax.swing.event.ChangeListener;
22
23import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
24import org.openstreetmap.josm.gui.widgets.JosmTextField;
25import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
26import org.openstreetmap.josm.plugins.PluginHandler;
27import org.openstreetmap.josm.spi.preferences.Config;
28
29/**
30 * A panel for configuring whether JOSM shall update plugins at startup.
31 *
32 */
33public class PluginUpdatePolicyPanel extends JPanel {
34
35 private enum Policy {
36 ASK("ask"),
37 ALWAYS("always"),
38 NEVER("never");
39
40 private final String preferenceValue;
41
42 Policy(String preferenceValue) {
43 this.preferenceValue = preferenceValue;
44 }
45
46 public String getPreferencesValue() {
47 return preferenceValue;
48 }
49
50 static Policy fromPreferenceValue(String preferenceValue) {
51 if (preferenceValue == null)
52 return null;
53 String prefValue = preferenceValue.trim().toLowerCase(Locale.ENGLISH);
54 return Arrays.stream(Policy.values())
55 .filter(p -> p.getPreferencesValue().equals(prefValue))
56 .findFirst().orElse(null);
57 }
58 }
59
60 private transient Map<Policy, JRadioButton> rbVersionBasedUpatePolicy;
61 private transient Map<Policy, JRadioButton> rbTimeBasedUpatePolicy;
62 private final JosmTextField tfUpdateInterval = new JosmTextField(5);
63 private final JLabel lblUpdateInterval = new JLabel(tr("Update interval (in days):"));
64
65 /**
66 * Constructs a new {@code PluginUpdatePolicyPanel}.
67 */
68 public PluginUpdatePolicyPanel() {
69 build();
70 initFromPreferences();
71 }
72
73 protected JPanel buildVersionBasedUpdatePolicyPanel() {
74 JPanel pnl = new JPanel(new GridBagLayout());
75 GridBagConstraints gc = new GridBagConstraints();
76 gc.anchor = GridBagConstraints.NORTHWEST;
77 gc.fill = GridBagConstraints.HORIZONTAL;
78 gc.weightx = 1.0;
79
80 ButtonGroup bgVersionBasedUpdatePolicy = new ButtonGroup();
81 rbVersionBasedUpatePolicy = new EnumMap<>(Policy.class);
82 JRadioButton btn = new JRadioButton(tr("Ask before updating"));
83 rbVersionBasedUpatePolicy.put(Policy.ASK, btn);
84 bgVersionBasedUpdatePolicy.add(btn);
85
86 btn = new JRadioButton(tr("Always update without asking"));
87 rbVersionBasedUpatePolicy.put(Policy.ALWAYS, btn);
88 bgVersionBasedUpdatePolicy.add(btn);
89
90 btn = new JRadioButton(tr("Never update"));
91 rbVersionBasedUpatePolicy.put(Policy.NEVER, btn);
92 bgVersionBasedUpdatePolicy.add(btn);
93
94 JMultilineLabel lbl = new JMultilineLabel(
95 tr("Please decide whether JOSM shall automatically update active plugins at startup after an update of JOSM itself."));
96 gc.gridy = 0;
97 pnl.add(lbl, gc);
98 for (Policy p: Policy.values()) {
99 gc.gridy++;
100 pnl.add(rbVersionBasedUpatePolicy.get(p), gc);
101 }
102 return pnl;
103 }
104
105 protected JPanel buildUpdateIntervalPanel() {
106 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
107 pnl.add(lblUpdateInterval);
108 pnl.add(tfUpdateInterval);
109 lblUpdateInterval.setLabelFor(tfUpdateInterval);
110 SelectAllOnFocusGainedDecorator.decorate(tfUpdateInterval);
111 return pnl;
112 }
113
114 protected JPanel buildTimeBasedUpdatePolicyPanel() {
115 JPanel pnl = new JPanel(new GridBagLayout());
116 GridBagConstraints gc = new GridBagConstraints();
117 gc.anchor = GridBagConstraints.NORTHWEST;
118 gc.fill = GridBagConstraints.HORIZONTAL;
119 gc.weightx = 1.0;
120
121 TimeBasedPolicyChangeListener changeListener = new TimeBasedPolicyChangeListener();
122
123 ButtonGroup bgTimeBasedUpdatePolicy = new ButtonGroup();
124 rbTimeBasedUpatePolicy = new EnumMap<>(Policy.class);
125 JRadioButton btn = new JRadioButton(tr("Ask before updating"));
126 btn.addChangeListener(changeListener);
127 rbTimeBasedUpatePolicy.put(Policy.ASK, btn);
128 bgTimeBasedUpdatePolicy.add(btn);
129
130 btn = new JRadioButton(tr("Always update without asking"));
131 btn.addChangeListener(changeListener);
132 rbTimeBasedUpatePolicy.put(Policy.ALWAYS, btn);
133 bgTimeBasedUpdatePolicy.add(btn);
134
135 btn = new JRadioButton(tr("Never update"));
136 btn.addChangeListener(changeListener);
137 rbTimeBasedUpatePolicy.put(Policy.NEVER, btn);
138 bgTimeBasedUpdatePolicy.add(btn);
139
140 JMultilineLabel lbl = new JMultilineLabel(
141 tr("Please decide whether JOSM shall automatically update active plugins after a certain period of time."));
142 gc.gridy = 0;
143 pnl.add(lbl, gc);
144 for (Policy p: Policy.values()) {
145 gc.gridy++;
146 pnl.add(rbTimeBasedUpatePolicy.get(p), gc);
147 }
148 gc.gridy++;
149 pnl.add(buildUpdateIntervalPanel(), gc);
150 return pnl;
151 }
152
153 protected final void build() {
154 setLayout(new GridBagLayout());
155 GridBagConstraints gc = new GridBagConstraints();
156 gc.anchor = GridBagConstraints.NORTHWEST;
157 gc.fill = GridBagConstraints.HORIZONTAL;
158 gc.weightx = 1.0;
159 gc.insets = new Insets(5, 5, 10, 5);
160
161 add(buildVersionBasedUpdatePolicyPanel(), gc);
162 gc.gridy = 1;
163 add(buildTimeBasedUpdatePolicyPanel(), gc);
164
165 gc.gridy = 2;
166 gc.weighty = 1.0;
167 gc.fill = GridBagConstraints.BOTH;
168 add(new JPanel(), gc);
169 }
170
171 /**
172 * Loads the relevant preference values from the JOSM preferences
173 */
174 public final void initFromPreferences() {
175 rbVersionBasedUpatePolicy.get(
176 Optional.ofNullable(Policy.fromPreferenceValue(
177 Config.getPref().get("pluginmanager.version-based-update.policy", "ask"))).orElse(Policy.ASK))
178 .setSelected(true);
179 rbTimeBasedUpatePolicy.get(
180 Optional.ofNullable(Policy.fromPreferenceValue(
181 Config.getPref().get("pluginmanager.time-based-update.policy", "ask"))).orElse(Policy.ASK))
182 .setSelected(true);
183
184 int days = Config.getPref().getInt("pluginmanager.time-based-update.interval", PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL);
185 tfUpdateInterval.setText(Integer.toString(days));
186 }
187
188 /**
189 * Remebers the update policy preference settings on the JOSM preferences
190 */
191 public void rememberInPreferences() {
192
193 // remember policy for version based update
194 Arrays.stream(Policy.values())
195 .filter(p -> rbVersionBasedUpatePolicy.get(p).isSelected()).findFirst()
196 .ifPresent(p -> Config.getPref().put("pluginmanager.version-based-update.policy", p.getPreferencesValue()));
197
198 // remember policy for time based update
199 Arrays.stream(Policy.values())
200 .filter(p -> rbTimeBasedUpatePolicy.get(p).isSelected()).findFirst()
201 .ifPresent(p -> Config.getPref().put("pluginmanager.time-based-update.policy", p.getPreferencesValue()));
202
203 // remember update interval
204 //
205 int days = 0;
206 try {
207 days = Integer.parseInt(tfUpdateInterval.getText().trim());
208 if (days <= 0) {
209 days = PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL;
210 }
211 } catch (NumberFormatException e) {
212 days = PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL;
213 }
214 Config.getPref().putInt("pluginmanager.time-based-update.interval", days);
215 }
216
217 class TimeBasedPolicyChangeListener implements ChangeListener {
218 @Override
219 public void stateChanged(ChangeEvent e) {
220 lblUpdateInterval.setEnabled(!rbTimeBasedUpatePolicy.get(Policy.NEVER).isSelected());
221 tfUpdateInterval.setEnabled(!rbTimeBasedUpatePolicy.get(Policy.NEVER).isSelected());
222 }
223 }
224
225}
Note: See TracBrowser for help on using the repository browser.