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.*;
/**
* 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
{
/**
* CSS style sheet. Don't try to collapse span.line and span.tail into one line with a comma.
* Not actually used in this example.
*/
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";
/**
* 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 );
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 );
jEditorPane.setMargin( new Insets( 4, 4, 4, 4 ) );
jEditorPane.setEnabled( true );
final JScrollPane scroller = new JScrollPane( jEditorPane );
final HTMLDocument htmlDocument = new HTMLDocument();
try
{
htmlDocument.setBase( new URL( "http://mindprod.com" ) );
}
catch ( MalformedURLException e )
{
System.exit( 1 );
}
jEditorPane.setDocument( htmlDocument );
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;\">&&</span>"
+ "</body></html>";
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 );
}
} );
}
}