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