/*
 * [TestJEditorPane.java]
 *
 * Summary: demonstrate the use of javax.swing.JEditorPane to edit HTML text.
 *
 * 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.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.html.HTMLDocument;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.net.MalformedURLException;
import java.net.URL;

import static java.lang.System.out;

/**
 * demonstrate the use of javax.swing.JEditorPane to edit HTML text.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
public final class TestJEditorPane
    {
    // ------------------------------ CONSTANTS ------------------------------

    /**
     * CSS style sheet. Don't try to collapse span.line and span.tail into one line with a comma.
     */
    private static final String styleSheet = "<style type=\"text/css\">\n"
                                             + "body{font-family:\"Bitstream Vera Sans Mono\",\"Lucida Console\",\"Lucida Sans\",\"Lucida Sans Unicode\",\"Segoe UI\",monospace;}\n"
                                             + "span.boundary{color:#000000;font-weight:bold;}\n"
                                             + "span.label{color:#ce0f14;}\n"
                                             + "span.line{color:#5c9548;}\n"
                                             + "span.multi{color:#409954;}\n"
                                             + "span.operand{color:#006677;}\n"
                                             + "span.operator{color:#000000;}\n"
                                             + "span.tail{color:#5c9548;}\n"
                                             + "</style>\n";

    // --------------------------- 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();
            contentPane.setLayout( new GridBagLayout() );
            final JEditorPane jEditorPane = new JEditorPane();
            jEditorPane.setContentType( "text/html" );
            jEditorPane.setEditable( true );
            // Gotcha! Setting background has no effect. You must set colours in HTML markup
            jEditorPane.setBackground( new Color( 0xffe8e8 ) );
            jEditorPane.setForeground( Color.BLACK );
            jEditorPane.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
            jEditorPane.setAlignmentX( 0.5f );
            jEditorPane.setAlignmentY( 0.5f );
            // provide some space around the inside
            jEditorPane.setMargin( new Insets( 4, 4, 4, 4 ) );
            jEditorPane.setEnabled( true );
            final JScrollPane scroller = new JScrollPane( jEditorPane );
            final HTMLDocument htmlDocument = new HTMLDocument();
            // If there are any relative links including relative references to images in your HTML,
            // you will need some sort of assumed base URL to define what they mean.
            // There is nothing inherently inside the document to define that.
            try
                {
                htmlDocument.setBase( new URL( "http://mindprod.com" ) );
                }
            catch ( MalformedURLException e )
                {
                System.exit( 1 );
                }
            jEditorPane.setDocument( htmlDocument );
            // to work on Apple or Linux you must limit yourself to HTML 3.2. No &rsquo; For Mac/Linux no CSS.
            final String html = "<html lang=\"en-CA\"><body>"
                                + "<font color=\"red\">hello</font>"
                                + "<span class=\"line\">some text</span>"
                                + "<span class=\"operator\" style=\"background-color:#eeffee;\">&amp;&amp;</span>"
                                + "</body></html>";
            // You must do setText after setDocument.
            jEditorPane.setText( html );
            htmlDocument.addDocumentListener( new DocumentListener()
            {
            public void changedUpdate( DocumentEvent theEvent )
                {
                out.println( "change: " + jEditorPane.getText() );
                }

            public void insertUpdate( DocumentEvent theEvent )
                {
                out.println( "insert: " + jEditorPane.getText() );
                }

            public void removeUpdate( DocumentEvent theEvent )
                {
                out.println( "remove: " + jEditorPane.getText() );
                }
            } );
            contentPane.add( scroller,
                    new GridBagConstraints( 0,
                            0,
                            1,
                            1,
                            100.0,
                            100.0,
                            GridBagConstraints.CENTER,
                            GridBagConstraints.BOTH,
                            new Insets( 10, 10, 10, 10 ),
                            0,
                            0 ) );
            jFrame.setSize( 150, 100 );
            jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            jFrame.validate();
            jFrame.setVisible( true );
            }
        } );
        }// end main
    }