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

Last change on this file since 17160 was 17160, 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.7 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.ConnectionPreference;
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 dependencies 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 GuiHelper.setDefaultIncrement(this);
256 }
257
258 @Override
259 public TabPreferenceSetting getTabPreferenceSetting() {
260 return preferenceSetting;
261 }
262
263 @Override
264 public Component getComponent() {
265 return this;
266 }
267 }
268
269 // all created tabs
270 private final transient List<PreferenceTab> tabs = new ArrayList<>();
271 private static final Collection<PreferenceSettingFactory> SETTINGS_FACTORIES = new LinkedList<>();
272 private static final PreferenceSettingFactory ADVANCED_PREFERENCE_FACTORY = new AdvancedPreference.Factory();
273 private final transient List<PreferenceSetting> settings = new ArrayList<>();
274
275 // distinct list of tabs that have been initialized (we do not initialize tabs until they are displayed to speed up dialog startup)
276 private final transient List<PreferenceSetting> settingsInitialized = new ArrayList<>();
277
278 final transient List<ValidationListener> validationListeners = new ArrayList<>();
279
280 /**
281 * Add validation listener to currently open preferences dialog. Calling to removeValidationListener is not necessary, all listeners will
282 * be automatically removed when dialog is closed
283 * @param validationListener validation listener to add
284 */
285 public void addValidationListener(ValidationListener validationListener) {
286 validationListeners.add(validationListener);
287 }
288
289 /**
290 * Construct a PreferencePanel for the preference settings. Layout is GridBagLayout
291 * and a centered title label and the description are added.
292 * @param caller Preference settings, that display a top level tab
293 * @return The created panel ready to add other controls.
294 */
295 public PreferencePanel createPreferenceTab(TabPreferenceSetting caller) {
296 return createPreferenceTab(caller, false);
297 }
298
299 /**
300 * Construct a PreferencePanel for the preference settings. Layout is GridBagLayout
301 * and a centered title label and the description are added.
302 * @param caller Preference settings, that display a top level tab
303 * @param inScrollPane if <code>true</code> the added tab will show scroll bars
304 * if the panel content is larger than the available space
305 * @return The created panel ready to add other controls.
306 */
307 public PreferencePanel createPreferenceTab(TabPreferenceSetting caller, boolean inScrollPane) {
308 CheckParameterUtil.ensureParameterNotNull(caller, "caller");
309 PreferencePanel p = new PreferencePanel(caller);
310
311 PreferenceTab tab = p;
312 if (inScrollPane) {
313 PreferenceScrollPane sp = new PreferenceScrollPane(p);
314 tab = sp;
315 }
316 tabs.add(tab);
317 return p;
318 }
319
320 private void selectTabBy(Predicate<TabPreferenceSetting> predicate) {
321 IntStream.range(0, getTabCount())
322 .filter(i -> getComponentAt(i) instanceof PreferenceTab
323 && predicate.test(((PreferenceTab) getComponentAt(i)).getTabPreferenceSetting()))
324 .findFirst()
325 .ifPresent(this::setSelectedIndex);
326 }
327
328 /**
329 * Selects a {@link TabPreferenceSetting} by its icon name
330 * @param name the icon name
331 */
332 public void selectTabByName(String name) {
333 Objects.requireNonNull(name);
334 selectTabBy(tps -> Objects.equals(name, tps.getIconName()));
335 }
336
337 /**
338 * Selects a {@link TabPreferenceSetting} by class
339 * @param clazz preferences tab class
340 */
341 public void selectTabByPref(Class<? extends TabPreferenceSetting> clazz) {
342 selectTabBy(clazz::isInstance);
343 }
344
345 /**
346 * Selects a {@link SubPreferenceSetting} by class
347 * @param clazz sub preferences tab class
348 * @return true if the specified preference settings have been selected, false otherwise.
349 */
350 public boolean selectSubTabByPref(Class<? extends SubPreferenceSetting> clazz) {
351 try {
352 final SubPreferenceSetting sub = getSetting(clazz);
353 final TabPreferenceSetting tab = sub.getTabPreferenceSetting(this);
354 selectTabBy(tps -> tps.equals(tab));
355 return tab.selectSubTab(sub);
356 } catch (NoSuchElementException ignore) {
357 Logging.trace(ignore);
358 return false;
359 }
360 }
361
362 /**
363 * Returns the currently selected preference and sub preference setting
364 * @return the currently selected preference and sub preference setting
365 */
366 public Pair<Class<? extends TabPreferenceSetting>, Class<? extends SubPreferenceSetting>> getSelectedTab() {
367 final Component selected = getSelectedComponent();
368 if (selected instanceof PreferenceTab) {
369 final TabPreferenceSetting setting = ((PreferenceTab) selected).getTabPreferenceSetting();
370 return Pair.create(setting.getClass(), setting.getSelectedSubTab());
371 } else {
372 return null;
373 }
374 }
375
376 /**
377 * Returns the {@code DisplayPreference} object.
378 * @return the {@code DisplayPreference} object.
379 */
380 public DisplayPreference getDisplayPreference() {
381 return getSetting(DisplayPreference.class);
382 }
383
384 /**
385 * Returns the {@code MapPreference} object.
386 * @return the {@code MapPreference} object.
387 */
388 public MapPreference getMapPreference() {
389 return getSetting(MapPreference.class);
390 }
391
392 /**
393 * Returns the {@code PluginPreference} object.
394 * @return the {@code PluginPreference} object.
395 */
396 public PluginPreference getPluginPreference() {
397 return getSetting(PluginPreference.class);
398 }
399
400 /**
401 * Returns the {@code ImageryPreference} object.
402 * @return the {@code ImageryPreference} object.
403 */
404 public ImageryPreference getImageryPreference() {
405 return getSetting(ImageryPreference.class);
406 }
407
408 /**
409 * Returns the {@code ShortcutPreference} object.
410 * @return the {@code ShortcutPreference} object.
411 */
412 public ShortcutPreference getShortcutPreference() {
413 return getSetting(ShortcutPreference.class);
414 }
415
416 /**
417 * Returns the {@code ServerAccessPreference} object.
418 * @return the {@code ServerAccessPreference} object.
419 * @since 6523
420 */
421 public ServerAccessPreference getServerPreference() {
422 return getSetting(ServerAccessPreference.class);
423 }
424
425 /**
426 * Returns the {@code ValidatorPreference} object.
427 * @return the {@code ValidatorPreference} object.
428 * @since 6665
429 */
430 public ValidatorPreference getValidatorPreference() {
431 return getSetting(ValidatorPreference.class);
432 }
433
434 /**
435 * Saves preferences.
436 */
437 public void savePreferences() {
438 // create a task for downloading plugins if the user has activated, yet not downloaded, new plugins
439 final PluginPreference preference = getPluginPreference();
440 if (preference != null) {
441 final Set<PluginInformation> toDownload = preference.getPluginsScheduledForUpdateOrDownload();
442 final PluginDownloadTask task;
443 if (toDownload != null && !toDownload.isEmpty()) {
444 task = new PluginDownloadTask(this, toDownload, tr("Download plugins"));
445 } else {
446 task = null;
447 }
448
449 // this is the task which will run *after* the plugins are downloaded
450 final Runnable continuation = new PluginDownloadAfterTask(preference, task, toDownload);
451
452 if (task != null) {
453 // if we have to launch a plugin download task we do it asynchronously, followed
454 // by the remaining "save preferences" activities run on the Swing EDT.
455 MainApplication.worker.submit(task);
456 MainApplication.worker.submit(() -> GuiHelper.runInEDT(continuation));
457 } else {
458 // no need for asynchronous activities. Simply run the remaining "save preference"
459 // activities on this thread (we are already on the Swing EDT
460 continuation.run();
461 }
462 }
463 }
464
465 /**
466 * If the dialog is closed with Ok, the preferences will be stored to the preferences-
467 * file, otherwise no change of the file happens.
468 */
469 public PreferenceTabbedPane() {
470 super(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
471 super.addMouseWheelListener(new WheelListener(this));
472 super.getModel().addChangeListener(this);
473 ExpertToggleAction.addExpertModeChangeListener(this);
474 }
475
476 public void buildGui() {
477 Collection<PreferenceSettingFactory> factories = new ArrayList<>(SETTINGS_FACTORIES);
478 factories.addAll(PluginHandler.getPreferenceSetting());
479 factories.add(ADVANCED_PREFERENCE_FACTORY);
480
481 for (PreferenceSettingFactory factory : factories) {
482 if (factory != null) {
483 PreferenceSetting setting = factory.createPreferenceSetting();
484 if (setting != null) {
485 settings.add(setting);
486 }
487 }
488 }
489 addGUITabs(false);
490 }
491
492 private void addGUITabsForSetting(Icon icon, TabPreferenceSetting tps) {
493 for (PreferenceTab tab : tabs) {
494 if (tab.getTabPreferenceSetting().equals(tps)) {
495 insertGUITabsForSetting(icon, tps, getTabCount());
496 }
497 }
498 }
499
500 private int insertGUITabsForSetting(Icon icon, TabPreferenceSetting tps, int index) {
501 int position = index;
502 for (PreferenceTab tab : tabs) {
503 if (tab.getTabPreferenceSetting().equals(tps) && tps.getIconName() != null) {
504 insertTab(null, icon, tab.getComponent(), tps.getTooltip(), position++);
505 } else if (tab.getTabPreferenceSetting().equals(tps)) {
506 insertTab(tps.getTitle(), null, tab.getComponent(), tps.getTooltip(), position++);
507 }
508 }
509 return position - 1;
510 }
511
512 private void addGUITabs(boolean clear) {
513 boolean expert = ExpertToggleAction.isExpert();
514 Component sel = getSelectedComponent();
515 if (clear) {
516 removeAll();
517 }
518 // Inspect each tab setting
519 for (PreferenceSetting setting : settings) {
520 if (setting instanceof TabPreferenceSetting) {
521 TabPreferenceSetting tps = (TabPreferenceSetting) setting;
522 if (expert || !tps.isExpert()) {
523 // Get icon
524 String iconName = tps.getIconName();
525 ImageIcon icon = null;
526
527 if (iconName != null && !iconName.isEmpty()) {
528 icon = ImageProvider.get("preferences", iconName, ImageProvider.ImageSizes.SETTINGS_TAB);
529 }
530 if (settingsInitialized.contains(tps)) {
531 // If it has been initialized, add corresponding tab(s)
532 addGUITabsForSetting(icon, tps);
533 } else if (tps.getIconName() != null) {
534 // If it has not been initialized, create an empty tab with only icon and tooltip
535 addTab(null, icon, new PreferencePanel(tps), tps.getTooltip());
536 } else {
537 addTab(tps.getTitle(), null, new PreferencePanel(tps), tps.getTooltip());
538 }
539 }
540 } else if (!(setting instanceof SubPreferenceSetting)) {
541 Logging.warn("Ignoring preferences "+setting);
542 }
543 }
544 if (sel != null) {
545 int index = indexOfComponent(sel);
546 if (index > -1) {
547 setSelectedIndex(index);
548 }
549 }
550 }
551
552 @Override
553 public void expertChanged(boolean isExpert) {
554 addGUITabs(true);
555 }
556
557 /**
558 * Returns a list of all preferences settings
559 * @return a list of all preferences settings
560 */
561 public List<PreferenceSetting> getSettings() {
562 return Collections.unmodifiableList(settings);
563 }
564
565 /**
566 * Returns the preferences setting for the given class
567 * @param clazz the preference setting class
568 * @param <T> the preference setting type
569 * @return the preferences setting for the given class
570 * @throws NoSuchElementException if there is no such value
571 */
572 public <T extends PreferenceSetting> T getSetting(Class<? extends T> clazz) {
573 return Utils.filteredCollection(settings, clazz).iterator().next();
574 }
575
576 static {
577 // order is important!
578 SETTINGS_FACTORIES.add(new DisplayPreference.Factory());
579 SETTINGS_FACTORIES.add(new DrawingPreference.Factory());
580 SETTINGS_FACTORIES.add(new GPXPreference.Factory());
581 SETTINGS_FACTORIES.add(new ColorPreference.Factory());
582 SETTINGS_FACTORIES.add(new LafPreference.Factory());
583 SETTINGS_FACTORIES.add(new LanguagePreference.Factory());
584
585 SETTINGS_FACTORIES.add(new ConnectionPreference.Factory());
586 SETTINGS_FACTORIES.add(new ServerAccessPreference.Factory());
587 SETTINGS_FACTORIES.add(new ProxyPreference.Factory());
588 SETTINGS_FACTORIES.add(new OverpassServerPreference.Factory());
589 SETTINGS_FACTORIES.add(new MapPreference.Factory());
590 SETTINGS_FACTORIES.add(new ProjectionPreference.Factory());
591 SETTINGS_FACTORIES.add(new MapPaintPreference.Factory());
592 SETTINGS_FACTORIES.add(new TaggingPresetPreference.Factory());
593 SETTINGS_FACTORIES.add(new BackupPreference.Factory());
594 SETTINGS_FACTORIES.add(new PluginPreference.Factory());
595 SETTINGS_FACTORIES.add(MainApplication.getToolbar());
596 SETTINGS_FACTORIES.add(new AudioPreference.Factory());
597 SETTINGS_FACTORIES.add(new ShortcutPreference.Factory());
598 SETTINGS_FACTORIES.add(new ValidatorPreference.Factory());
599 SETTINGS_FACTORIES.add(new ValidatorTestsPreference.Factory());
600 SETTINGS_FACTORIES.add(new ValidatorTagCheckerRulesPreference.Factory());
601 SETTINGS_FACTORIES.add(new RemoteControlPreference.Factory());
602 SETTINGS_FACTORIES.add(new ImageryPreference.Factory());
603 }
604
605 /**
606 * This mouse wheel listener reacts when a scroll is carried out over the
607 * tab strip and scrolls one tab/down or up, selecting it immediately.
608 */
609 static final class WheelListener implements MouseWheelListener {
610
611 final JTabbedPane tabbedPane;
612
613 WheelListener(JTabbedPane tabbedPane) {
614 this.tabbedPane = tabbedPane;
615 }
616
617 @Override
618 public void mouseWheelMoved(MouseWheelEvent wev) {
619 // Ensure the cursor is over the tab strip
620 if (tabbedPane.indexAtLocation(wev.getPoint().x, wev.getPoint().y) < 0)
621 return;
622
623 // Get currently selected tab && ensure the new tab index is sound
624 int newTab = Utils.clamp(tabbedPane.getSelectedIndex() + wev.getWheelRotation(),
625 0, tabbedPane.getTabCount() - 1);
626
627 tabbedPane.setSelectedIndex(newTab);
628 }
629 }
630
631 @Override
632 public void stateChanged(ChangeEvent e) {
633 int index = getSelectedIndex();
634 Component sel = getSelectedComponent();
635 if (index > -1 && sel instanceof PreferenceTab) {
636 PreferenceTab tab = (PreferenceTab) sel;
637 TabPreferenceSetting preferenceSettings = tab.getTabPreferenceSetting();
638 if (!settingsInitialized.contains(preferenceSettings)) {
639 try {
640 getModel().removeChangeListener(this);
641 preferenceSettings.addGui(this);
642 // Add GUI for sub preferences
643 for (PreferenceSetting setting : settings) {
644 if (setting instanceof SubPreferenceSetting) {
645 addSubPreferenceSetting(preferenceSettings, (SubPreferenceSetting) setting);
646 }
647 }
648 Icon icon = getIconAt(index);
649 remove(index);
650 if (index <= insertGUITabsForSetting(icon, preferenceSettings, index)) {
651 setSelectedIndex(index);
652 }
653 } catch (SecurityException ex) {
654 Logging.error(ex);
655 } catch (RuntimeException ex) { // NOPMD
656 // allow to change most settings even if e.g. a plugin fails
657 BugReportExceptionHandler.handleException(ex);
658 } finally {
659 settingsInitialized.add(preferenceSettings);
660 getModel().addChangeListener(this);
661 }
662 }
663 Container ancestor = getTopLevelAncestor();
664 if (ancestor instanceof PreferenceDialog) {
665 ((PreferenceDialog) ancestor).setHelpContext(preferenceSettings.getHelpContext());
666 }
667 }
668 }
669
670 private void addSubPreferenceSetting(TabPreferenceSetting preferenceSettings, SubPreferenceSetting sps) {
671 if (sps.getTabPreferenceSetting(this) == preferenceSettings) {
672 try {
673 sps.addGui(this);
674 } catch (SecurityException ex) {
675 Logging.error(ex);
676 } catch (RuntimeException ex) { // NOPMD
677 BugReportExceptionHandler.handleException(ex);
678 } finally {
679 settingsInitialized.add(sps);
680 }
681 }
682 }
683}
Note: See TracBrowser for help on using the repository browser.