1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.plugin;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.FlowLayout;
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Insets;
|
---|
10 | import java.util.EnumMap;
|
---|
11 | import java.util.Locale;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Optional;
|
---|
14 |
|
---|
15 | import javax.swing.ButtonGroup;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JRadioButton;
|
---|
19 | import javax.swing.event.ChangeEvent;
|
---|
20 | import javax.swing.event.ChangeListener;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
23 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
24 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
25 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
26 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
27 | import org.openstreetmap.josm.tools.Logging;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * A panel for configuring whether JOSM shall update plugins at startup.
|
---|
31 | *
|
---|
32 | */
|
---|
33 | public 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 | for (Policy p: Policy.values()) {
|
---|
55 | if (p.getPreferencesValue().equals(prefValue))
|
---|
56 | return p;
|
---|
57 | }
|
---|
58 | return null;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | private transient Map<Policy, JRadioButton> rbVersionBasedUpatePolicy;
|
---|
63 | private transient Map<Policy, JRadioButton> rbTimeBasedUpatePolicy;
|
---|
64 | private final JosmTextField tfUpdateInterval = new JosmTextField(5);
|
---|
65 | private final JLabel lblUpdateInterval = new JLabel(tr("Update interval (in days):"));
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Constructs a new {@code PluginUpdatePolicyPanel}.
|
---|
69 | */
|
---|
70 | public PluginUpdatePolicyPanel() {
|
---|
71 | build();
|
---|
72 | initFromPreferences();
|
---|
73 | }
|
---|
74 |
|
---|
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;
|
---|
80 | gc.weightx = 1.0;
|
---|
81 |
|
---|
82 | ButtonGroup bgVersionBasedUpdatePolicy = new ButtonGroup();
|
---|
83 | rbVersionBasedUpatePolicy = new EnumMap<>(Policy.class);
|
---|
84 | JRadioButton btn = new JRadioButton(tr("Ask before updating"));
|
---|
85 | rbVersionBasedUpatePolicy.put(Policy.ASK, btn);
|
---|
86 | bgVersionBasedUpdatePolicy.add(btn);
|
---|
87 |
|
---|
88 | btn = new JRadioButton(tr("Always update without asking"));
|
---|
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 |
|
---|
96 | JMultilineLabel lbl = new JMultilineLabel(
|
---|
97 | tr("Please decide whether JOSM shall automatically update active plugins at startup after an update of JOSM itself."));
|
---|
98 | gc.gridy = 0;
|
---|
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));
|
---|
109 | pnl.add(lblUpdateInterval);
|
---|
110 | pnl.add(tfUpdateInterval);
|
---|
111 | lblUpdateInterval.setLabelFor(tfUpdateInterval);
|
---|
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;
|
---|
121 | gc.weightx = 1.0;
|
---|
122 |
|
---|
123 | TimeBasedPolicyChangeListener changeListener = new TimeBasedPolicyChangeListener();
|
---|
124 |
|
---|
125 | ButtonGroup bgTimeBasedUpdatePolicy = new ButtonGroup();
|
---|
126 | rbTimeBasedUpatePolicy = new EnumMap<>(Policy.class);
|
---|
127 | JRadioButton btn = new JRadioButton(tr("Ask before updating"));
|
---|
128 | btn.addChangeListener(changeListener);
|
---|
129 | rbTimeBasedUpatePolicy.put(Policy.ASK, btn);
|
---|
130 | bgTimeBasedUpdatePolicy.add(btn);
|
---|
131 |
|
---|
132 | btn = new JRadioButton(tr("Always update without asking"));
|
---|
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 |
|
---|
142 | JMultilineLabel lbl = new JMultilineLabel(
|
---|
143 | tr("Please decide whether JOSM shall automatically update active plugins after a certain period of time."));
|
---|
144 | gc.gridy = 0;
|
---|
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 |
|
---|
155 | protected final void build() {
|
---|
156 | setLayout(new GridBagLayout());
|
---|
157 | GridBagConstraints gc = new GridBagConstraints();
|
---|
158 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
159 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
160 | gc.weightx = 1.0;
|
---|
161 | gc.insets = new Insets(5, 5, 10, 5);
|
---|
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 |
|
---|
173 | /**
|
---|
174 | * Loads the relevant preference values from the JOSM preferences
|
---|
175 | */
|
---|
176 | public final void initFromPreferences() {
|
---|
177 | rbVersionBasedUpatePolicy.get(
|
---|
178 | Optional.ofNullable(Policy.fromPreferenceValue(
|
---|
179 | Config.getPref().get("pluginmanager.version-based-update.policy", "ask"))).orElse(Policy.ASK))
|
---|
180 | .setSelected(true);
|
---|
181 | rbTimeBasedUpatePolicy.get(
|
---|
182 | Optional.ofNullable(Policy.fromPreferenceValue(
|
---|
183 | Config.getPref().get("pluginmanager.time-based-update.policy", "ask"))).orElse(Policy.ASK))
|
---|
184 | .setSelected(true);
|
---|
185 |
|
---|
186 | String pref = Config.getPref().get("pluginmanager.warntime", null);
|
---|
187 | int days = 0;
|
---|
188 | if (pref != null) {
|
---|
189 | // remove legacy preference
|
---|
190 | Config.getPref().put("pluginmanager.warntime", null);
|
---|
191 | try {
|
---|
192 | days = Integer.parseInt(pref.trim());
|
---|
193 | } catch (NumberFormatException e) {
|
---|
194 | // ignore - load from preference pluginmanager.time-based-update.interval
|
---|
195 | Logging.trace(e);
|
---|
196 | }
|
---|
197 | if (days <= 0) {
|
---|
198 | days = PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | if (days == 0) {
|
---|
202 | days = Config.getPref().getInt("pluginmanager.time-based-update.interval", PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL);
|
---|
203 | }
|
---|
204 | tfUpdateInterval.setText(Integer.toString(days));
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Remebers the update policy preference settings on the JOSM preferences
|
---|
209 | */
|
---|
210 | public void rememberInPreferences() {
|
---|
211 |
|
---|
212 | // remember policy for version based update
|
---|
213 | //
|
---|
214 | for (Policy p: Policy.values()) {
|
---|
215 | if (rbVersionBasedUpatePolicy.get(p).isSelected()) {
|
---|
216 | Config.getPref().put("pluginmanager.version-based-update.policy", p.getPreferencesValue());
|
---|
217 | break;
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | // remember policy for time based update
|
---|
222 | //
|
---|
223 | for (Policy p: Policy.values()) {
|
---|
224 | if (rbTimeBasedUpatePolicy.get(p).isSelected()) {
|
---|
225 | Config.getPref().put("pluginmanager.time-based-update.policy", p.getPreferencesValue());
|
---|
226 | break;
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | // remember update interval
|
---|
231 | //
|
---|
232 | int days = 0;
|
---|
233 | try {
|
---|
234 | days = Integer.parseInt(tfUpdateInterval.getText().trim());
|
---|
235 | if (days <= 0) {
|
---|
236 | days = PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL;
|
---|
237 | }
|
---|
238 | } catch (NumberFormatException e) {
|
---|
239 | days = PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL;
|
---|
240 | }
|
---|
241 | Config.getPref().putInt("pluginmanager.time-based-update.interval", days);
|
---|
242 | }
|
---|
243 |
|
---|
244 | class TimeBasedPolicyChangeListener implements ChangeListener {
|
---|
245 | @Override
|
---|
246 | public void stateChanged(ChangeEvent e) {
|
---|
247 | lblUpdateInterval.setEnabled(!rbTimeBasedUpatePolicy.get(Policy.NEVER).isSelected());
|
---|
248 | tfUpdateInterval.setEnabled(!rbTimeBasedUpatePolicy.get(Policy.NEVER).isSelected());
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | }
|
---|