package com.mindprod.example;

import com.mindprod.common11.FontFactory;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Insets;

/**
 * demonstrate the use of javax.swing.JTextArea
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0
 */
public final class TestJTextArea
    {
    // --------------------------- main() method ---------------------------

    /**
     * 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();

            // The user hitting enter inserts a \n character into the text.
            final JTextArea jTextArea =
                    new JTextArea( "this is a TEST\n"
                                   + "of multiline." );
            // select colours
            jTextArea.setBackground( Color.BLACK );
            jTextArea.setForeground( Color.YELLOW );

            // select font
            jTextArea.setFont( FontFactory.build( "Dialog", Font.BOLD, 15 ) );

            // provide some space around the inside
            jTextArea.setMargin( new Insets( 4, 4, 4, 4 ) );

            // automatically wrap lines
            jTextArea.setLineWrap( true );

            // break lines on word, rather than character boundaries.
            jTextArea.setWrapStyleWord( true );

            // You need some sort of border, perhaps created with
            // contrasting background colour or with an explicit border or
            // else the JTextArea will blend into the background
            // and you won't be able to see item.
            jTextArea.setBorder( BorderFactory.createLoweredBevelBorder() );
            jTextArea.setEnabled( true );
            jTextArea.setEditable( true );
            // textArea.InputMethodListener does not work unless you override
            // getInputMethodRequests.
            // Must add DocumentListener to the associated document, not the
            // JTextArea.
            Document document = jTextArea.getDocument();
            document.addDocumentListener( new DocumentListener()
            {
            /**
             * Gives notification that an attribute or set of attributes
             * changed.
             *
             * @param e the document event
             */
            public void changedUpdate( DocumentEvent e )
                {
                System.out.println( "changed: " + jTextArea.getText() );
                }

            /**
             * Gives notification that there was an insert into the
             * document. The range given by the DocumentEvent bounds the
             * freshly inserted region.
             *
             * @param e the document event
             */
            public void insertUpdate( DocumentEvent e )
                {
                System.out
                        .println( "inserted: " + jTextArea.getText() );
                }

            /**
             * Gives notification that a portion of the document has
             * been removed. The range is given in terms of what the
             * view last saw (that is, before updating sticky
             * positions).
             *
             * @param e the document event
             */
            public void removeUpdate( DocumentEvent e )
                {
                System.out.println( "removed: " + jTextArea.getText() );
                }
            } );

            contentPane.add( jTextArea );
            jFrame.setSize( 100, 100 );
            jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            jFrame.validate();
            jFrame.setVisible( true );
            }
        } );
        }// end main
    }