[3083] | 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;
|
---|
[8388] | 10 | import java.util.EnumMap;
|
---|
[8404] | 11 | import java.util.Locale;
|
---|
[3083] | 12 | import java.util.Map;
|
---|
[11553] | 13 | import java.util.Optional;
|
---|
[3083] | 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.Main;
|
---|
[6340] | 23 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
[8308] | 24 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
[3083] | 25 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
[5145] | 26 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
[12620] | 27 | import org.openstreetmap.josm.tools.Logging;
|
---|
[3083] | 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 {
|
---|
[9212] | 36 | ASK("ask"),
|
---|
[3083] | 37 | ALWAYS("always"),
|
---|
| 38 | NEVER("never");
|
---|
| 39 |
|
---|
[11489] | 40 | private final String preferenceValue;
|
---|
| 41 |
|
---|
[3083] | 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) {
|
---|
[10179] | 51 | if (preferenceValue == null)
|
---|
| 52 | return null;
|
---|
| 53 | String prefValue = preferenceValue.trim().toLowerCase(Locale.ENGLISH);
|
---|
[3083] | 54 | for (Policy p: Policy.values()) {
|
---|
[10179] | 55 | if (p.getPreferencesValue().equals(prefValue))
|
---|
[3083] | 56 | return p;
|
---|
| 57 | }
|
---|
| 58 | return null;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[8308] | 62 | private transient Map<Policy, JRadioButton> rbVersionBasedUpatePolicy;
|
---|
| 63 | private transient Map<Policy, JRadioButton> rbTimeBasedUpatePolicy;
|
---|
[10179] | 64 | private final JosmTextField tfUpdateInterval = new JosmTextField(5);
|
---|
| 65 | private final JLabel lblUpdateInterval = new JLabel(tr("Update interval (in days):"));
|
---|
[3083] | 66 |
|
---|
[10179] | 67 | /**
|
---|
| 68 | * Constructs a new {@code PluginUpdatePolicyPanel}.
|
---|
| 69 | */
|
---|
| 70 | public PluginUpdatePolicyPanel() {
|
---|
| 71 | build();
|
---|
| 72 | initFromPreferences();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[3083] | 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;
|
---|
[8510] | 80 | gc.weightx = 1.0;
|
---|
[3083] | 81 |
|
---|
[6267] | 82 | ButtonGroup bgVersionBasedUpdatePolicy = new ButtonGroup();
|
---|
[8388] | 83 | rbVersionBasedUpatePolicy = new EnumMap<>(Policy.class);
|
---|
[3083] | 84 | JRadioButton btn = new JRadioButton(tr("Ask before updating"));
|
---|
| 85 | rbVersionBasedUpatePolicy.put(Policy.ASK, btn);
|
---|
| 86 | bgVersionBasedUpdatePolicy.add(btn);
|
---|
| 87 |
|
---|
[4310] | 88 | btn = new JRadioButton(tr("Always update without asking"));
|
---|
[3083] | 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 |
|
---|
[8509] | 96 | JMultilineLabel lbl = new JMultilineLabel(
|
---|
| 97 | tr("Please decide whether JOSM shall automatically update active plugins at startup after an update of JOSM itself."));
|
---|
[8510] | 98 | gc.gridy = 0;
|
---|
[3083] | 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));
|
---|
[10179] | 109 | pnl.add(lblUpdateInterval);
|
---|
| 110 | pnl.add(tfUpdateInterval);
|
---|
[8426] | 111 | lblUpdateInterval.setLabelFor(tfUpdateInterval);
|
---|
[3083] | 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;
|
---|
[8510] | 121 | gc.weightx = 1.0;
|
---|
[3083] | 122 |
|
---|
| 123 | TimeBasedPolicyChangeListener changeListener = new TimeBasedPolicyChangeListener();
|
---|
| 124 |
|
---|
[6267] | 125 | ButtonGroup bgTimeBasedUpdatePolicy = new ButtonGroup();
|
---|
[8388] | 126 | rbTimeBasedUpatePolicy = new EnumMap<>(Policy.class);
|
---|
[3083] | 127 | JRadioButton btn = new JRadioButton(tr("Ask before updating"));
|
---|
| 128 | btn.addChangeListener(changeListener);
|
---|
| 129 | rbTimeBasedUpatePolicy.put(Policy.ASK, btn);
|
---|
| 130 | bgTimeBasedUpdatePolicy.add(btn);
|
---|
| 131 |
|
---|
[4310] | 132 | btn = new JRadioButton(tr("Always update without asking"));
|
---|
[3083] | 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 |
|
---|
[8426] | 142 | JMultilineLabel lbl = new JMultilineLabel(
|
---|
| 143 | tr("Please decide whether JOSM shall automatically update active plugins after a certain period of time."));
|
---|
[8510] | 144 | gc.gridy = 0;
|
---|
[3083] | 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 |
|
---|
[6890] | 155 | protected final void build() {
|
---|
[3083] | 156 | setLayout(new GridBagLayout());
|
---|
| 157 | GridBagConstraints gc = new GridBagConstraints();
|
---|
| 158 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
| 159 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
[8510] | 160 | gc.weightx = 1.0;
|
---|
| 161 | gc.insets = new Insets(5, 5, 10, 5);
|
---|
[3083] | 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 |
|
---|
[6267] | 173 | /**
|
---|
[3083] | 174 | * Loads the relevant preference values from the JOSM preferences
|
---|
| 175 | */
|
---|
[6890] | 176 | public final void initFromPreferences() {
|
---|
[11553] | 177 | rbVersionBasedUpatePolicy.get(Optional.ofNullable(Policy.fromPreferenceValue(
|
---|
| 178 | Main.pref.get("pluginmanager.version-based-update.policy", "ask"))).orElse(Policy.ASK)).setSelected(true);
|
---|
| 179 | rbTimeBasedUpatePolicy.get(Optional.ofNullable(Policy.fromPreferenceValue(
|
---|
| 180 | Main.pref.get("pluginmanager.time-based-update.policy", "ask"))).orElse(Policy.ASK)).setSelected(true);
|
---|
[3083] | 181 |
|
---|
[11553] | 182 | String pref = Main.pref.get("pluginmanager.warntime", null);
|
---|
[3083] | 183 | int days = 0;
|
---|
| 184 | if (pref != null) {
|
---|
| 185 | // remove legacy preference
|
---|
| 186 | Main.pref.put("pluginmanager.warntime", null);
|
---|
| 187 | try {
|
---|
[11553] | 188 | days = Integer.parseInt(pref.trim());
|
---|
[8510] | 189 | } catch (NumberFormatException e) {
|
---|
[3083] | 190 | // ignore - load from preference pluginmanager.time-based-update.interval
|
---|
[12620] | 191 | Logging.trace(e);
|
---|
[3083] | 192 | }
|
---|
| 193 | if (days <= 0) {
|
---|
[5145] | 194 | days = PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL;
|
---|
[3083] | 195 | }
|
---|
| 196 | }
|
---|
| 197 | if (days == 0) {
|
---|
[12841] | 198 | days = Main.pref.getInt("pluginmanager.time-based-update.interval", PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL);
|
---|
[3083] | 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) {
|
---|
[5145] | 232 | days = PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL;
|
---|
[3083] | 233 | }
|
---|
[8510] | 234 | } catch (NumberFormatException e) {
|
---|
[5145] | 235 | days = PluginHandler.DEFAULT_TIME_BASED_UPDATE_INTERVAL;
|
---|
[3083] | 236 | }
|
---|
[12841] | 237 | Main.pref.putInt("pluginmanager.time-based-update.interval", days);
|
---|
[3083] | 238 | }
|
---|
| 239 |
|
---|
| 240 | class TimeBasedPolicyChangeListener implements ChangeListener {
|
---|
[6084] | 241 | @Override
|
---|
[3083] | 242 | public void stateChanged(ChangeEvent e) {
|
---|
| 243 | lblUpdateInterval.setEnabled(!rbTimeBasedUpatePolicy.get(Policy.NEVER).isSelected());
|
---|
| 244 | tfUpdateInterval.setEnabled(!rbTimeBasedUpatePolicy.get(Policy.NEVER).isSelected());
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | }
|
---|