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

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

fix #19734 - Autofilter: clicking active rule/key opens GPS settings instead of OSM settings

Extract GPXPreference from DrawingPreference

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