source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceTabbedPane.java@ 12005

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

fix #14667 - IOOBE at PreferenceTabbedPane.stateChanged

  • Property svn:eol-style set to native
File size: 25.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.Font;
8import java.awt.GridBagLayout;
9import java.awt.event.MouseWheelEvent;
10import java.awt.event.MouseWheelListener;
11import java.util.ArrayList;
12import java.util.Collection;
13import java.util.HashSet;
14import java.util.Iterator;
15import java.util.LinkedList;
16import java.util.List;
17import java.util.Set;
18
19import javax.swing.BorderFactory;
20import javax.swing.Icon;
21import javax.swing.ImageIcon;
22import javax.swing.JLabel;
23import javax.swing.JOptionPane;
24import javax.swing.JPanel;
25import javax.swing.JScrollPane;
26import javax.swing.JTabbedPane;
27import javax.swing.SwingUtilities;
28import javax.swing.event.ChangeEvent;
29import javax.swing.event.ChangeListener;
30
31import org.openstreetmap.josm.Main;
32import org.openstreetmap.josm.actions.ExpertToggleAction;
33import org.openstreetmap.josm.actions.ExpertToggleAction.ExpertModeChangeListener;
34import org.openstreetmap.josm.actions.RestartAction;
35import org.openstreetmap.josm.gui.HelpAwareOptionPane;
36import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
37import org.openstreetmap.josm.gui.preferences.advanced.AdvancedPreference;
38import org.openstreetmap.josm.gui.preferences.audio.AudioPreference;
39import org.openstreetmap.josm.gui.preferences.display.ColorPreference;
40import org.openstreetmap.josm.gui.preferences.display.DisplayPreference;
41import org.openstreetmap.josm.gui.preferences.display.DrawingPreference;
42import org.openstreetmap.josm.gui.preferences.display.LafPreference;
43import org.openstreetmap.josm.gui.preferences.display.LanguagePreference;
44import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
45import org.openstreetmap.josm.gui.preferences.map.BackupPreference;
46import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
47import org.openstreetmap.josm.gui.preferences.map.MapPreference;
48import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
49import org.openstreetmap.josm.gui.preferences.plugin.PluginPreference;
50import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
51import org.openstreetmap.josm.gui.preferences.remotecontrol.RemoteControlPreference;
52import org.openstreetmap.josm.gui.preferences.server.AuthenticationPreference;
53import org.openstreetmap.josm.gui.preferences.server.OverpassServerPreference;
54import org.openstreetmap.josm.gui.preferences.server.ProxyPreference;
55import org.openstreetmap.josm.gui.preferences.server.ServerAccessPreference;
56import org.openstreetmap.josm.gui.preferences.shortcut.ShortcutPreference;
57import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
58import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
59import org.openstreetmap.josm.gui.preferences.validator.ValidatorTestsPreference;
60import org.openstreetmap.josm.plugins.PluginDownloadTask;
61import org.openstreetmap.josm.plugins.PluginHandler;
62import org.openstreetmap.josm.plugins.PluginInformation;
63import org.openstreetmap.josm.tools.CheckParameterUtil;
64import org.openstreetmap.josm.tools.GBC;
65import org.openstreetmap.josm.tools.ImageProvider;
66import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
67
68/**
69 * The preference settings.
70 *
71 * @author imi
72 */
73public final class PreferenceTabbedPane extends JTabbedPane implements MouseWheelListener, ExpertModeChangeListener, ChangeListener {
74
75 private final class PluginDownloadAfterTask implements Runnable {
76 private final PluginPreference preference;
77 private final PluginDownloadTask task;
78 private final Set<PluginInformation> toDownload;
79
80 private PluginDownloadAfterTask(PluginPreference preference, PluginDownloadTask task,
81 Set<PluginInformation> toDownload) {
82 this.preference = preference;
83 this.task = task;
84 this.toDownload = toDownload;
85 }
86
87 @Override
88 public void run() {
89 boolean requiresRestart = false;
90
91 for (PreferenceSetting setting : settingsInitialized) {
92 if (setting.ok()) {
93 requiresRestart = true;
94 }
95 }
96
97 // build the messages. We only display one message, including the status information from the plugin download task
98 // and - if necessary - a hint to restart JOSM
99 //
100 StringBuilder sb = new StringBuilder();
101 sb.append("<html>");
102 if (task != null && !task.isCanceled()) {
103 PluginHandler.refreshLocalUpdatedPluginInfo(task.getDownloadedPlugins());
104 sb.append(PluginPreference.buildDownloadSummary(task));
105 }
106 if (requiresRestart) {
107 sb.append(tr("You have to restart JOSM for some settings to take effect."));
108 sb.append("<br/><br/>");
109 sb.append(tr("Would you like to restart now?"));
110 }
111 sb.append("</html>");
112
113 // display the message, if necessary
114 //
115 if (requiresRestart) {
116 final ButtonSpec[] options = RestartAction.getButtonSpecs();
117 if (0 == HelpAwareOptionPane.showOptionDialog(
118 Main.parent,
119 sb.toString(),
120 tr("Restart"),
121 JOptionPane.INFORMATION_MESSAGE,
122 null, /* no special icon */
123 options,
124 options[0],
125 null /* no special help */
126 )) {
127 Main.main.menu.restart.actionPerformed(null);
128 }
129 } else if (task != null && !task.isCanceled()) {
130 JOptionPane.showMessageDialog(
131 Main.parent,
132 sb.toString(),
133 tr("Warning"),
134 JOptionPane.WARNING_MESSAGE
135 );
136 }
137
138 // load the plugins that can be loaded at runtime
139 List<PluginInformation> newPlugins = preference.getNewlyActivatedPlugins();
140 if (newPlugins != null) {
141 Collection<PluginInformation> downloadedPlugins = null;
142 if (task != null && !task.isCanceled()) {
143 downloadedPlugins = task.getDownloadedPlugins();
144 }
145 List<PluginInformation> toLoad = new ArrayList<>();
146 for (PluginInformation pi : newPlugins) {
147 if (toDownload.contains(pi) && downloadedPlugins != null && !downloadedPlugins.contains(pi)) {
148 continue; // failed download
149 }
150 if (pi.canloadatruntime) {
151 toLoad.add(pi);
152 }
153 }
154 // check if plugin dependences can also be loaded
155 Collection<PluginInformation> allPlugins = new HashSet<>(toLoad);
156 allPlugins.addAll(PluginHandler.getPlugins());
157 boolean removed;
158 do {
159 removed = false;
160 Iterator<PluginInformation> it = toLoad.iterator();
161 while (it.hasNext()) {
162 if (!PluginHandler.checkRequiredPluginsPreconditions(null, allPlugins, it.next(), requiresRestart)) {
163 it.remove();
164 removed = true;
165 }
166 }
167 } while (removed);
168
169 if (!toLoad.isEmpty()) {
170 PluginHandler.loadPlugins(PreferenceTabbedPane.this, toLoad, null);
171 }
172 }
173
174 Main.parent.repaint();
175 }
176 }
177
178 /**
179 * Allows PreferenceSettings to do validation of entered values when ok was pressed.
180 * If data is invalid then event can return false to cancel closing of preferences dialog.
181 * @since 10600 (functional interface)
182 */
183 @FunctionalInterface
184 public interface ValidationListener {
185 /**
186 *
187 * @return True if preferences can be saved
188 */
189 boolean validatePreferences();
190 }
191
192 private interface PreferenceTab {
193 TabPreferenceSetting getTabPreferenceSetting();
194
195 Component getComponent();
196 }
197
198 public static final class PreferencePanel extends JPanel implements PreferenceTab {
199 private final transient TabPreferenceSetting preferenceSetting;
200
201 private PreferencePanel(TabPreferenceSetting preferenceSetting) {
202 super(new GridBagLayout());
203 CheckParameterUtil.ensureParameterNotNull(preferenceSetting);
204 this.preferenceSetting = preferenceSetting;
205 buildPanel();
206 }
207
208 private void buildPanel() {
209 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
210 add(new JLabel(preferenceSetting.getTitle()), GBC.eol().insets(0, 5, 0, 10).anchor(GBC.NORTHWEST));
211
212 JLabel descLabel = new JLabel("<html>"+preferenceSetting.getDescription()+"</html>");
213 descLabel.setFont(descLabel.getFont().deriveFont(Font.ITALIC));
214 add(descLabel, GBC.eol().insets(5, 0, 5, 20).fill(GBC.HORIZONTAL));
215 }
216
217 @Override
218 public TabPreferenceSetting getTabPreferenceSetting() {
219 return preferenceSetting;
220 }
221
222 @Override
223 public Component getComponent() {
224 return this;
225 }
226 }
227
228 public static final class PreferenceScrollPane extends JScrollPane implements PreferenceTab {
229 private final transient TabPreferenceSetting preferenceSetting;
230
231 private PreferenceScrollPane(Component view, TabPreferenceSetting preferenceSetting) {
232 super(view);
233 this.preferenceSetting = preferenceSetting;
234 }
235
236 private PreferenceScrollPane(PreferencePanel preferencePanel) {
237 this(preferencePanel.getComponent(), preferencePanel.getTabPreferenceSetting());
238 }
239
240 @Override
241 public TabPreferenceSetting getTabPreferenceSetting() {
242 return preferenceSetting;
243 }
244
245 @Override
246 public Component getComponent() {
247 return this;
248 }
249 }
250
251 // all created tabs
252 private final transient List<PreferenceTab> tabs = new ArrayList<>();
253 private static final Collection<PreferenceSettingFactory> settingsFactories = new LinkedList<>();
254 private static final PreferenceSettingFactory advancedPreferenceFactory = new AdvancedPreference.Factory();
255 private final transient List<PreferenceSetting> settings = new ArrayList<>();
256
257 // distinct list of tabs that have been initialized (we do not initialize tabs until they are displayed to speed up dialog startup)
258 private final transient List<PreferenceSetting> settingsInitialized = new ArrayList<>();
259
260 final transient List<ValidationListener> validationListeners = new ArrayList<>();
261
262 /**
263 * Add validation listener to currently open preferences dialog. Calling to removeValidationListener is not necessary, all listeners will
264 * be automatically removed when dialog is closed
265 * @param validationListener validation listener to add
266 */
267 public void addValidationListener(ValidationListener validationListener) {
268 validationListeners.add(validationListener);
269 }
270
271 /**
272 * Construct a PreferencePanel for the preference settings. Layout is GridBagLayout
273 * and a centered title label and the description are added.
274 * @param caller Preference settings, that display a top level tab
275 * @return The created panel ready to add other controls.
276 */
277 public PreferencePanel createPreferenceTab(TabPreferenceSetting caller) {
278 return createPreferenceTab(caller, false);
279 }
280
281 /**
282 * Construct a PreferencePanel for the preference settings. Layout is GridBagLayout
283 * and a centered title label and the description are added.
284 * @param caller Preference settings, that display a top level tab
285 * @param inScrollPane if <code>true</code> the added tab will show scroll bars
286 * if the panel content is larger than the available space
287 * @return The created panel ready to add other controls.
288 */
289 public PreferencePanel createPreferenceTab(TabPreferenceSetting caller, boolean inScrollPane) {
290 CheckParameterUtil.ensureParameterNotNull(caller, "caller");
291 PreferencePanel p = new PreferencePanel(caller);
292
293 PreferenceTab tab = p;
294 if (inScrollPane) {
295 PreferenceScrollPane sp = new PreferenceScrollPane(p);
296 tab = sp;
297 }
298 tabs.add(tab);
299 return p;
300 }
301
302 @FunctionalInterface
303 private interface TabIdentifier {
304 boolean identify(TabPreferenceSetting tps, Object param);
305 }
306
307 private void selectTabBy(TabIdentifier method, Object param) {
308 for (int i = 0; i < getTabCount(); i++) {
309 Component c = getComponentAt(i);
310 if (c instanceof PreferenceTab) {
311 PreferenceTab tab = (PreferenceTab) c;
312 if (method.identify(tab.getTabPreferenceSetting(), param)) {
313 setSelectedIndex(i);
314 return;
315 }
316 }
317 }
318 }
319
320 public void selectTabByName(String name) {
321 selectTabBy((tps, name1) -> name1 != null && tps != null && tps.getIconName() != null && name1.equals(tps.getIconName()), name);
322 }
323
324 public void selectTabByPref(Class<? extends TabPreferenceSetting> clazz) {
325 selectTabBy((tps, clazz1) -> tps.getClass().isAssignableFrom((Class<?>) clazz1), clazz);
326 }
327
328 public boolean selectSubTabByPref(Class<? extends SubPreferenceSetting> clazz) {
329 for (PreferenceSetting setting : settings) {
330 if (clazz.isInstance(setting)) {
331 final SubPreferenceSetting sub = (SubPreferenceSetting) setting;
332 final TabPreferenceSetting tab = sub.getTabPreferenceSetting(this);
333 selectTabBy((tps, unused) -> tps.equals(tab), null);
334 return tab.selectSubTab(sub);
335 }
336 }
337 return false;
338 }
339
340 /**
341 * Returns the {@code DisplayPreference} object.
342 * @return the {@code DisplayPreference} object.
343 */
344 public DisplayPreference getDisplayPreference() {
345 return getSetting(DisplayPreference.class);
346 }
347
348 /**
349 * Returns the {@code MapPreference} object.
350 * @return the {@code MapPreference} object.
351 */
352 public MapPreference getMapPreference() {
353 return getSetting(MapPreference.class);
354 }
355
356 /**
357 * Returns the {@code PluginPreference} object.
358 * @return the {@code PluginPreference} object.
359 */
360 public PluginPreference getPluginPreference() {
361 return getSetting(PluginPreference.class);
362 }
363
364 /**
365 * Returns the {@code ImageryPreference} object.
366 * @return the {@code ImageryPreference} object.
367 */
368 public ImageryPreference getImageryPreference() {
369 return getSetting(ImageryPreference.class);
370 }
371
372 /**
373 * Returns the {@code ShortcutPreference} object.
374 * @return the {@code ShortcutPreference} object.
375 */
376 public ShortcutPreference getShortcutPreference() {
377 return getSetting(ShortcutPreference.class);
378 }
379
380 /**
381 * Returns the {@code ServerAccessPreference} object.
382 * @return the {@code ServerAccessPreference} object.
383 * @since 6523
384 */
385 public ServerAccessPreference getServerPreference() {
386 return getSetting(ServerAccessPreference.class);
387 }
388
389 /**
390 * Returns the {@code ValidatorPreference} object.
391 * @return the {@code ValidatorPreference} object.
392 * @since 6665
393 */
394 public ValidatorPreference getValidatorPreference() {
395 return getSetting(ValidatorPreference.class);
396 }
397
398 /**
399 * Saves preferences.
400 */
401 public void savePreferences() {
402 // create a task for downloading plugins if the user has activated, yet not downloaded, new plugins
403 final PluginPreference preference = getPluginPreference();
404 if (preference != null) {
405 final Set<PluginInformation> toDownload = preference.getPluginsScheduledForUpdateOrDownload();
406 final PluginDownloadTask task;
407 if (toDownload != null && !toDownload.isEmpty()) {
408 task = new PluginDownloadTask(this, toDownload, tr("Download plugins"));
409 } else {
410 task = null;
411 }
412
413 // this is the task which will run *after* the plugins are downloaded
414 final Runnable continuation = new PluginDownloadAfterTask(preference, task, toDownload);
415
416 if (task != null) {
417 // if we have to launch a plugin download task we do it asynchronously, followed
418 // by the remaining "save preferences" activites run on the Swing EDT.
419 Main.worker.submit(task);
420 Main.worker.submit(() -> SwingUtilities.invokeLater(continuation));
421 } else {
422 // no need for asynchronous activities. Simply run the remaining "save preference"
423 // activities on this thread (we are already on the Swing EDT
424 continuation.run();
425 }
426 }
427 }
428
429 /**
430 * If the dialog is closed with Ok, the preferences will be stored to the preferences-
431 * file, otherwise no change of the file happens.
432 */
433 public PreferenceTabbedPane() {
434 super(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
435 super.addMouseWheelListener(this);
436 super.getModel().addChangeListener(this);
437 ExpertToggleAction.addExpertModeChangeListener(this);
438 }
439
440 public void buildGui() {
441 Collection<PreferenceSettingFactory> factories = new ArrayList<>(settingsFactories);
442 factories.addAll(PluginHandler.getPreferenceSetting());
443 factories.add(advancedPreferenceFactory);
444
445 for (PreferenceSettingFactory factory : factories) {
446 if (factory != null) {
447 PreferenceSetting setting = factory.createPreferenceSetting();
448 if (setting != null) {
449 settings.add(setting);
450 }
451 }
452 }
453 addGUITabs(false);
454 }
455
456 private void addGUITabsForSetting(Icon icon, TabPreferenceSetting tps) {
457 for (PreferenceTab tab : tabs) {
458 if (tab.getTabPreferenceSetting().equals(tps)) {
459 insertGUITabsForSetting(icon, tps, getTabCount());
460 }
461 }
462 }
463
464 private int insertGUITabsForSetting(Icon icon, TabPreferenceSetting tps, int index) {
465 int position = index;
466 for (PreferenceTab tab : tabs) {
467 if (tab.getTabPreferenceSetting().equals(tps)) {
468 insertTab(null, icon, tab.getComponent(), tps.getTooltip(), position++);
469 }
470 }
471 return position - 1;
472 }
473
474 private void addGUITabs(boolean clear) {
475 boolean expert = ExpertToggleAction.isExpert();
476 Component sel = getSelectedComponent();
477 if (clear) {
478 removeAll();
479 }
480 // Inspect each tab setting
481 for (PreferenceSetting setting : settings) {
482 if (setting instanceof TabPreferenceSetting) {
483 TabPreferenceSetting tps = (TabPreferenceSetting) setting;
484 if (expert || !tps.isExpert()) {
485 // Get icon
486 String iconName = tps.getIconName();
487 ImageIcon icon = null;
488
489 if (iconName != null && !iconName.isEmpty()) {
490 icon = ImageProvider.get("preferences", iconName, ImageProvider.ImageSizes.SETTINGS_TAB);
491 }
492 if (settingsInitialized.contains(tps)) {
493 // If it has been initialized, add corresponding tab(s)
494 addGUITabsForSetting(icon, tps);
495 } else {
496 // If it has not been initialized, create an empty tab with only icon and tooltip
497 addTab(null, icon, new PreferencePanel(tps), tps.getTooltip());
498 }
499 }
500 } else if (!(setting instanceof SubPreferenceSetting)) {
501 Main.warn("Ignoring preferences "+setting);
502 }
503 }
504 try {
505 if (sel != null) {
506 setSelectedComponent(sel);
507 }
508 } catch (IllegalArgumentException e) {
509 Main.warn(e);
510 }
511 }
512
513 @Override
514 public void expertChanged(boolean isExpert) {
515 addGUITabs(true);
516 }
517
518 public List<PreferenceSetting> getSettings() {
519 return settings;
520 }
521
522 @SuppressWarnings("unchecked")
523 public <T> T getSetting(Class<? extends T> clazz) {
524 for (PreferenceSetting setting:settings) {
525 if (clazz.isAssignableFrom(setting.getClass()))
526 return (T) setting;
527 }
528 return null;
529 }
530
531 static {
532 // order is important!
533 settingsFactories.add(new DisplayPreference.Factory());
534 settingsFactories.add(new DrawingPreference.Factory());
535 settingsFactories.add(new ColorPreference.Factory());
536 settingsFactories.add(new LafPreference.Factory());
537 settingsFactories.add(new LanguagePreference.Factory());
538 settingsFactories.add(new ServerAccessPreference.Factory());
539 settingsFactories.add(new AuthenticationPreference.Factory());
540 settingsFactories.add(new ProxyPreference.Factory());
541 settingsFactories.add(new OverpassServerPreference.Factory());
542 settingsFactories.add(new MapPreference.Factory());
543 settingsFactories.add(new ProjectionPreference.Factory());
544 settingsFactories.add(new MapPaintPreference.Factory());
545 settingsFactories.add(new TaggingPresetPreference.Factory());
546 settingsFactories.add(new BackupPreference.Factory());
547 settingsFactories.add(new PluginPreference.Factory());
548 settingsFactories.add(Main.toolbar);
549 settingsFactories.add(new AudioPreference.Factory());
550 settingsFactories.add(new ShortcutPreference.Factory());
551 settingsFactories.add(new ValidatorPreference.Factory());
552 settingsFactories.add(new ValidatorTestsPreference.Factory());
553 settingsFactories.add(new ValidatorTagCheckerRulesPreference.Factory());
554 settingsFactories.add(new RemoteControlPreference.Factory());
555 settingsFactories.add(new ImageryPreference.Factory());
556 }
557
558 /**
559 * This mouse wheel listener reacts when a scroll is carried out over the
560 * tab strip and scrolls one tab/down or up, selecting it immediately.
561 */
562 @Override
563 public void mouseWheelMoved(MouseWheelEvent wev) {
564 // Ensure the cursor is over the tab strip
565 if (super.indexAtLocation(wev.getPoint().x, wev.getPoint().y) < 0)
566 return;
567
568 // Get currently selected tab
569 int newTab = super.getSelectedIndex() + wev.getWheelRotation();
570
571 // Ensure the new tab index is sound
572 newTab = newTab < 0 ? 0 : newTab;
573 newTab = newTab >= super.getTabCount() ? super.getTabCount() - 1 : newTab;
574
575 // select new tab
576 super.setSelectedIndex(newTab);
577 }
578
579 @Override
580 public void stateChanged(ChangeEvent e) {
581 int index = getSelectedIndex();
582 Component sel = getSelectedComponent();
583 if (index > -1 && sel instanceof PreferenceTab) {
584 PreferenceTab tab = (PreferenceTab) sel;
585 TabPreferenceSetting preferenceSettings = tab.getTabPreferenceSetting();
586 if (!settingsInitialized.contains(preferenceSettings)) {
587 try {
588 getModel().removeChangeListener(this);
589 preferenceSettings.addGui(this);
590 // Add GUI for sub preferences
591 for (PreferenceSetting setting : settings) {
592 if (setting instanceof SubPreferenceSetting) {
593 addSubPreferenceSetting(preferenceSettings, (SubPreferenceSetting) setting);
594 }
595 }
596 Icon icon = getIconAt(index);
597 remove(index);
598 if (index <= insertGUITabsForSetting(icon, preferenceSettings, index)) {
599 setSelectedIndex(index);
600 }
601 } catch (SecurityException ex) {
602 Main.error(ex);
603 } catch (RuntimeException ex) { // NOPMD
604 // allow to change most settings even if e.g. a plugin fails
605 BugReportExceptionHandler.handleException(ex);
606 } finally {
607 settingsInitialized.add(preferenceSettings);
608 getModel().addChangeListener(this);
609 }
610 }
611 }
612 }
613
614 private void addSubPreferenceSetting(TabPreferenceSetting preferenceSettings, SubPreferenceSetting sps) {
615 if (sps.getTabPreferenceSetting(this) == preferenceSettings) {
616 try {
617 sps.addGui(this);
618 } catch (SecurityException ex) {
619 Main.error(ex);
620 } catch (RuntimeException ex) { // NOPMD
621 BugReportExceptionHandler.handleException(ex);
622 } finally {
623 settingsInitialized.add(sps);
624 }
625 }
626 }
627}
Note: See TracBrowser for help on using the repository browser.