/*
 * [TestJLabel.java]
 *
 * Summary: demonstrate the use of javax.swing.JLabel.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2009-01-01 initial version
 */
package com.mindprod.example;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * demonstrate the use of javax.swing.JLabel.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
public final class TestJLabel
    {
    private static final Color FOREGROUND_FOR_LABEL = new Color( 0x0000b0 );

    private static final Color PALE_CYAN = new Color( 200, 255, 200 );

    private static final Font FONT_FOR_LABELS = new Font( "Dialog", Font.PLAIN, 15 );

    /**
     * track whether showing false=short or true=long string.
     */
    private static boolean toggle = false;

    /**
     * Debugging harness for a Frame
     *
     * @param args command line arguments are ignored.
     */
    public static void main( String args[] )
        {
        SwingUtilities.invokeLater( new Runnable()
            {
            /**
             * fire up a JFrame on the Swing thread
             */
            public void run()
                {
                final JFrame jFrame = new JFrame();
                final Container contentPane = jFrame.getContentPane();
                contentPane.setLayout( new FlowLayout( FlowLayout.LEADING ) );
                // typical JLabel
                final JLabel plainLabel = new JLabel( "this is a TEST" );
                plainLabel.setBackground( Color.BLACK );
                plainLabel.setForeground( Color.YELLOW );
                plainLabel.setOpaque( true );
                plainLabel.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
                plainLabel.setHorizontalAlignment( JTextField.RIGHT );
                plainLabel.setEnabled( true );
                // embedded HTML to underline
                final JLabel htmlLabel =
                        new JLabel( "<html lang=\"en-CA\"><u>look! underlined!</u></html>" );
                htmlLabel.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
                htmlLabel.setHorizontalAlignment( JTextField.LEFT );
                htmlLabel.setEnabled( true );
                // all the bells and whistles
                final JLabel fancyLabel = new JLabel( "Hello" );
                fancyLabel.setHorizontalAlignment( JLabel.RIGHT );
                fancyLabel.setHorizontalTextPosition( JLabel.LEFT );
                fancyLabel.setBackground( Color.YELLOW );
                fancyLabel.setForeground( FOREGROUND_FOR_LABEL );
                fancyLabel.setOpaque( true );
                fancyLabel.setFont( FONT_FOR_LABELS );
                // add an image
                // The jar contains the resource com/mindprod/example/raspberry.png
                final ImageIcon raspberry = new ImageIcon( TestJLabel.class.getResource(
                        "raspberry.png" ) );
                fancyLabel.setIcon( raspberry );
                Border border = BorderFactory.createEmptyBorder( 2/*top*/, 2/* left*/, 2/*bottom */, 2/*right*/ );
                fancyLabel.setBorder( border );
                fancyLabel.setText( "adios" );
                // dynamically changing JLabel
                final JLabel varyingLabel = new JLabel( "a short string" );
                varyingLabel.setHorizontalAlignment( JLabel.CENTER );
                varyingLabel.setHorizontalTextPosition( JLabel.CENTER );
                varyingLabel.setBackground( PALE_CYAN );
                varyingLabel.setForeground( FOREGROUND_FOR_LABEL );
                varyingLabel.setOpaque( true );
                varyingLabel.setFont( FONT_FOR_LABELS );
                // toggle button, to change length of varying label
                final JButton toggleButton = new JButton( "toggle" );
                toggleButton.setHorizontalAlignment( JLabel.CENTER );
                toggleButton.setHorizontalTextPosition( JLabel.CENTER );
                toggleButton.setBackground( PALE_CYAN );
                toggleButton.setForeground( FOREGROUND_FOR_LABEL );
                toggleButton.setOpaque( true );
                toggleButton.setFont( FONT_FOR_LABELS );
                toggleButton.setBorder( BorderFactory.createRaisedBevelBorder() );
                toggleButton.setFocusPainted( false );
                toggleButton.setToolTipText( "Toggle long and short label text." );
                toggleButton.addActionListener( new ActionListener()
                    {
                    /**
                     * Invoked when  button pressed
                     */
                    public void actionPerformed( ActionEvent e )
                        {
                        // toggle back and forth between displaying long and short string.
                        toggle = !toggle;
                        varyingLabel.setText( toggle ? "a long string of text to display" : "a short string" );
                        }
                    } );
                // lay components out is a line, left to right.
                contentPane.add( plainLabel );
                contentPane.add( htmlLabel );
                contentPane.add( fancyLabel );
                contentPane.add( varyingLabel );
                contentPane.add( toggleButton );
                jFrame.setSize( 560, 70 );
                jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                jFrame.validate();
                jFrame.setVisible( true );
                }
            } );
        } // end main
    }