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

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

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 8.9 KB
RevLine 
[3083]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;
[8388]10import java.util.EnumMap;
[8404]11import java.util.Locale;
[3083]12import java.util.Map;
[11553]13import java.util.Optional;
[3083]14
15import javax.swing.ButtonGroup;
16import javax.swing.JLabel;
17import javax.swing.JPanel;
18import javax.swing.JRadioButton;
19import javax.swing.event.ChangeEvent;
20import javax.swing.event.ChangeListener;
21
22import org.openstreetmap.josm.Main;
[6340]23import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
[8308]24import org.openstreetmap.josm.gui.widgets.JosmTextField;
[3083]25import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
[5145]26import org.openstreetmap.josm.plugins.PluginHandler;
[12620]27import org.openstreetmap.josm.tools.Logging;
[3083]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 {
[9212]36 ASK("ask"),
[3083]37 ALWAYS("always"),
38 NEVER("never");
39
[11489]40 private final String preferenceValue;
41
[3083]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) {
[10179]51 if (preferenceValue == null)
52 return null;
53 String prefValue = preferenceValue.trim().toLowerCase(Locale.ENGLISH);
[3083]54 for (Policy p: Policy.values()) {
[10179]55 if (p.getPreferencesValue().equals(prefValue))
[3083]56 return p;
57 }
58 return null;
59 }
60 }
61
[8308]62 private transient Map<Policy, JRadioButton> rbVersionBasedUpatePolicy;
63 private transient Map<Policy, JRadioButton> rbTimeBasedUpatePolicy;
[10179]64 private final JosmTextField tfUpdateInterval = new JosmTextField(5);
65 private final JLabel lblUpdateInterval = new JLabel(tr("Update interval (in days):"));
[3083]66
[10179]67 /**
68 * Constructs a new {@code PluginUpdatePolicyPanel}.
69 */
70 public PluginUpdatePolicyPanel() {
71 build();
72 initFromPreferences();
73 }
74
[3083]75 protected JPanel buildVersionBasedUpdatePolicyPanel() {
76 JPanel pnl = new JPanel(new GridBagLayout());
77 GridBagConstraints gc = new GridBagConstraints();
78 gc.anchor = GridBagConstraints.NORTHWEST;
79 gc.fill = GridBagConstraints.HORIZONTAL;
[8510]80 gc.weightx = 1.0;
[3083]81
[6267]82 ButtonGroup bgVersionBasedUpdatePolicy = new ButtonGroup();
[8388]83 rbVersionBasedUpatePolicy = new EnumMap<>(Policy.class);
[3083]84 JRadioButton btn = new JRadioButton(tr("Ask before updating"));
85 rbVersionBasedUpatePolicy.put(Policy.ASK, btn);
86 bgVersionBasedUpdatePolicy.add(btn);
87
[4310]88 btn = new JRadioButton(tr("Always update without asking"));
[3083]89 rbVersionBasedUpatePolicy.put(Policy.ALWAYS, btn);
90 bgVersionBasedUpdatePolicy.add(btn);
91
92 btn = new JRadioButton(tr("Never update"));
93 rbVersionBasedUpatePolicy.put(Policy.NEVER, btn);
94 bgVersionBasedUpdatePolicy.add(btn);
95
[8509]96 JMultilineLabel lbl = new JMultilineLabel(
97 tr("Please decide whether JOSM shall automatically update active plugins at startup after an update of JOSM itself."));
[8510]98 gc.gridy = 0;
[3083]99 pnl.add(lbl, gc);
100 for (Policy p: Policy.values()) {
101 gc.gridy++;
102 pnl.add(rbVersionBasedUpatePolicy.get(p), gc);
103 }
104 return pnl;
105 }
106
107 protected JPanel buildUpdateIntervalPanel() {
108 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
[10179]109 pnl.add(lblUpdateInterval);
110 pnl.add(tfUpdateInterval);
[8426]111 lblUpdateInterval.setLabelFor(tfUpdateInterval);
[3083]112 SelectAllOnFocusGainedDecorator.decorate(tfUpdateInterval);
113 return pnl;
114 }
115
116 protected JPanel buildTimeBasedUpdatePolicyPanel() {
117 JPanel pnl = new JPanel(new GridBagLayout());
118 GridBagConstraints gc = new GridBagConstraints();
119 gc.anchor = GridBagConstraints.NORTHWEST;
120 gc.fill = GridBagConstraints.HORIZONTAL;
[8510]121 gc.weightx = 1.0;
[3083]122
123 TimeBasedPolicyChangeListener changeListener = new TimeBasedPolicyChangeListener();
124
[6267]125 ButtonGroup bgTimeBasedUpdatePolicy = new ButtonGroup();
[8388]126 rbTimeBasedUpatePolicy = new EnumMap<>(Policy.class);
[3083]127 JRadioButton btn = new JRadioButton(tr("Ask before updating"));
128 btn.addChangeListener(changeListener);
129 rbTimeBasedUpatePolicy.put(Policy.ASK, btn);
130 bgTimeBasedUpdatePolicy.add(btn);
131
[4310]132 btn = new JRadioButton(tr("Always update without asking"));
[3083]133 btn.addChangeListener(changeListener);
134 rbTimeBasedUpatePolicy.put(Policy.ALWAYS, btn);
135 bgTimeBasedUpdatePolicy.add(btn);
136
137 btn = new JRadioButton(tr("Never update"));
138 btn.addChangeListener(changeListener);
139 rbTimeBasedUpatePolicy.put(Policy.NEVER, btn);
140 bgTimeBasedUpdatePolicy.add(btn);
141
[8426]142 JMultilineLabel lbl = new JMultilineLabel(
143 tr("Please decide whether JOSM shall automatically update active plugins after a certain period of time."));
[8510]144 gc.gridy = 0;
[3083]145 pnl.add(lbl, gc);
146 for (Policy p: Policy.values()) {
147 gc.gridy++;
148 pnl.add(rbTimeBasedUpatePolicy.get(p), gc);
149 }
150 gc.gridy++;
151 pnl.add(buildUpdateIntervalPanel(), gc);
152 return pnl;
153 }
154
[6890]155 protected final void build() {
[3083]156 setLayout(new GridBagLayout());
157 GridBagConstraints gc = new GridBagConstraints();
158 gc.anchor = GridBagConstraints.NORTHWEST;
159 gc.fill = GridBagConstraints.HORIZONTAL;
[8510]160 gc.weightx = 1.0;
161 gc.insets = new Insets(5, 5, 10, 5);
[3083]162
163 add(buildVersionBasedUpdatePolicyPanel(), gc);
164 gc.gridy = 1;
165 add(buildTimeBasedUpdatePolicyPanel(), gc);
166
167 gc.gridy = 2;
168 gc.weighty = 1.0;
169 gc.fill = GridBagConstraints.BOTH;
170 add(new JPanel(), gc);
171 }
172
[6267]173 /**
[3083]174 * Loads the relevant preference values from the JOSM preferences
175 */
[6890]176 public final void initFromPreferences() {
[11553]177 rbVersionBasedUpatePolicy.get(Optional.ofNullable(Policy.fromPreferenceValue(
178 Main.pref.get("pluginmanager.version-based-update.policy", "ask"))).orElse(Policy.ASK)).setSelected(true);
179 rbTimeBasedUpatePolicy.get(Optional.ofNullable(Policy.fromPreferenceValue(
180 Main.pref.get("pluginmanager.time-based-update.policy", "ask"))).orElse(Policy.ASK)).setSelected(true);
[3083]181
[11553]182 String pref = Main.pref.get("pluginmanager.warntime", null);
[3083]183 int days = 0;
184 if (pref != null) {
185 // remove legacy preference
186 Main.pref.put("pluginmanager.warntime", null);
187 try {
[11553]188 days = Integer.parseInt(pref.trim());
[8510]189 } catch (NumberFormatException e) {
[3083]190 // ignore - load from preference pluginmanager.time-based-update.interval
[12620]191 Logging.trace(e);
[3083]192 }
193 if (days <= 0) {
[5145]194 days = PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL;
[3083]195 }
196 }
197 if (days == 0) {
[5145]198 days = Main.pref.getInteger("pluginmanager.time-based-update.interval", PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL);
[3083]199 }
200 tfUpdateInterval.setText(Integer.toString(days));
201 }
202
203 /**
204 * Remebers the update policy preference settings on the JOSM preferences
205 */
206 public void rememberInPreferences() {
207
208 // remember policy for version based update
209 //
210 for (Policy p: Policy.values()) {
211 if (rbVersionBasedUpatePolicy.get(p).isSelected()) {
212 Main.pref.put("pluginmanager.version-based-update.policy", p.getPreferencesValue());
213 break;
214 }
215 }
216
217 // remember policy for time based update
218 //
219 for (Policy p: Policy.values()) {
220 if (rbTimeBasedUpatePolicy.get(p).isSelected()) {
221 Main.pref.put("pluginmanager.time-based-update.policy", p.getPreferencesValue());
222 break;
223 }
224 }
225
226 // remember update interval
227 //
228 int days = 0;
229 try {
230 days = Integer.parseInt(tfUpdateInterval.getText().trim());
231 if (days <= 0) {
[5145]232 days = PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL;
[3083]233 }
[8510]234 } catch (NumberFormatException e) {
[5145]235 days = PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL;
[3083]236 }
237 Main.pref.putInteger("pluginmanager.time-based-update.interval", days);
238 }
239
240 class TimeBasedPolicyChangeListener implements ChangeListener {
[6084]241 @Override
[3083]242 public void stateChanged(ChangeEvent e) {
243 lblUpdateInterval.setEnabled(!rbTimeBasedUpatePolicy.get(Policy.NEVER).isSelected());
244 tfUpdateInterval.setEnabled(!rbTimeBasedUpatePolicy.get(Policy.NEVER).isSelected());
245 }
246 }
247
248}
Note: See TracBrowser for help on using the repository browser.