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

Last change on this file since 17734 was 17734, checked in by simon04, 3 years ago

see #16163 - Fix SwingConstants.LEFT for PreferenceTabbedPane

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