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

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

see #7548 - Re-organize the preference dialog (connection preferences)

  • Property svn:eol-style set to native
File size: 27.4 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.GPXPreference;
48import org.openstreetmap.josm.gui.preferences.display.LafPreference;
49import org.openstreetmap.josm.gui.preferences.display.LanguagePreference;
50import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
51import org.openstreetmap.josm.gui.preferences.map.BackupPreference;
52import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
53import org.openstreetmap.josm.gui.preferences.map.MapPreference;
54import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
55import org.openstreetmap.josm.gui.preferences.plugin.PluginPreference;
56import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
57import org.openstreetmap.josm.gui.preferences.remotecontrol.RemoteControlPreference;
58import org.openstreetmap.josm.gui.preferences.server.ProxyPreference;
59import org.openstreetmap.josm.gui.preferences.server.ServerAccessPreference;
60import org.openstreetmap.josm.gui.preferences.shortcut.ShortcutPreference;
61import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
62import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
63import org.openstreetmap.josm.gui.preferences.validator.ValidatorTestsPreference;
64import org.openstreetmap.josm.gui.util.GuiHelper;
65import org.openstreetmap.josm.plugins.PluginDownloadTask;
66import org.openstreetmap.josm.plugins.PluginHandler;
67import org.openstreetmap.josm.plugins.PluginInformation;
68import org.openstreetmap.josm.tools.CheckParameterUtil;
69import org.openstreetmap.josm.tools.GBC;
70import org.openstreetmap.josm.tools.ImageProvider;
71import org.openstreetmap.josm.tools.Logging;
72import org.openstreetmap.josm.tools.Pair;
73import org.openstreetmap.josm.tools.Utils;
74import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
75
76/**
77 * The preference settings.
78 *
79 * @author imi
80 */
81public final class PreferenceTabbedPane extends JTabbedPane implements ExpertModeChangeListener, ChangeListener {
82
83 private final class PluginDownloadAfterTask implements Runnable {
84 private final PluginPreference preference;
85 private final PluginDownloadTask task;
86 private final Set<PluginInformation> toDownload;
87
88 private PluginDownloadAfterTask(PluginPreference preference, PluginDownloadTask task,
89 Set<PluginInformation> toDownload) {
90 this.preference = preference;
91 this.task = task;
92 this.toDownload = toDownload;
93 }
94
95 @Override
96 public void run() {
97 boolean requiresRestart = false;
98
99 for (PreferenceSetting setting : settingsInitialized) {
100 if (setting.ok()) {
101 requiresRestart = true;
102 }
103 }
104
105 // build the messages. We only display one message, including the status information from the plugin download task
106 // and - if necessary - a hint to restart JOSM
107 //
108 StringBuilder sb = new StringBuilder();
109 sb.append("<html>");
110 if (task != null && !task.isCanceled()) {
111 PluginHandler.refreshLocalUpdatedPluginInfo(task.getDownloadedPlugins());
112 sb.append(PluginPreference.buildDownloadSummary(task));
113 }
114 if (requiresRestart) {
115 sb.append(tr("You have to restart JOSM for some settings to take effect."));
116 sb.append("<br/><br/>");
117 sb.append(tr("Would you like to restart now?"));
118 }
119 sb.append("</html>");
120
121 // display the message, if necessary
122 //
123 if (requiresRestart) {
124 final ButtonSpec[] options = RestartAction.getButtonSpecs();
125 if (0 == HelpAwareOptionPane.showOptionDialog(
126 MainApplication.getMainFrame(),
127 sb.toString(),
128 tr("Restart"),
129 JOptionPane.INFORMATION_MESSAGE,
130 null, /* no special icon */
131 options,
132 options[0],
133 null /* no special help */
134 )) {
135 MainApplication.getMenu().restart.actionPerformed(null);
136 }
137 } else if (task != null && !task.isCanceled()) {
138 JOptionPane.showMessageDialog(
139 MainApplication.getMainFrame(),
140 sb.toString(),
141 tr("Warning"),
142 JOptionPane.WARNING_MESSAGE
143 );
144 }
145
146 // load the plugins that can be loaded at runtime
147 List<PluginInformation> newPlugins = preference.getNewlyActivatedPlugins();
148 if (newPlugins != null) {
149 Collection<PluginInformation> downloadedPlugins = null;
150 if (task != null && !task.isCanceled()) {
151 downloadedPlugins = task.getDownloadedPlugins();
152 }
153 List<PluginInformation> toLoad = new ArrayList<>();
154 for (PluginInformation pi : newPlugins) {
155 if (toDownload.contains(pi) && downloadedPlugins != null && !downloadedPlugins.contains(pi)) {
156 continue; // failed download
157 }
158 if (pi.canloadatruntime) {
159 toLoad.add(pi);
160 }
161 }
162 // check if plugin dependencies can also be loaded
163 Collection<PluginInformation> allPlugins = new HashSet<>(toLoad);
164 allPlugins.addAll(PluginHandler.getPlugins());
165 boolean removed;
166 do {
167 removed = false;
168 Iterator<PluginInformation> it = toLoad.iterator();
169 while (it.hasNext()) {
170 if (!PluginHandler.checkRequiredPluginsPreconditions(null, allPlugins, it.next(), requiresRestart)) {
171 it.remove();
172 removed = true;
173 }
174 }
175 } while (removed);
176
177 if (!toLoad.isEmpty()) {
178 PluginHandler.loadPlugins(PreferenceTabbedPane.this, toLoad, null);
179 }
180 }
181
182 if (MainApplication.getMainFrame() != null) {
183 MainApplication.getMainFrame().repaint();
184 }
185 }
186 }
187
188 /**
189 * Allows PreferenceSettings to do validation of entered values when ok was pressed.
190 * If data is invalid then event can return false to cancel closing of preferences dialog.
191 * @since 10600 (functional interface)
192 */
193 @FunctionalInterface
194 public interface ValidationListener {
195 /**
196 * Determines if preferences can be saved.
197 * @return True if preferences can be saved
198 */
199 boolean validatePreferences();
200 }
201
202 private interface PreferenceTab {
203 TabPreferenceSetting getTabPreferenceSetting();
204
205 Component getComponent();
206 }
207
208 public static final class PreferencePanel extends JPanel implements PreferenceTab {
209 private final transient TabPreferenceSetting preferenceSetting;
210
211 private PreferencePanel(TabPreferenceSetting preferenceSetting) {
212 super(new GridBagLayout());
213 CheckParameterUtil.ensureParameterNotNull(preferenceSetting);
214 this.preferenceSetting = preferenceSetting;
215 buildPanel();
216 }
217
218 private void buildPanel() {
219 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
220 add(buildHtmlPanel(preferenceSetting.getTitle(), Font.BOLD),
221 GBC.eol().insets(0, 5, 0, 10).anchor(GBC.NORTHWEST).fill(GBC.HORIZONTAL));
222 add(buildHtmlPanel(preferenceSetting.getDescription(), Font.ITALIC),
223 GBC.eol().insets(5, 0, 5, 20).fill(GBC.HORIZONTAL));
224 }
225
226 private static JLabel buildHtmlPanel(String text, int fontStyle) {
227 JLabel label = new JLabel("<html>" + text + "</html>");
228 label.setFont(label.getFont().deriveFont(fontStyle));
229 return label;
230 }
231
232 @Override
233 public TabPreferenceSetting getTabPreferenceSetting() {
234 return preferenceSetting;
235 }
236
237 @Override
238 public Component getComponent() {
239 return this;
240 }
241 }
242
243 public static final class PreferenceScrollPane extends JScrollPane implements PreferenceTab {
244 private final transient TabPreferenceSetting preferenceSetting;
245
246 private PreferenceScrollPane(Component view, TabPreferenceSetting preferenceSetting) {
247 super(view);
248 this.preferenceSetting = preferenceSetting;
249 }
250
251 private PreferenceScrollPane(PreferencePanel preferencePanel) {
252 this(preferencePanel.getComponent(), preferencePanel.getTabPreferenceSetting());
253 GuiHelper.setDefaultIncrement(this);
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) && tps.getIconName() != null) {
502 insertTab(null, icon, tab.getComponent(), tps.getTooltip(), position++);
503 } else if (tab.getTabPreferenceSetting().equals(tps)) {
504 insertTab(tps.getTitle(), null, tab.getComponent(), tps.getTooltip(), position++);
505 }
506 }
507 return position - 1;
508 }
509
510 private void addGUITabs(boolean clear) {
511 boolean expert = ExpertToggleAction.isExpert();
512 Component sel = getSelectedComponent();
513 if (clear) {
514 removeAll();
515 }
516 // Inspect each tab setting
517 for (PreferenceSetting setting : settings) {
518 if (setting instanceof TabPreferenceSetting) {
519 TabPreferenceSetting tps = (TabPreferenceSetting) setting;
520 if (expert || !tps.isExpert()) {
521 // Get icon
522 String iconName = tps.getIconName();
523 ImageIcon icon = null;
524
525 if (iconName != null && !iconName.isEmpty()) {
526 icon = ImageProvider.get("preferences", iconName, ImageProvider.ImageSizes.SETTINGS_TAB);
527 }
528 if (settingsInitialized.contains(tps)) {
529 // If it has been initialized, add corresponding tab(s)
530 addGUITabsForSetting(icon, tps);
531 } else if (tps.getIconName() != null) {
532 // If it has not been initialized, create an empty tab with only icon and tooltip
533 addTab(null, icon, new PreferencePanel(tps), tps.getTooltip());
534 } else {
535 addTab(tps.getTitle(), null, new PreferencePanel(tps), tps.getTooltip());
536 }
537 }
538 } else if (!(setting instanceof SubPreferenceSetting)) {
539 Logging.warn("Ignoring preferences "+setting);
540 }
541 }
542 if (sel != null) {
543 int index = indexOfComponent(sel);
544 if (index > -1) {
545 setSelectedIndex(index);
546 }
547 }
548 }
549
550 @Override
551 public void expertChanged(boolean isExpert) {
552 addGUITabs(true);
553 }
554
555 /**
556 * Returns a list of all preferences settings
557 * @return a list of all preferences settings
558 */
559 public List<PreferenceSetting> getSettings() {
560 return Collections.unmodifiableList(settings);
561 }
562
563 /**
564 * Returns the preferences setting for the given class
565 * @param clazz the preference setting class
566 * @param <T> the preference setting type
567 * @return the preferences setting for the given class
568 * @throws NoSuchElementException if there is no such value
569 */
570 public <T extends PreferenceSetting> T getSetting(Class<? extends T> clazz) {
571 return Utils.filteredCollection(settings, clazz).iterator().next();
572 }
573
574 static {
575 // order is important!
576 SETTINGS_FACTORIES.add(new DisplayPreference.Factory());
577 SETTINGS_FACTORIES.add(new DrawingPreference.Factory());
578 SETTINGS_FACTORIES.add(new GPXPreference.Factory());
579 SETTINGS_FACTORIES.add(new ColorPreference.Factory());
580 SETTINGS_FACTORIES.add(new LafPreference.Factory());
581 SETTINGS_FACTORIES.add(new LanguagePreference.Factory());
582
583 SETTINGS_FACTORIES.add(new ServerAccessPreference.Factory());
584 SETTINGS_FACTORIES.add(new ProxyPreference.Factory());
585 SETTINGS_FACTORIES.add(new MapPreference.Factory());
586 SETTINGS_FACTORIES.add(new ProjectionPreference.Factory());
587 SETTINGS_FACTORIES.add(new MapPaintPreference.Factory());
588 SETTINGS_FACTORIES.add(new TaggingPresetPreference.Factory());
589 SETTINGS_FACTORIES.add(new BackupPreference.Factory());
590 SETTINGS_FACTORIES.add(new PluginPreference.Factory());
591 SETTINGS_FACTORIES.add(MainApplication.getToolbar());
592 SETTINGS_FACTORIES.add(new AudioPreference.Factory());
593 SETTINGS_FACTORIES.add(new ShortcutPreference.Factory());
594 SETTINGS_FACTORIES.add(new ValidatorPreference.Factory());
595 SETTINGS_FACTORIES.add(new ValidatorTestsPreference.Factory());
596 SETTINGS_FACTORIES.add(new ValidatorTagCheckerRulesPreference.Factory());
597 SETTINGS_FACTORIES.add(new RemoteControlPreference.Factory());
598 SETTINGS_FACTORIES.add(new ImageryPreference.Factory());
599 }
600
601 /**
602 * This mouse wheel listener reacts when a scroll is carried out over the
603 * tab strip and scrolls one tab/down or up, selecting it immediately.
604 */
605 static final class WheelListener implements MouseWheelListener {
606
607 final JTabbedPane tabbedPane;
608
609 WheelListener(JTabbedPane tabbedPane) {
610 this.tabbedPane = tabbedPane;
611 }
612
613 @Override
614 public void mouseWheelMoved(MouseWheelEvent wev) {
615 // Ensure the cursor is over the tab strip
616 if (tabbedPane.indexAtLocation(wev.getPoint().x, wev.getPoint().y) < 0)
617 return;
618
619 // Get currently selected tab && ensure the new tab index is sound
620 int newTab = Utils.clamp(tabbedPane.getSelectedIndex() + wev.getWheelRotation(),
621 0, tabbedPane.getTabCount() - 1);
622
623 tabbedPane.setSelectedIndex(newTab);
624 }
625 }
626
627 @Override
628 public void stateChanged(ChangeEvent e) {
629 int index = getSelectedIndex();
630 Component sel = getSelectedComponent();
631 if (index > -1 && sel instanceof PreferenceTab) {
632 PreferenceTab tab = (PreferenceTab) sel;
633 TabPreferenceSetting preferenceSettings = tab.getTabPreferenceSetting();
634 if (!settingsInitialized.contains(preferenceSettings)) {
635 try {
636 getModel().removeChangeListener(this);
637 preferenceSettings.addGui(this);
638 // Add GUI for sub preferences
639 for (PreferenceSetting setting : settings) {
640 if (setting instanceof SubPreferenceSetting) {
641 addSubPreferenceSetting(preferenceSettings, (SubPreferenceSetting) setting);
642 }
643 }
644 Icon icon = getIconAt(index);
645 remove(index);
646 if (index <= insertGUITabsForSetting(icon, preferenceSettings, index)) {
647 setSelectedIndex(index);
648 }
649 } catch (SecurityException ex) {
650 Logging.error(ex);
651 } catch (RuntimeException ex) { // NOPMD
652 // allow to change most settings even if e.g. a plugin fails
653 BugReportExceptionHandler.handleException(ex);
654 } finally {
655 settingsInitialized.add(preferenceSettings);
656 getModel().addChangeListener(this);
657 }
658 }
659 Container ancestor = getTopLevelAncestor();
660 if (ancestor instanceof PreferenceDialog) {
661 ((PreferenceDialog) ancestor).setHelpContext(preferenceSettings.getHelpContext());
662 }
663 }
664 }
665
666 private void addSubPreferenceSetting(TabPreferenceSetting preferenceSettings, SubPreferenceSetting sps) {
667 if (sps.getTabPreferenceSetting(this) == preferenceSettings) {
668 try {
669 sps.addGui(this);
670 } catch (SecurityException ex) {
671 Logging.error(ex);
672 } catch (RuntimeException ex) { // NOPMD
673 BugReportExceptionHandler.handleException(ex);
674 } finally {
675 settingsInitialized.add(sps);
676 }
677 }
678 }
679}
Note: See TracBrowser for help on using the repository browser.