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

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

Sonar/FindBugs - Replace singular fields by local variables

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