JAVA Swing JTextPane 扩展,实现文本高亮

 JTextPane 扩展改造,可以实现多关键字不同颜色高亮,日期高亮 和  注释高亮,行数控制

import java.awt.Color;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import org.apache.commons.lang3.time.DateUtils;
import com.google.common.collect.ImmutableMap;

import freemarker.template.utility.DateUtil;
import psl.dg.com.StringUtils;

/**
 * 自定义的JTextPane,扩展了JTextPane没有的一些功能(java关键字、单行注释、多行注释加亮)
 * 
 * @author yangze
 * @since 2019-09-15
 * 
 */

public class MyJTextPane extends JTextPane implements DocumentListener {
    private Set keywords;
    private Map key2Color = new HashMap();
    private Style keywordStyle;
    private Style normalStyle;
    private Style datetimeStyle;
    private Style classNameStyle;
    private int holdLineNum;

    public MyJTextPane() {
        super();
        this.getDocument().addDocumentListener(this);
        // 准备着色使用的样式
        /*keywordStyle = ((StyledDocument) getDocument()).addStyle(
                "Keyword_Style", null);
        

        StyleConstants.setForeground(keywordStyle, Color.BLUE);*/
        
        normalStyle = ((StyledDocument) getDocument()).addStyle(
                "Keyword_Style", null);
        StyleConstants.setForeground(normalStyle, Color.BLACK);
        
        datetimeStyle = ((StyledDocument) getDocument()).addStyle(
                "Keyword_Style", null);
        StyleConstants.setForeground(datetimeStyle, Color.BLUE);
        

        key2Color = ImmutableMap.of("导入CSV文件", Color.GREEN, "导入FTL文件", Color.GREEN,"错误",Color.RED);  
        
        //获取关键字
        keywords = key2Color.keySet();
        
        holdLineNum = -1;

    }

    /**
     * 设置全文本属性
     * 
     * @param attr
     * @param replace
     */
    public void setTextAttributes(AttributeSet attr, boolean replace) {
        int p0 = 0;
        int p1 = this.getText().length();
        if (p0 != p1) {
            StyledDocument doc = getStyledDocument();
            doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
        } else {
            MutableAttributeSet inputAttributes = getInputAttributes();
            if (replace) {
                inputAttributes.removeAttributes(inputAttributes);
            }
            inputAttributes.addAttributes(attr);
        }
    }

    /**
     * 单行注释
     */
    public void setSingleLineNoteCharacterAttributes() {
        String text = this.getText();
        int startPointer = 0;
        int endPointer = 0;
        if ((startPointer = text.indexOf("//")) == -1) {
            return;
        }

        while ((endPointer = text.substring(startPointer).indexOf("\n")) != -1) {
            endPointer = startPointer + endPointer;
            if (startPointer >= endPointer) {
                break;
            }
            int hangshu = text.substring(0, endPointer).split("\\n").length;
            System.out.println("hangshu:" + hangshu);
            SwingUtilities
                    .invokeLater(new ColouringWord(this, startPointer - hangshu
                            + 1, endPointer - hangshu, new Color(63, 217, 95)));
            startPointer = text.substring(endPointer + 1).indexOf("//");
            startPointer = startPointer + endPointer + 1;

        }
    }

    /**
     * 多行注释
     */
    public void setMultiLineNoteCharacterAttributes() {
        String text = this.getText();
        int startPointer = 0;
        int endPointer = 0;
        if ((startPointer = text.indexOf("/*")) == -1) {
            return;
        }

        while ((endPointer = text.substring(startPointer).indexOf("*/")) != -1) {
            endPointer = startPointer + endPointer;
            if (startPointer >= endPointer) {
                break;
            }
            int hangshu = text.substring(0, endPointer).split("\\n").length;
            int kuaju = text.substring(startPointer, endPointer).split("\\n").length;
            SwingUtilities.invokeLater(new ColouringWord(this, startPointer
                    - hangshu + kuaju, endPointer + 3 - hangshu, new Color(63,
                    217, 95)));
            startPointer = text.substring(endPointer + 1).indexOf("/*");
            startPointer = startPointer + endPointer + 1;

        }
    }

    /**
     * 实时加亮关键字
     * @param styledDocument
     * @param pos
     * @param len
     * @throws BadLocationException
     */
    public void myColouring(StyledDocument styledDocument, int pos, int len)
            throws BadLocationException {
        int start = indexOfWordStart(styledDocument, pos);
        int end = indexOfWordEnd(styledDocument, pos + len);

        char ch;
        while (start < end) {
            ch = getCharAt(styledDocument, start);
            if (Character.isLetter(ch) || ch == '_') {//判断是否为字母
                start = myColouringWord(styledDocument, start);
            } 
            else if(Character.isDigit(ch)){
            	start = myColouringDateTime(styledDocument, start);
            }else {
                SwingUtilities.invokeLater(new ColouringTask(styledDocument,
                        start, 1, normalStyle));
                ++start;
            }
        }
    }

    /**
     * 实时着色
     * 
     * @param doc
     * @param pos
     * @return
     * @throws BadLocationException
     */
    public int myColouringWord(StyledDocument doc, int pos)
            throws BadLocationException {
        int wordEnd = indexOfWordEnd(doc, pos);
        String word = doc.getText(pos, wordEnd - pos);

       if (keywords.contains(word)) {

        	keywordStyle = ((StyledDocument) getDocument()).addStyle(
                    "Keyword_Style", null);
              StyleConstants.setForeground(keywordStyle, key2Color.get(word));
              
            SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd
                    - pos, keywordStyle));
        } else {
            SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd
                    - pos, normalStyle));
        }

        return wordEnd;
    }
    
    public int myColouringDateTime(StyledDocument doc, int pos)
            throws BadLocationException {
    	
    	int endPos = doc.getLength();
    	if(endPos - pos < 19) return pos+1;
    	else {
    		 int wordEnd = pos + 19;
    		 String word = doc.getText(pos, wordEnd - pos);

 	        if(psl.dg.com.DateUtils.isDatetime(word)) {
 	        	SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd
 	                    - pos, datetimeStyle));
 	        	return wordEnd;
 	        }
 	        return pos+1;
    	}
    }

    /**
     * 取得在文档中下标在pos处的字符.
     * 
     * @param doc
     * @param pos
     * @return
     * @throws BadLocationException
     */
    public char getCharAt(Document doc, int pos) throws BadLocationException {
        return doc.getText(pos, 1).charAt(0);
    }

    /**
     * 取得下标为pos时, 它所在的单词开始的下标.
     * 
     * @param doc
     * @param pos
     * @return
     * @throws BadLocationException
     */
    public int indexOfWordStart(Document doc, int pos)
            throws BadLocationException {
        // 从pos开始向前找到第一个非单词字符.
        for (; pos > 0 && isWordCharacter(doc, pos - 1); --pos)
            ;

        return pos;
    }
    
    public void setHoldLineNum(int num) {
    	this.holdLineNum = num;
    	this.setText(checkLine(this.getText()));
    }
    
    public String checkLine(String oldStr) {
    	if(holdLineNum > 0) {
    		String[] strArr = oldStr.split("\n");
			if(holdLineNum < strArr.length ) {
			
				String newStr = "";
				for(int i = strArr.length - holdLineNum ; i < strArr.length; ++i) {
					newStr += strArr[i] +  "\n";
				}
				
				newStr = newStr.substring(0,newStr.length() - 1);
				return newStr;
			}
			else {
				return oldStr;
			}
    	}else if(holdLineNum == 0) {
    		return "";
    	}else{
    		return oldStr;
    	}
    	
    }
    
    
    public void appendLine(String text) {
    	String str = this.getText();
    	if(StringUtils.isBlank(str)) {
    		
    		this.setText(checkLine(text));
    	}
    	else{
    		this.setText(checkLine(str + "\n"  + text));
    	}
    }

    /**
     * 取得下标为pos时, 它所在的单词结束的下标.
     * 
     * @param doc
     * @param pos
     * @return
     * @throws BadLocationException
     */
    public int indexOfWordEnd(Document doc, int pos)
            throws BadLocationException {
        // 从pos开始向前找到第一个非单词字符.
        for (; isWordCharacter(doc, pos); ++pos)
            ;

        return pos;
    }
    
 
    /**
     * 如果一个字符是字母, 数字, 下划线, 则返回true.
     * 
     * @param doc
     * @param pos
     * @return
     * @throws BadLocationException
     */
    public boolean isWordCharacter(Document doc, int pos)
            throws BadLocationException {
        char ch = getCharAt(doc, pos);
        if (Character.isLetter(ch) || Character.isDigit(ch) || ch == '_' ) {
            return true;
        }
        return false;
    }
    
  
    @Override
    // 给出属性或属性集发生了更改的通知
    public void changedUpdate(DocumentEvent e) {

    }

    @Override
    // 给出对文档执行了插入操作的通知
    public void insertUpdate(DocumentEvent e) {
        try {
            myColouring((StyledDocument) e.getDocument(), e.getOffset(),
                    e.getLength());
            // noteFinder.ColorNote(this.getText());// 给注释上色
            setSingleLineNoteCharacterAttributes();
            setMultiLineNoteCharacterAttributes();
        } catch (BadLocationException e1) {
            e1.printStackTrace();
        }
    }

    @Override
    // 给出移除了一部分文档的通知
    public void removeUpdate(DocumentEvent e) {
        try {
            // 因为删除后光标紧接着影响的单词两边, 所以长度就不需要了
            myColouring((StyledDocument) e.getDocument(), e.getOffset(), 0);
            // noteFinder.ColorNote(this.getText());// 给注释上色
            setSingleLineNoteCharacterAttributes();
            setMultiLineNoteCharacterAttributes();
        } catch (BadLocationException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 多线程绘制颜色
     */
    private class ColouringTask implements Runnable {
        private StyledDocument doc;
        private Style style;
        private int pos;
        private int len;

        public ColouringTask(StyledDocument doc, int pos, int len, Style style) {
            this.doc = doc;
            this.pos = pos;
            this.len = len;
            this.style = style;
        }

        public void run() {
            try {
                // 这里就是对字符进行着色
                doc.setCharacterAttributes(pos, len, style, false);
            } catch (Exception e) {
            }
        }
    }

}

/**
 * 多线程绘制颜色
 * 
 * 
 * 
 */
class ColouringWord implements Runnable {
    private int startPointer;
    private int endPointer;
    private Color color;
    private JTextPane jTextPane;

    public ColouringWord(JTextPane jTextPane, int pos, int len, Color color) {
        this.jTextPane = jTextPane;
        this.startPointer = pos;
        this.endPointer = len;
        this.color = color;
    }

    @Override
    public void run() {
        SimpleAttributeSet attributeSet = new SimpleAttributeSet();
        StyleConstants.setForeground(attributeSet, color);
        boolean replace = false;
        int p0 = startPointer;
        int p1 = endPointer;
        if (p0 != p1) {
            StyledDocument doc = jTextPane.getStyledDocument();
            doc.setCharacterAttributes(p0, p1 - p0, attributeSet, replace);
        } else {
            MutableAttributeSet inputAttributes = jTextPane
                    .getInputAttributes();
            if (replace) {
                inputAttributes.removeAttributes(inputAttributes);
            }
            inputAttributes.addAttributes(attributeSet);
        }
    }
}

 

你可能感兴趣的:(JAVA)