idltexteditor.java

来自「UCS (Ultra Corba Simulator) is one more 」· Java 代码 · 共 250 行

JAVA
250
字号
/**
 * (c) 2007 UCS_2008
 *
 * Project    UCS (Ultra Corba Simulator)
 * Subproject CorbaMNQ
 * File       IdlTextEditor.java
 * Created on Sep 13, 2007 by ucs_2008
 *
 * History:
 * Date(Y.M.D) User           Reason (plus CR, LM, Fault number)
 *
 */

package com.corba.mnq.ui.text.idl;

import com.corba.mnq.tool.idl.IdlFile;

import java.awt.Color;

import javax.swing.JEditorPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.Element;

import java.util.StringTokenizer;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * name: "IdlTextEditor"
 * 
 * @author ucs_2008
 */
public class IdlTextEditor extends JEditorPane {

    public static final String CHAR_LITERAL_COLOR = "ucs.char_literal_color";

    public static final String COMMON_WORD_COLOR = "ucs.common_word_color";

    public static final String INDENT_ON_INSERT_BREAK = "indentOnInsertBreak";

    public static final String KEYWORD_COLOR = "ucs.keyword_color";

    /** used for java.util.logging */
    private static final Logger LOG = Logger.getLogger(IdlTextEditor.class.getName());

    public static final String MULTI_LINE_COMMENT_COLOR = "ucs.multi_line_color";

    public static final String NUMERIC_LITERAL_COLOR = "ucs.numeric_literal_color";

    public static final String OPERATOR_COLOR = "ucs.operator_color";

    static final long serialVersionUID = -5737922218642574020L;

    public static final String SINGLE_LINE_COMMENT_COLOR = "ucs.single_line_color";

    public static final String STRING_LITERAL_COLOR = "ucs.string_literal_color";

    public static final String TAB_SIZE = "    ";

    public static final String TYPE_COLOR = "ucs.type_color";

    private Document doc = null;

    // store the idl file node
    public IdlFile fn = null;

    /**
     * This is one class constructor
     */
    public IdlTextEditor() {
        try {
            doc = getDocument();
            setOpaque(true);
            // setBackground(new Color(231, 255, 206));
            // setForeground(Color.darkGray);
            setEditable(false);
            setIndentOnBreak(true);
            setKeywordColor(Color.blue);
            setCommonWordColor(Color.black);
            setCharLiteralColor(new Color(83, 114, 238));
            setMultiLineCommentColor(new Color(0, 200, 0));
            setNumericLiteralColor(new Color(255, 128, 64));
            setSingleLineCommentColor(new Color(0, 200, 0));
            setStringLiteralColor(new Color(83, 114, 238));
            setOperatorColor(new Color(223, 72, 227));
            setTypeColor(new Color(255, 0, 255));
            // setDragEnabled(true);

        } catch (Exception ex) {
            LOG.severe(ex.getMessage());
        }

        setOpaque(true);
    }

    public void clear() {
        try {
            doc.remove(0, doc.getLength());
        } catch (Exception e) {
            LOG.severe(e.getMessage());
        }
    }

    protected EditorKit createDefaultEditorKit() {
        return new IdlEditorKit();
    }

    public Color getCharLiteralColor() {
        return (Color) getClientProperty(CHAR_LITERAL_COLOR);
    }

    public Color getCommonWordColor() {
        return (Color) getClientProperty(COMMON_WORD_COLOR);
    }

    private Element getCurrentRow() {
        return getRowAt(getCaretPosition());
    }

    public boolean getIndentOnBreak() {
        Boolean b = (Boolean) getClientProperty(INDENT_ON_INSERT_BREAK);
        if (b != null)
            return false;
        else
            return b.booleanValue();
    }

    public Color getKeywordColor() {
        return (Color) getClientProperty(KEYWORD_COLOR);
    }

    private String getLastToken(String rowSegment) throws BadLocationException {
        int slashSlashIndex = rowSegment.indexOf("//");
        int slashStarIndex = rowSegment.indexOf("/*");
        int starSlashIndex = rowSegment.indexOf("*/");
        if (slashSlashIndex != -1) {
            rowSegment = rowSegment.substring(0, slashSlashIndex);
        } else if (slashStarIndex != -1) {
            try {
                rowSegment = rowSegment.substring(0, slashStarIndex)
                        + rowSegment.substring(starSlashIndex, rowSegment.length());
            } catch (StringIndexOutOfBoundsException exc) {
                rowSegment = rowSegment.substring(0, slashStarIndex);
            }
        }
        StringTokenizer st = new StringTokenizer(rowSegment, "; ", false);
        String lastToken = "";
        if (st.hasMoreTokens()) {
            lastToken = st.nextToken();
        }
        System.out.println("last token=" + lastToken);
        return lastToken;
    }

    public Color getMultiLineCommentColor() {
        return (Color) getClientProperty(MULTI_LINE_COMMENT_COLOR);
    }

    public Color getNumericLiteralColor() {
        return (Color) getClientProperty(NUMERIC_LITERAL_COLOR);
    }

    public Color getOperatorColor() {
        return (Color) getClientProperty(OPERATOR_COLOR);
    }

    private Element getRowAt(int offset) {
        Element element = getDocument().getDefaultRootElement();
        int rowNumber = element.getElementIndex(offset);
        return element.getElement(rowNumber);
    }

    public Color getSingleLineCommentColor() {
        return (Color) getClientProperty(SINGLE_LINE_COMMENT_COLOR);
    }

    public Color getStringLiteralColor() {
        return (Color) getClientProperty(STRING_LITERAL_COLOR);
    }

    public Color getTypeColor() {
        return (Color) getClientProperty(TYPE_COLOR);
    }

    public int getWindowsReturnGap(String content, int end) {
        String part = content.substring(0, end);
        Pattern partPattern = Pattern.compile("\n");
        Matcher partMatcher = partPattern.matcher(part);
        int count = 0;
        while (partMatcher.find()) {
            count++;
        }
        return count;
    }

    public void setCharLiteralColor(Color c) {
        putClientProperty(CHAR_LITERAL_COLOR, c);
    }

    public void setCommonWordColor(Color c) {
        putClientProperty(COMMON_WORD_COLOR, c);
    }

    public void setIndentOnBreak(boolean b) {
        putClientProperty(INDENT_ON_INSERT_BREAK, new Boolean(b));
    }

    public void setKeywordColor(Color c) {
        putClientProperty(KEYWORD_COLOR, c);
    }

    public void setMultiLineCommentColor(Color c) {
        putClientProperty(MULTI_LINE_COMMENT_COLOR, c);
    }

    public void setNumericLiteralColor(Color c) {
        putClientProperty(NUMERIC_LITERAL_COLOR, c);
    }

    public void setOperatorColor(Color c) {
        putClientProperty(OPERATOR_COLOR, c);
    }

    public void setSingleLineCommentColor(Color c) {
        putClientProperty(SINGLE_LINE_COMMENT_COLOR, c);
    }

    public void setStringLiteralColor(Color c) {
        putClientProperty(STRING_LITERAL_COLOR, c);
    }

    public void setTypeColor(Color c) {
        putClientProperty(TYPE_COLOR, c);
    }

    public void showIDL(String idl) {
        try {
            doc.remove(0, doc.getLength());
            doc.insertString(0, idl, null);
        } catch (Exception e) {
            LOG.severe(e.getMessage());
        }
        setCaretPosition(0);
    }
}
/* EOF */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?