- Timestamp:
- 2020-02-12T00:06:41+01:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceTabbedPane.java
r14273 r15844 12 12 import java.util.ArrayList; 13 13 import java.util.Collection; 14 import java.util.Collections; 14 15 import java.util.HashSet; 15 16 import java.util.Iterator; 16 17 import java.util.LinkedList; 17 18 import java.util.List; 19 import java.util.NoSuchElementException; 20 import java.util.Objects; 18 21 import java.util.Set; 22 import java.util.function.Predicate; 23 import java.util.stream.IntStream; 19 24 20 25 import javax.swing.BorderFactory; … … 66 71 import org.openstreetmap.josm.tools.ImageProvider; 67 72 import org.openstreetmap.josm.tools.Logging; 73 import org.openstreetmap.josm.tools.Utils; 68 74 import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler; 69 75 … … 304 310 } 305 311 306 @FunctionalInterface 307 private interface TabIdentifier { 308 boolean identify(TabPreferenceSetting tps, Object param); 309 } 310 311 private void selectTabBy(TabIdentifier method, Object param) { 312 for (int i = 0; i < getTabCount(); i++) { 313 Component c = getComponentAt(i); 314 if (c instanceof PreferenceTab) { 315 PreferenceTab tab = (PreferenceTab) c; 316 if (method.identify(tab.getTabPreferenceSetting(), param)) { 317 setSelectedIndex(i); 318 return; 319 } 320 } 321 } 322 } 323 312 private void selectTabBy(Predicate<TabPreferenceSetting> predicate) { 313 IntStream.range(0, getTabCount()) 314 .filter(i -> getComponentAt(i) instanceof PreferenceTab 315 && predicate.test(((PreferenceTab) getComponentAt(i)).getTabPreferenceSetting())) 316 .findFirst() 317 .ifPresent(this::setSelectedIndex); 318 } 319 320 /** 321 * Selects a {@link TabPreferenceSetting} by its icon name 322 * @param name the icon name 323 */ 324 324 public void selectTabByName(String name) { 325 selectTabBy((tps, name1) -> name1 != null && tps != null && tps.getIconName() != null && name1.equals(tps.getIconName()), name); 326 } 327 325 Objects.requireNonNull(name); 326 selectTabBy(tps -> Objects.equals(name, tps.getIconName())); 327 } 328 329 /** 330 * Selects a {@link TabPreferenceSetting} by class 331 * @param clazz preferences tab class 332 */ 328 333 public void selectTabByPref(Class<? extends TabPreferenceSetting> clazz) { 329 selectTabBy((tps, clazz1) -> tps.getClass().isAssignableFrom((Class<?>) clazz1), clazz); 330 } 331 334 selectTabBy(clazz::isInstance); 335 } 336 337 /** 338 * Selects a {@link SubPreferenceSetting} by class 339 * @param clazz sub preferences tab class 340 * @return true if the specified preference settings have been selected, false otherwise. 341 */ 332 342 public boolean selectSubTabByPref(Class<? extends SubPreferenceSetting> clazz) { 333 for (PreferenceSetting setting : settings) { 334 if (clazz.isInstance(setting)) { 335 final SubPreferenceSetting sub = (SubPreferenceSetting) setting; 336 final TabPreferenceSetting tab = sub.getTabPreferenceSetting(this); 337 selectTabBy((tps, unused) -> tps.equals(tab), null); 338 return tab.selectSubTab(sub); 339 } 340 } 341 return false; 343 try { 344 final SubPreferenceSetting sub = getSetting(clazz); 345 final TabPreferenceSetting tab = sub.getTabPreferenceSetting(this); 346 selectTabBy(tps -> tps.equals(tab)); 347 return tab.selectSubTab(sub); 348 } catch (NoSuchElementException ignore) { 349 return false; 350 } 342 351 } 343 352 … … 519 528 } 520 529 530 /** 531 * Returns a list of all preferences settings 532 * @return a list of all preferences settings 533 */ 521 534 public List<PreferenceSetting> getSettings() { 522 return settings; 523 } 524 525 @SuppressWarnings("unchecked") 526 public <T> T getSetting(Class<? extends T> clazz) { 527 for (PreferenceSetting setting:settings) { 528 if (clazz.isAssignableFrom(setting.getClass())) 529 return (T) setting; 530 } 531 return null; 535 return Collections.unmodifiableList(settings); 536 } 537 538 /** 539 * Returns the preferences setting for the given class 540 * @param clazz the preference setting class 541 * @param <T> the preference setting type 542 * @return the preferences setting for the given class 543 * @throws NoSuchElementException if there is no such value 544 */ 545 public <T extends PreferenceSetting> T getSetting(Class<? extends T> clazz) { 546 return Utils.filteredCollection(settings, clazz).iterator().next(); 532 547 } 533 548
Note:
See TracChangeset
for help on using the changeset viewer.