All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pekinsoft.desktop.ButtonTabComponent Maven / Gradle / Ivy

/*
 * Copyright (C) 2024 PekinSOFT Systems
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 *
 * *****************************************************************************
 *  Project    :   application-management-api
 *  Class      :   ButtonTabComponent.java
 *  Author     :   Sean Carrick
 *  Created    :   Jun 23, 2024
 *  Modified   :   Jun 23, 2024
 *
 *  Purpose: See class JavaDoc for explanation
 *
 *  Revision History:
 *
 *  WHEN          BY                   REASON
 *  ------------  -------------------  -----------------------------------------
 *  Jun 23, 2024  Sean Carrick         Initial creation.
 * *****************************************************************************
 */
package com.pekinsoft.desktop;

import com.pekinsoft.api.Dockable;
import com.pekinsoft.api.DockingManager;
import com.pekinsoft.utils.ColorUtils;
import java.awt.*;
import java.awt.event.*;
import java.util.Objects;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;

/**
 *
 * @author Sean Carrick <sean at pekinsoft dot com>
 *
 * @version 1.0
 * @since 1.0
 */
public class ButtonTabComponent extends JPanel {

    public ButtonTabComponent(final JTabbedPane pane, final Dockable dock) {
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        Objects.requireNonNull(pane, "The JTabbedPane cannot be null.");
        Objects.requireNonNull(dock, "The Dockable cannot be null.");

        this.pane = pane;
        this.dock = dock;

        setOpaque(false);

        JLabel label = new JLabel() {
            @Override
            public Icon getIcon() {
                int i = pane.indexOfTabComponent(ButtonTabComponent.this);
                if (i != -1) {
                    return pane.getIconAt(i);
                } else {
                    return dock.getIcon();
                }
            }

            @Override
            public String getText() {
                int i = pane.indexOfTabComponent(ButtonTabComponent.this);
                if (i != -1) {
                    return pane.getTitleAt(i);
                } else {
                    return dock.getTitle();
                }
            }
        };
        add(label);
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
        JButton button = new TabButton();
        add(button);
        setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
    }

    public Dockable getDockable() {
        return dock;
    }

    public JTabbedPane getTabbedPane() {
        return pane;
    }

    private final JTabbedPane pane;
    private final Dockable dock;

    private class TabButton extends JButton implements ActionListener {

        public TabButton() {
            int size = 17;
            setPreferredSize(new Dimension(size, size));
            setToolTipText("Close this Dock");
            setUI(new BasicButtonUI());
            setContentAreaFilled(false);
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            addMouseListener(buttonMouseListener);
            setRolloverEnabled(true);
            addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            DockingManager mgr = DockingManager.getDefault();
            int i = pane.indexOfTabComponent(ButtonTabComponent.this);
            if (i != -1) {
                mgr.undock(dock.getDockId());
                pane.remove(ButtonTabComponent.this);
            }
        }

        @Override
        public void updateUI() {
            // Prevent UI updates.
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();

            if (getModel().isPressed()) {
                g2.translate(1, 1);
            }

            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.black);
            if (getModel().isRollover()) {
                g2.setColor(ColorUtils.CRIMSON);
            }
            int delta = 6;
            g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
            g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
            g2.dispose();
        }

    }

    private final static MouseListener buttonMouseListener = new MouseAdapter() {
        @Override
        public void mouseExited(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton button) {
                button.setBorderPainted(false);
            }
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton button) {
                button.setBorderPainted(true);
            }
        }
    };

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy