Changeset 17313 in josm for trunk/src/org


Ignore:
Timestamp:
2020-11-15T19:47:37+01:00 (3 years ago)
Author:
Don-vip
Message:

see #7548 - make tab width dynamic + add 5px padding between icon and text

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceTabbedPane.java

    r17307 r17313  
    88import java.awt.Container;
    99import java.awt.Font;
     10import java.awt.FontMetrics;
     11import java.awt.GridBagConstraints;
    1012import java.awt.GridBagLayout;
    1113import java.awt.event.MouseWheelEvent;
     
    3335import javax.swing.JScrollPane;
    3436import javax.swing.JTabbedPane;
    35 import javax.swing.LookAndFeel;
     37import javax.swing.SwingConstants;
    3638import javax.swing.UIManager;
    3739import javax.swing.event.ChangeEvent;
     
    223225            setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    224226            JPanel headerPanel = new JPanel(new BorderLayout());
    225             add(headerPanel, GBC.eop().fill(GBC.HORIZONTAL));
     227            add(headerPanel, GBC.eop().fill(GridBagConstraints.HORIZONTAL));
    226228
    227229            JLabel label = new JLabel("<html>" +
     
    474476     */
    475477    public PreferenceTabbedPane() {
    476         super(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
     478        super(SwingConstants.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
    477479        super.addMouseWheelListener(new WheelListener(this));
    478480        ExpertToggleAction.addExpertModeChangeListener(this);
    479481    }
    480482
     483    /**
     484     * Constructs GUI.
     485     */
    481486    public void buildGui() {
    482487        Collection<PreferenceSettingFactory> factories = new ArrayList<>(SETTINGS_FACTORIES);
     
    496501    }
    497502
    498     private void addGUITabsForSetting(Icon icon, TabPreferenceSetting tps) {
     503    private void addGUITabsForSetting(Icon icon, TabPreferenceSetting tps, int maxWidth) {
    499504        for (PreferenceTab tab : tabs) {
    500505            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) {
    507512        int position = index;
    508513        for (PreferenceTab tab : tabs) {
    509514            if (tab.getTabPreferenceSetting().equals(tps)) {
    510                 insertGUITabsForSetting(icon, tps, tab.getComponent(), position);
     515                insertGUITabsForSetting(icon, tps, tab.getComponent(), position, maxWidth);
    511516                position++;
    512517            }
     
    515520    }
    516521
    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:80
    521             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>";
    526531    }
    527532
     
    531536            removeAll();
    532537        }
     538        // Compute max tab length in pixels
     539        int maxWidth = computeMaxTabWidth();
    533540        // Inspect each tab setting
    534541        for (PreferenceSetting setting : settings) {
     
    539546                    if (settingsInitialized.contains(tps)) {
    540547                        // If it has been initialized, add corresponding tab(s)
    541                         addGUITabsForSetting(icon, tps);
     548                        addGUITabsForSetting(icon, tps, maxWidth);
    542549                    } else {
    543550                        // 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);
    545552                    }
    546553                }
     
    561568        }
    562569        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);
    563576    }
    564577
     
    665678                    Icon icon = getIconAt(index);
    666679                    remove(index);
    667                     if (index <= insertGUITabsForSetting(icon, preferenceSettings, index)) {
     680                    if (index <= insertGUITabsForSetting(icon, preferenceSettings, index, computeMaxTabWidth())) {
    668681                        setSelectedIndex(index);
    669682                    }
Note: See TracChangeset for help on using the changeset viewer.