HTMLのエレメント部分を色付けして表示する -- JTextPaneの使い方

戻る


import javax.swing.*;
import javax.swing.text.*;

import java.awt.event.*;
import java.awt.*;

import java.io.*;
import java.util.*;

/**
$Id: tpt.html,v 1.1 2009/06/22 16:12:31 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class MyJTextPaneTest extends JFrame {

    protected JTextPane textPane;

    protected DefaultStyledDocument doc;
    protected StyleContext sc;

    /** HTMLのエレメント部分の前景色 */
    private final Color ELEMENT_COLOR = Color.blue;

    private StringBuffer sb = null;

    public static void main( String[] args ) {
        MyJTextPaneTest test = new MyJTextPaneTest();

        test.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        test.setVisible( true );
    }

    MyJTextPaneTest() {
        setTitle( "HTMLのエレメント部分を色付けして表示する$" );
        setBounds( 10, 10, 800, 600 );

        textPane = new JTextPane();
        JScrollPane scroll = new JScrollPane( textPane,
                                              JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                              JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );

        getContentPane().add( scroll );

        sc = new StyleContext();
        doc = new DefaultStyledDocument( sc );

        textPane.setDocument( doc );

        readDocument();

        changeElementFGColor();
    }

    private void readDocument() {

        try {
            BufferedReader br = new BufferedReader( new FileReader( "./test.html" ) );

            sb = new StringBuffer();
            String line;
            while ( ( line = br.readLine() ) != null ) {
                sb.append( line + "\n" );
            }

            br.close();

        } catch ( Exception e ) {
            e.printStackTrace();
        }

        try {
            /* 文書を挿入する */
            doc.insertString( 0, new String( sb ),
                              sc.getStyle( StyleContext.DEFAULT_STYLE ) );
        } catch ( BadLocationException ble ) {
            System.err.println( "初期文書の読み込みに失敗しました。" );
        }

        /*
        MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setForeground(attr, Color.red);

        doc.setCharacterAttributes(0, 20, attr, false);
        */

    }

    private void changeElementFGColor() {
        MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setForeground( attr, ELEMENT_COLOR );

        // エレメント部の開始終了の位置を求める

        RegionRetriever rr = new RegionRetriever( sb );
        java.util.List list = rr.getRegions();

        Iterator iterator = list.iterator();
        while ( iterator.hasNext() ) {
            int[] region = ( int[] ) iterator.next();
            int from = region[ 0 ];
            int to = region[ 1 ];

            System.out.println( region[ 0 ] + " : " + region[ 1 ] );
            doc.setCharacterAttributes( from , to - from, attr, false );
        }

    }

}
import java.io.*;
import java.util.*;

/**
$Id: tpt.html,v 1.1 2009/06/22 16:12:31 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class RegionRetriever {

    private StringBuffer sb;

    public RegionRetriever( StringBuffer sb ) {
        this.sb = sb;
    }

    public java.util.List getRegions() {
        LinkedList list = new LinkedList();

        StringReader sr = new StringReader( sb.toString() );
        boolean isStarted = false;

        try {
            int c;
            int from = 0;
            int to = 0;
            int offset = 0;
            while ( ( c = sr.read() ) != -1 ) {

                if ( !isStarted && c == '<' ) {

                    isStarted = true;
                    System.out.println( "タグが開始しました!" );

                    from = offset;
                }

                if ( isStarted && c == '>' ) {

                    isStarted = false;
                    System.out.println( "タグが閉じられました!" );
                    to = offset;

                    int[] region = new int[ 2 ];
                    region[ 0 ] = from;
                    region[ 1 ] = to;

                    list.add( region );
                }

                offset++;
            }

        } catch ( IOException e ) {
            e.printStackTrace();
        }

        return list;
    }

}


戻る inserted by FC2 system