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

Last change on this file since 16966 was 16966, checked in by simon04, 4 years ago

fix #19732 - Preferences: open previously selected preference tab

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