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

Last change on this file since 3083 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

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