/*
 * [TestJTextArea.java]
 *
 * Summary: demonstrate the use of javax.swing.JTextArea.
 *
 * Copyright: (c) 2009-2012 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.7+
 *
 * 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.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;

import static java.lang.System.out;

/**
 * demonstrate the use of javax.swing.JTextArea.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
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( new Font( "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 )
                {
                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 )
                {
                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 )
                {
                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
    }