001package jmri.swing;
002
003import java.awt.BorderLayout;
004import java.awt.Color;
005import java.awt.Dimension;
006import java.awt.Font;
007import java.awt.GridBagConstraints;
008import java.awt.GridBagLayout;
009import java.awt.Insets;
010import javax.swing.BorderFactory;
011import javax.swing.JLabel;
012import javax.swing.JPanel;
013import javax.swing.JSeparator;
014import javax.swing.UIManager;
015import javax.swing.border.TitledBorder;
016
017/**
018 * A separator with a title.
019 *
020 * JTitledSeparator based on
021 * https://github.com/rhwood/DJ-Swing-Suite/blob/master/DJSwingSuite/src/chrriis/dj/swingsuite/JTitledSeparator.java
022 * by
023 * Christopher Deckers (chrriis@nextencia.net)
024 * http://www.nextencia.net
025 *
026 * @author Randall Wood
027 */
028public class JTitledSeparator extends JPanel {
029
030    private final static class SeparatorPane extends JPanel {
031
032        private SeparatorPane() {
033            super(new GridBagLayout());
034            setOpaque(false);
035            setDoubleBuffered(false);
036            add(new JSeparator(), new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
037        }
038
039        @Override
040        public void setBounds(int x, int y, int w, int h) {
041            super.setBounds(x, y, w, h);
042            doLayout();
043        }
044    }
045    private final JLabel label = new JLabel();
046
047    /**
048     * Construct a separator with a title.
049     *
050     * @param title the title to set.
051     */
052    public JTitledSeparator(String title) {
053        super(new BorderLayout());
054        JPanel westPanel = new JPanel(new BorderLayout()) {
055
056            @Override
057            public void setBounds(int x, int y, int w, int h) {
058                super.setBounds(x, y, w, h);
059                doLayout();
060            }
061        };
062        westPanel.setOpaque(false);
063        westPanel.setDoubleBuffered(false);
064        boolean isLeftToRight = getComponentOrientation().isLeftToRight();
065        setOpaque(false);
066        westPanel.add(label, BorderLayout.CENTER);
067        if (isLeftToRight) {
068            add(westPanel, BorderLayout.WEST);
069        } else {
070            add(westPanel, BorderLayout.EAST);
071        }
072        SeparatorPane separatorPane = new SeparatorPane();
073        if (isLeftToRight) {
074            separatorPane.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0));
075        } else {
076            separatorPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2));
077        }
078        add(separatorPane, BorderLayout.CENTER);
079        setTitle(title);
080        this.setMaximumSize(new Dimension(Integer.MAX_VALUE, this.getPreferredSize().height));
081        adjustLook();
082    }
083
084    /**
085     * Get the title of this separator.
086     *
087     * @return the title.
088     */
089    public String getTitle() {
090        return label.getText();
091    }
092
093    /**
094     * Set the title of the separator.
095     *
096     * @param title the new title.
097     */
098    public void setTitle(String title) {
099        if (title == null) {
100            title = "";
101        }
102        boolean isVisible = title.length() != 0;
103        label.setVisible(isVisible);
104        label.setText(title);
105    }
106
107    @Override
108    public void updateUI() {
109        super.updateUI();
110        adjustLook();
111    }
112
113    private void adjustLook() {
114        if (label != null) {
115            Color titleColor = UIManager.getColor("TitledBorder.titleColor");
116            Font font = UIManager.getFont("TitledBorder.font");
117            if (titleColor == null || font == null) {
118                TitledBorder titledBorder = new TitledBorder("");
119                titleColor = titledBorder.getTitleColor();
120                font = titledBorder.getTitleFont();
121            }
122            label.setForeground(titleColor);
123            label.setFont(font);
124        }
125    }
126}