| 1 | // License: GPL. For details, see LICENSE file. |
| 2 | package org.openstreetmap.josm.plugins.flatlaf; |
| 3 | |
| 4 | import java.beans.PropertyChangeEvent; |
| 5 | import java.beans.PropertyChangeListener; |
| 6 | |
| 7 | import javax.swing.JComponent; |
| 8 | import javax.swing.plaf.ComponentUI; |
| 9 | |
| 10 | import org.openstreetmap.josm.gui.widgets.HideableTabbedPane; |
| 11 | import com.formdev.flatlaf.ui.FlatTabbedPaneUI; |
| 12 | |
| 13 | /** |
| 14 | * Special JOSM UI delegate for JTabbedPane that supports hiding tab area for {@link HideableTabbedPane}. |
| 15 | */ |
| 16 | public class JosmFlatTabbedPaneUI extends FlatTabbedPaneUI { |
| 17 | public static ComponentUI createUI(JComponent c) { |
| 18 | return (c instanceof HideableTabbedPane) |
| 19 | ? new JosmFlatTabbedPaneUI() |
| 20 | : FlatTabbedPaneUI.createUI(c); |
| 21 | } |
| 22 | |
| 23 | @Override |
| 24 | public void uninstallUI(JComponent c) { |
| 25 | super.uninstallUI(c); |
| 26 | |
| 27 | // HideableTabbedPane replaces FlatTabbedPaneUI with its own implementation |
| 28 | // based on BasicTabbedPaneUI. This listener detects this, sets a FlatLaf client |
| 29 | // property to enabed tab area hiding, and then resets the UI delegate to FlatTabbedPaneUI. |
| 30 | PropertyChangeListener l = new PropertyChangeListener() { |
| 31 | @Override |
| 32 | public void propertyChange(PropertyChangeEvent e) { |
| 33 | c.removePropertyChangeListener("UI", this); |
| 34 | |
| 35 | if (e.getOldValue() instanceof FlatTabbedPaneUI && |
| 36 | e.getNewValue() != null && |
| 37 | e.getNewValue().getClass().getName().startsWith(HideableTabbedPane.class.getName()+'$')) |
| 38 | { |
| 39 | c.putClientProperty("JTabbedPane.hideTabAreaWithOneTab", true); |
| 40 | |
| 41 | // reset to FlatTabbedPaneUI |
| 42 | c.updateUI(); |
| 43 | } |
| 44 | } |
| 45 | }; |
| 46 | c.addPropertyChangeListener("UI", l); |
| 47 | } |
| 48 | } |