Changeset 17313 in josm
- Timestamp:
- 2020-11-15T19:47:37+01:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceTabbedPane.java
r17307 r17313 8 8 import java.awt.Container; 9 9 import java.awt.Font; 10 import java.awt.FontMetrics; 11 import java.awt.GridBagConstraints; 10 12 import java.awt.GridBagLayout; 11 13 import java.awt.event.MouseWheelEvent; … … 33 35 import javax.swing.JScrollPane; 34 36 import javax.swing.JTabbedPane; 35 import javax.swing. LookAndFeel;37 import javax.swing.SwingConstants; 36 38 import javax.swing.UIManager; 37 39 import javax.swing.event.ChangeEvent; … … 223 225 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 224 226 JPanel headerPanel = new JPanel(new BorderLayout()); 225 add(headerPanel, GBC.eop().fill(G BC.HORIZONTAL));227 add(headerPanel, GBC.eop().fill(GridBagConstraints.HORIZONTAL)); 226 228 227 229 JLabel label = new JLabel("<html>" + … … 474 476 */ 475 477 public PreferenceTabbedPane() { 476 super( JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);478 super(SwingConstants.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT); 477 479 super.addMouseWheelListener(new WheelListener(this)); 478 480 ExpertToggleAction.addExpertModeChangeListener(this); 479 481 } 480 482 483 /** 484 * Constructs GUI. 485 */ 481 486 public void buildGui() { 482 487 Collection<PreferenceSettingFactory> factories = new ArrayList<>(SETTINGS_FACTORIES); … … 496 501 } 497 502 498 private void addGUITabsForSetting(Icon icon, TabPreferenceSetting tps ) {503 private void addGUITabsForSetting(Icon icon, TabPreferenceSetting tps, int maxWidth) { 499 504 for (PreferenceTab tab : tabs) { 500 505 if (tab.getTabPreferenceSetting().equals(tps)) { 501 insertGUITabsForSetting(icon, tps, tab.getComponent(), getTabCount() );502 } 503 } 504 } 505 506 private int insertGUITabsForSetting(Icon icon, TabPreferenceSetting tps, int index ) {506 insertGUITabsForSetting(icon, tps, tab.getComponent(), getTabCount(), maxWidth); 507 } 508 } 509 } 510 511 private int insertGUITabsForSetting(Icon icon, TabPreferenceSetting tps, int index, int maxWidth) { 507 512 int position = index; 508 513 for (PreferenceTab tab : tabs) { 509 514 if (tab.getTabPreferenceSetting().equals(tps)) { 510 insertGUITabsForSetting(icon, tps, tab.getComponent(), position );515 insertGUITabsForSetting(icon, tps, tab.getComponent(), position, maxWidth); 511 516 position++; 512 517 } … … 515 520 } 516 521 517 private void insertGUITabsForSetting(Icon icon, TabPreferenceSetting tps, final Component component, int position ) {518 LookAndFeel currentLAF = UIManager.getLookAndFeel();519 if ("com.apple.laf.AquaLookAndFeel".equals(currentLAF.getClass().getName())) {520 // macOS / AquaLookAndFeel does not support horizontal tabs, see https://josm.openstreetmap.de/ticket/7548#comment:80521 insertTab(null, icon, component, tps.getTooltip(), position);522 } else { 523 String title = "<html><div style='width:150px'>" + tps.getTitle();524 insertTab(title, icon, component, tps.getTooltip(), position);525 }522 private void insertGUITabsForSetting(Icon icon, TabPreferenceSetting tps, final Component component, int position, int maxWidth) { 523 // macOS / AquaLookAndFeel does not support horizontal tabs, see https://josm.openstreetmap.de/ticket/7548#comment:80 524 String title = "Aqua".equals(UIManager.getLookAndFeel().getID()) ? null : htmlTabTitle(tps.getTitle(), maxWidth); 525 insertTab(title, icon, component, tps.getTooltip(), position); 526 } 527 528 private String htmlTabTitle(String title, int maxWidth) { 529 // Width is set to force left alignment, see https://stackoverflow.com/a/33781096/2257172 530 return "<html><div style='padding-left:5px; width:" + maxWidth + "px'>" + title + "</div></html>"; 526 531 } 527 532 … … 531 536 removeAll(); 532 537 } 538 // Compute max tab length in pixels 539 int maxWidth = computeMaxTabWidth(); 533 540 // Inspect each tab setting 534 541 for (PreferenceSetting setting : settings) { … … 539 546 if (settingsInitialized.contains(tps)) { 540 547 // If it has been initialized, add corresponding tab(s) 541 addGUITabsForSetting(icon, tps );548 addGUITabsForSetting(icon, tps, maxWidth); 542 549 } else { 543 550 // If it has not been initialized, create an empty tab with only icon and tooltip 544 insertGUITabsForSetting(icon, tps, new PreferencePanel(tps), getTabCount() );551 insertGUITabsForSetting(icon, tps, new PreferencePanel(tps), getTabCount(), maxWidth); 545 552 } 546 553 } … … 561 568 } 562 569 setSelectedIndex(-1); 570 } 571 572 private int computeMaxTabWidth() { 573 FontMetrics fm = getFontMetrics(getFont()); 574 return settings.stream().filter(x -> x instanceof TabPreferenceSetting) 575 .mapToInt(x -> fm.stringWidth(((TabPreferenceSetting) x).getTitle())).max().orElse(120); 563 576 } 564 577 … … 665 678 Icon icon = getIconAt(index); 666 679 remove(index); 667 if (index <= insertGUITabsForSetting(icon, preferenceSettings, index )) {680 if (index <= insertGUITabsForSetting(icon, preferenceSettings, index, computeMaxTabWidth())) { 668 681 setSelectedIndex(index); 669 682 }
Note:
See TracChangeset
for help on using the changeset viewer.