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

net.shredzone.jshred.swing.JHeadline Maven / Gradle / Ivy

/**
 * jshred - Shred's Toolbox
 *
 * Copyright (C) 2009 Richard "Shred" Körber
 *   http://jshred.shredzone.org
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License / GNU Lesser
 * General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * 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.
 *
 */
package net.shredzone.jshred.swing;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * This is a headline to be used in dialogs etc. It shows a big title to the left, and
 * optionally a nice icon to the right and a description below the title. The headline is
 * colored in a gradient, starting in a certain color to the left, and going to the
 * current background color to the right.
 *
 * @author Richard "Shred" Körber
 * @since R8
 */
public class JHeadline extends JGradientPanel {
    private static final long serialVersionUID = 3618137866132074806L;
    private final JLabel jlTitle;
    private final JLabel jlDesc;
    private final JLabel jlIcon;
    private boolean init = false;

    /**
     * Creates an empty {@link JHeadline}. {@link Color#GRAY} is used as default color.
     */
    public JHeadline() {
        this("");
    }

    /**
     * Creates a new {@link JHeadline} with the given title. {@link Color#GRAY} is used as
     * default color.
     *
     * @param title
     *            Title to be used
     */
    public JHeadline(String title) {
        this(title, null);
    }

    /**
     * Creates a new {@link JHeadline} with the given title and icon. {@link Color#GRAY}
     * is used as default color.
     *
     * @param title
     *            Title to be used
     * @param icon
     *            {@link Icon} or {@code null}
     */
    public JHeadline(String title, Icon icon) {
        this(title, null, icon);
    }

    /**
     * Creates a new {@link JHeadline} with the given title, description, icon.
     * {@link Color#GRAY} is used as default color.
     *
     * @param title
     *            Title to be used
     * @param desc
     *            Description or {@code null}
     * @param icon
     *            {@link Icon} or {@code null}
     */
    public JHeadline(String title, String desc, Icon icon) {
        this(title, desc, icon, Color.GRAY);
    }

    /**
     * Creates a new {@link JHeadline} with the given title, description, icon and color.
     *
     * @param title
     *            Title to be used
     * @param desc
     *            Description or {@code null}
     * @param icon
     *            {@link Icon} or {@code null}
     * @param color
     *            Left gradient {@link Color}
     */
    public JHeadline(String title, String desc, Icon icon, Color color) {
        super(color, null, JGradientPanel.HORIZONTAL);
        setLayout(new BorderLayout());

        jlTitle = new JLabel(title);
        jlTitle.setFont(jlTitle.getFont().deriveFont(20.0f));

        jlDesc = new JLabel("");
        jlDesc.setBorder(BorderFactory.createEmptyBorder(0, 25, 0, 0));
        setDescription(desc);

        jlIcon = new JLabel("");
        setIcon(icon);

        JPanel jpLeft = new JPanel();
        jpLeft.setOpaque(false);
        jpLeft.setLayout(new BoxLayout(jpLeft, BoxLayout.Y_AXIS));
        {
            jpLeft.add(Box.createVerticalGlue());
            jpLeft.add(jlTitle);
            jpLeft.add(jlDesc);
        }
        add(jpLeft, BorderLayout.WEST);
        add(jlIcon, BorderLayout.EAST);

        init = true;
        setForeground(Color.WHITE);

        setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));
    }

    /**
     * Sets a new title text.
     *
     * @param title
     *            new title text, must not be {@code null}.
     */
    public void setTitle(String title) {
        if (title == null) throw new NullPointerException("title must not be null");

        jlTitle.setText(title);
    }

    /**
     * Gets the current title text.
     *
     * @return Current title text.
     */
    public String getTitle() {
        return jlTitle.getText();
    }

    /**
     * Sets a new description.
     *
     * @param desc
     *            new description, {@code null} if there is none.
     */
    public void setDescription(String desc) {
        if (desc != null) {
            jlDesc.setText(desc);
            jlDesc.setVisible(true);
        } else {
            jlDesc.setVisible(false);
        }
    }

    /**
     * Gets the current description.
     *
     * @return Current description, or {@code null} if there is none.
     */
    public String getDescription() {
        if (jlDesc.isVisible()) return jlDesc.getText();
        else return null;
    }

    /**
     * Sets a new {@link Icon}.
     *
     * @param icon
     *            new {@link Icon}, {@code null} if there is none
     */
    public void setIcon(Icon icon) {
        if (icon != null) {
            jlIcon.setIcon(icon);
            jlIcon.setVisible(true);
        } else {
            jlIcon.setVisible(false);
        }
    }

    /**
     * Gets the current {@link Icon}
     *
     * @return Current {@link Icon}, or {@code null} if there is none.
     */
    public Icon getIcon() {
        if (jlIcon.isVisible()) return jlIcon.getIcon();
        else return null;
    }

    /**
     * Sets the text color. The title will be shown in this color. The description will be
     * shown in the same color, but with an alpha value of 200, so the background gradient
     * will shine through a little.
     *
     * @param fg
     *            New foreground {@link Color}
     */
    @Override
    public void setForeground(Color fg) {
        super.setForeground(fg);
        if (init) {
            jlTitle.setForeground(fg);
            jlDesc.setForeground(new Color(fg.getRed(), fg.getGreen(), fg.getBlue(), 200));
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy