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