• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

扩展JTextPane,使其有关键字自动加亮功能。

发布: 2007-7-01 20:40 | 作者: admin | 来源: | 查看: 51次 | 进入软件测试论坛讨论

领测软件测试网


import java.util.StringTokenizer;

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

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.text.rtf.RTFEditorKit;

public class MyTextPane extends JTextPane {

    protected StyleContext m_context;
    protected DefaultStyledDocument m_doc;
    private   MutableAttributeSet keyAttr,normalAttr;
    private   MutableAttributeSet inputAttributes =
                new RTFEditorKit().getInputAttributes();

 private String[] keyWord={"SELECT","FROM","WHERE"};

    public MyTextPane() {
        super();
        m_context = new StyleContext();
        m_doc = new DefaultStyledDocument(m_context);
        this.setDocument(m_doc);

        this.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent ke) {
                syntaxParse();
            }
        });

  义关键字显示属性
        keyAttr = new SimpleAttributeSet();
        StyleConstants.setForeground(keyAttr, Color.blue);

  义一般文本显示属性
        normalAttr = new SimpleAttributeSet();
        StyleConstants.setForeground(normalAttr, Color.black);
    }

    public void syntaxParse() {
        try {
            String s = null;
            Element root = m_doc.getDefaultRootElement();
光标当前行
            int cursorPos = this.getCaretPosition();    前光标的位置
            int line = root.getElementIndex(cursorPos); 当前行

            Element para = root.getElement(line);
            int start = para.getStartOffset();
            int end = para.getEndOffset() - 1; 除\r字符
            s = m_doc.getText(start, end - start).toUpperCase();

            int i = 0;
            int xStart = 0;

析关键字---
            m_doc.setCharacterAttributes(start, s.length(),normalAttr, false);
            MyStringTokenizer st = new MyStringTokenizer(s);
            while( st.hasMoreTokens()) {
                s = st.nextToken();
                if ( s == null) return;
                for (i = 0; i < keyWord.length; i++ ) {
                    if (s.equals(keyWord[i])) break;
                }
                if ( i >= keyWord.length ) continue;

                xStart = st.getCurrPosition();

                置关键字显示属性
                m_doc.setCharacterAttributes(start+xStart, s.length(),
                                             keyAttr, false);
            }
            inputAttributes.addAttributes(normalAttr);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("test text pane");
        frame.getContentPane().add(new MyTextPane());
        WindowListener wndCloser = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };
        frame.addWindowListener(wndCloser);
        final int inset = 50;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setBounds ( inset, inset, screenSize.width - inset*2, screenSize.height - inset*2 );
        frame.show();
    }
}

/*在分析字符串的同时,记录每个token所在的位置
*
*/
class MyStringTokenizer extends StringTokenizer{
    String sval = " ";
    String oldStr,str;
    int m_currPosition = 0,m_beginPosition=0;
    MyStringTokenizer(String str) {
        super(str," ");
        this.oldStr = str;
        this.str = str;
    }

    public String nextToken() {
       try {
           String s = super.nextToken();
           int pos = -1;

           if (oldStr.equals(s)) {
               return s;
           }

           pos = str.indexOf(s + sval);
           if ( pos == -1) {
               pos = str.indexOf(sval + s);
               if ( pos == -1)
                   return null;
               else pos += 1;
           }

           int xBegin = pos + s.length();
           str = str.substring(xBegin);

           m_currPosition = m_beginPosition + pos;
           m_beginPosition = m_beginPosition + xBegin;
           return s;
       } catch (java.util.NoSuchElementException ex) {
           ex.printStackTrace();
           return null;
       }
    }

   回token在字符串中的位置
   public int getCurrPosition() {
       return m_currPosition;
   }
}


文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网